| 1 | "use client"; |
| 2 | |
| 3 | import { Users } from |
| 4 | import { useRouter } |
| 5 | import { useAction } |
| 6 | import { Fragment, type |
| 7 | import { toast } |
| 8 | |
| 9 | import { FromNowHoverCard } |
| 10 | import { RoleSelect } |
| 11 | import { Avatar, AvatarFallback |
| 12 | import { Badge } |
| 13 | import { Button } |
| 14 | import { Card } |
| 15 | import { |
| 16 | Empty, |
| 17 | EmptyDescription, |
| 18 | EmptyHeader, |
| 19 | EmptyMedia, |
| 20 | EmptyTitle, |
| 21 | } from "@/components/ui/empty"; |
| 22 | import { Field, FieldDescription |
| 23 | import { Input } |
| 24 | import { |
| 25 | Item, |
| 26 | ItemActions, |
| 27 | ItemContent, |
| 28 | ItemDescription, |
| 29 | ItemGroup, |
| 30 | ItemMedia, |
| 31 | ItemSeparator, |
| 32 | ItemTitle, |
| 33 | } from "@/components/ui/item"; |
| 34 | import type { components |
| 35 | import { |
| 36 | type CollaboratorRole, |
| 37 | collaboratorRoles, |
| 38 | roleDescriptions, |
| 39 | } from "@/lib/repository-role"; |
| 40 | |
| 41 | import { inviteCollaborator, removeCollaborator |
| 42 | import { SettingCard } |
| 43 | |
| 44 | type Collaborator |
| 45 | |
| 46 | type RepositoryProps |
| 47 | username: string |
| 48 | slug: string |
| 49 | }; |
| 50 | |
| 51 | export function RepositoryCollaborators({ |
| 52 | username, |
| 53 | slug, |
| 54 | collaborators, |
| 55 | }: RepositoryProps |
| 56 | const router = |
| 57 | |
| 58 | const change = |
| 59 | onSuccess: () => |
| 60 | onError: ({ error |
| 61 | toast.error(error |
| 62 | }); |
| 63 | const remove = |
| 64 | onSuccess: () => |
| 65 | onError: ({ error |
| 66 | toast.error(error |
| 67 | }); |
| 68 | const setRole = |
| 69 | change.execute({ |
| 70 | const removeUser = |
| 71 | remove.execute({ |
| 72 | |
| 73 | const members = |
| 74 | const invited = |
| 75 | const busy = |
| 76 | |
| 77 | return ( |
| 78 | <div className="flex flex-col gap-8 |
| 79 | <InviteCard username={ |
| 80 | |
| 81 | <section> |
| 82 | <h2 className="mb-3 text-sm font-semibold |
| 83 | Collaborators |
| 84 | <span className="ml-2 font-normal text-muted-foreground tabular-nums |
| 85 | {members.length} |
| 86 | </span> |
| 87 | </h2> |
| 88 | <Card className=" |
| 89 | {members.length === |
| 90 | <Empty className=" |
| 91 | <EmptyHeader> |
| 92 | <EmptyMedia variant=" |
| 93 | <Users /> |
| 94 | </EmptyMedia> |
| 95 | <EmptyTitle>No collaborators yet</EmptyTitle |
| 96 | <EmptyDescription> |
| 97 | People you invite show up here once they accept. |
| 98 | </EmptyDescription> |
| 99 | </EmptyHeader> |
| 100 | </Empty> |
| 101 | ) : ( |
| 102 | <ItemGroup className=" |
| 103 | {members.map(( |
| 104 | <Fragment key={ |
| 105 | {index > 0 |
| 106 | <PersonRow collaborator={ |
| 107 | <RoleSelect |
| 108 | roles={collaboratorRoles} |
| 109 | value={collaborator.role |
| 110 | disabled={busy} |
| 111 | onChange={(role) |
| 112 | label={`Role for ${collaborator |
| 113 | /> |
| 114 | <Button |
| 115 | size="sm" |
| 116 | variant="ghost" |
| 117 | disabled={busy} |
| 118 | onClick={() => |
| 119 | > |
| 120 | Remove |
| 121 | </Button> |
| 122 | </PersonRow> |
| 123 | </Fragment> |
| 124 | ))} |
| 125 | </ItemGroup> |
| 126 | )} |
| 127 | </Card> |
| 128 | </section> |
| 129 | |
| 130 | {invited.length > |
| 131 | <section> |
| 132 | <h2 className="mb-3 text-sm font-semibold |
| 133 | <Card className=" |
| 134 | <ItemGroup className=" |
| 135 | {invited.map(( |
| 136 | <Fragment key={ |
| 137 | {index > 0 |
| 138 | <PersonRow collaborator={ |
| 139 | {collaborator.status === |
| 140 | <Button |
| 141 | size="sm" |
| 142 | variant="outline" |
| 143 | disabled={busy} |
| 144 | onClick={() => |
| 145 | setRole(collaborator.username |
| 146 | } |
| 147 | > |
| 148 | Resend |
| 149 | </Button> |
| 150 | )} |
| 151 | <Button |
| 152 | size="sm" |
| 153 | variant="ghost" |
| 154 | disabled={busy} |
| 155 | onClick={() => |
| 156 | > |
| 157 | Cancel |
| 158 | </Button> |
| 159 | </PersonRow> |
| 160 | </Fragment> |
| 161 | ))} |
| 162 | </ItemGroup> |
| 163 | </Card> |
| 164 | </section> |
| 165 | )} |
| 166 | </div> |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | function InviteCard({ username |
| 171 | const router = |
| 172 | const [invitee, |
| 173 | const [role, |
| 174 | |
| 175 | const invite = |
| 176 | onSuccess: ({ input |
| 177 | toast.success(`Invitation sent to |
| 178 | setInvitee(""); |
| 179 | router.refresh(); |
| 180 | }, |
| 181 | onError: ({ error |
| 182 | toast.error(error |
| 183 | }); |
| 184 | |
| 185 | return ( |
| 186 | <SettingCard |
| 187 | title="Invite a collaborator" |
| 188 | hint="They get access once they accept. Invitations lapse after 7 days." |
| 189 | onSubmit={() => |
| 190 | invite.execute({ |
| 191 | } |
| 192 | pending={invite.isExecuting |
| 193 | canSave={Boolean(invitee |
| 194 | submitText="Send invitation" |
| 195 | > |
| 196 | <div className="flex flex-col gap-4 sm:flex-row |
| 197 | <Field className=" |
| 198 | <FieldLabel htmlFor=" |
| 199 | <Input |
| 200 | id="invite-username" |
| 201 | autoComplete="off" |
| 202 | placeholder="octocat" |
| 203 | value={invitee} |
| 204 | onChange={(event) |
| 205 | /> |
| 206 | </Field> |
| 207 | <Field className=" |
| 208 | <FieldLabel htmlFor=" |
| 209 | <RoleSelect |
| 210 | id="invite-role" |
| 211 | roles={collaboratorRoles} |
| 212 | value={role} |
| 213 | onChange={setRole} |
| 214 | /> |
| 215 | </Field> |
| 216 | </div> |
| 217 | <FieldDescription className=" |
| 218 | <span className="font-medium capitalize text-foreground |
| 219 | {": "} |
| 220 | {roleDescriptions[role]} |
| 221 | </FieldDescription> |
| 222 | </SettingCard> |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | function PersonRow({ |
| 227 | collaborator, |
| 228 | children, |
| 229 | }: { |
| 230 | collaborator: Collaborator |
| 231 | children: ReactNode |
| 232 | }) { |
| 233 | return ( |
| 234 | <Item className=" |
| 235 | <ItemMedia> |
| 236 | <Avatar className=" |
| 237 | {collaborator.image && |
| 238 | <AvatarImage src={ |
| 239 | )} |
| 240 | <AvatarFallback> |
| 241 | {collaborator.username.slice |
| 242 | </AvatarFallback> |
| 243 | </Avatar> |
| 244 | </ItemMedia> |
| 245 | <ItemContent className=" |
| 246 | <ItemTitle> |
| 247 | {collaborator.username} |
| 248 | {collaborator.status !== |
| 249 | <Badge variant=" |
| 250 | {collaborator.role} |
| 251 | </Badge> |
| 252 | )} |
| 253 | {collaborator.status === |
| 254 | <Badge variant=" |
| 255 | Expired |
| 256 | </Badge> |
| 257 | )} |
| 258 | </ItemTitle> |
| 259 | <ItemDescription className=" |
| 260 | {collaborator.status === |
| 261 | collaborator.name |
| 262 | ) : ( |
| 263 | <> |
| 264 | Invited <FromNowHoverCard date={ |
| 265 | {collaborator.status === |
| 266 | <FromNowHoverCard date={ |
| 267 | </> |
| 268 | )} |
| 269 | </ItemDescription> |
| 270 | </ItemContent> |
| 271 | <ItemActions>{children} |
| 272 | </Item> |
| 273 | ); |
| 274 | } |