| 1 | import { useRouter } from '@bprogress/next/app' |
| 2 | import { UsersRound, X |
| 3 | import { useAction } |
| 4 | import { useState } |
| 5 | import { toast } |
| 6 | import { Avatar, AvatarFallback |
| 7 | import { Badge } |
| 8 | import { Button } |
| 9 | import { |
| 10 | Dialog, |
| 11 | DialogContent, |
| 12 | DialogHeader, |
| 13 | DialogTitle, |
| 14 | } from '@/components/ui/dialog' |
| 15 | import { |
| 16 | Empty, |
| 17 | EmptyDescription, |
| 18 | EmptyHeader, |
| 19 | EmptyMedia, |
| 20 | EmptyTitle, |
| 21 | } from '@/components/ui/empty' |
| 22 | import { |
| 23 | Select, |
| 24 | SelectContent, |
| 25 | SelectItem, |
| 26 | SelectTrigger, |
| 27 | SelectValue, |
| 28 | } from '@/components/ui/select' |
| 29 | import { assignTeamAction, unassignTeamAction |
| 30 | import type { OrgTeam, |
| 31 | |
| 32 | export default function TeamsSection |
| 33 | projectId, |
| 34 | orgSlug, |
| 35 | projectSlug, |
| 36 | projectTeams, |
| 37 | orgTeams, |
| 38 | canManage, |
| 39 | showAddDialog, |
| 40 | onShowAddDialogChange, |
| 41 | }: { |
| 42 | projectId: string |
| 43 | orgSlug: string |
| 44 | projectSlug: string |
| 45 | projectTeams: ProjectTeam |
| 46 | orgTeams: OrgTeam |
| 47 | canManage: boolean |
| 48 | showAddDialog: boolean |
| 49 | onShowAddDialogChange: (open |
| 50 | }) { |
| 51 | const router = |
| 52 | const [selectedTeamId, |
| 53 | |
| 54 | const { execute: |
| 55 | assignTeamAction, |
| 56 | { |
| 57 | onSuccess() { |
| 58 | toast.success('Team assigned to project |
| 59 | onShowAddDialogChange(false) |
| 60 | setSelectedTeamId('') |
| 61 | router.refresh() |
| 62 | }, |
| 63 | onError({ error |
| 64 | toast.error(error |
| 65 | }, |
| 66 | } |
| 67 | ) |
| 68 | |
| 69 | const { execute: |
| 70 | onSuccess() { |
| 71 | toast.success('Team removed from project |
| 72 | router.refresh() |
| 73 | }, |
| 74 | onError({ error |
| 75 | toast.error(error |
| 76 | }, |
| 77 | }) |
| 78 | |
| 79 | const assignedTeamIds = |
| 80 | const availableTeams = |
| 81 | |
| 82 | if (projectTeams.length |
| 83 | return ( |
| 84 | <Empty> |
| 85 | <EmptyHeader> |
| 86 | <EmptyMedia variant=' |
| 87 | <UsersRound /> |
| 88 | </EmptyMedia> |
| 89 | <EmptyTitle>No teams assigned</EmptyTitle |
| 90 | <EmptyDescription> |
| 91 | No teams have been assigned to this project yet. |
| 92 | </EmptyDescription> |
| 93 | </EmptyHeader> |
| 94 | </Empty> |
| 95 | ) |
| 96 | } |
| 97 | |
| 98 | return ( |
| 99 | <> |
| 100 | {projectTeams.length === |
| 101 | <Empty> |
| 102 | <EmptyHeader> |
| 103 | <EmptyMedia variant=' |
| 104 | <UsersRound /> |
| 105 | </EmptyMedia> |
| 106 | <EmptyTitle>No teams yet</EmptyTitle |
| 107 | <EmptyDescription> |
| 108 | Assign teams from your workspace to this project. |
| 109 | </EmptyDescription> |
| 110 | </EmptyHeader> |
| 111 | </Empty> |
| 112 | ) : ( |
| 113 | <div className='space-y-4 |
| 114 | {projectTeams.map(( |
| 115 | <div className='rounded-lg border p-4 |
| 116 | <div className='mb-3 flex items-center justify-between |
| 117 | <div className='flex items-center gap-2 |
| 118 | <UsersRound className=' |
| 119 | <span className='font-medium |
| 120 | <Badge className=' |
| 121 | {team.members.length |
| 122 | {team.members.length |
| 123 | </Badge> |
| 124 | </div> |
| 125 | {canManage && ( |
| 126 | <Button |
| 127 | onClick={() => |
| 128 | executeUnassign({ |
| 129 | assignmentId: team.assignmentId |
| 130 | orgSlug, |
| 131 | projectSlug, |
| 132 | }) |
| 133 | } |
| 134 | variant='ghost' |
| 135 | > |
| 136 | <X className=' |
| 137 | </Button> |
| 138 | )} |
| 139 | </div> |
| 140 | {team.members.length |
| 141 | <div className='flex flex-wrap gap-2 |
| 142 | {team.members.map |
| 143 | <div |
| 144 | className='flex items-center gap-2 rounded-full bg-muted/50 px-3 py-1' |
| 145 | key={m.userId |
| 146 | > |
| 147 | <Avatar className=' |
| 148 | <AvatarImage alt={ |
| 149 | <AvatarFallback className=' |
| 150 | {m.userName.substring |
| 151 | </AvatarFallback> |
| 152 | </Avatar> |
| 153 | <span className='text-xs |
| 154 | </div> |
| 155 | ))} |
| 156 | </div> |
| 157 | )} |
| 158 | </div> |
| 159 | ))} |
| 160 | </div> |
| 161 | )} |
| 162 | |
| 163 | <Dialog onOpenChange={ |
| 164 | <DialogContent className=' |
| 165 | <DialogHeader> |
| 166 | <DialogTitle>Assign Team to Project</DialogTitle |
| 167 | </DialogHeader> |
| 168 | <div className='space-y-4 |
| 169 | <Select onValueChange={ |
| 170 | <SelectTrigger> |
| 171 | <SelectValue placeholder=' |
| 172 | </SelectTrigger> |
| 173 | <SelectContent> |
| 174 | {availableTeams.map(( |
| 175 | <SelectItem key={ |
| 176 | {t.teamName} |
| 177 | </SelectItem> |
| 178 | ))} |
| 179 | </SelectContent> |
| 180 | </Select> |
| 181 | <div className='flex justify-end gap-2 |
| 182 | <Button |
| 183 | onClick={() => |
| 184 | variant='outline' |
| 185 | > |
| 186 | Cancel |
| 187 | </Button> |
| 188 | <Button |
| 189 | disabled={!selectedTeamId} |
| 190 | loading={isAssigning} |
| 191 | onClick={() => |
| 192 | executeAssign({ |
| 193 | projectId, |
| 194 | teamId: selectedTeamId, |
| 195 | }) |
| 196 | } |
| 197 | > |
| 198 | Assign Team |
| 199 | </Button> |
| 200 | </div> |
| 201 | </div> |
| 202 | </DialogContent> |
| 203 | </Dialog> |
| 204 | </> |
| 205 | ) |
| 206 | } |