| 1 | "use client"; |
| 2 | |
| 3 | import { Users } from |
| 4 | import { useRouter } |
| 5 | import { useAction } |
| 6 | import { Fragment } |
| 7 | import { toast } |
| 8 | |
| 9 | import { Card } |
| 10 | import { |
| 11 | Empty, |
| 12 | EmptyDescription, |
| 13 | EmptyHeader, |
| 14 | EmptyMedia, |
| 15 | EmptyTitle, |
| 16 | } from "@/components/ui/empty"; |
| 17 | import { |
| 18 | Item, |
| 19 | ItemActions, |
| 20 | ItemContent, |
| 21 | ItemDescription, |
| 22 | ItemGroup, |
| 23 | ItemMedia, |
| 24 | ItemSeparator, |
| 25 | ItemTitle, |
| 26 | } from "@/components/ui/item"; |
| 27 | import { |
| 28 | Select, |
| 29 | SelectContent, |
| 30 | SelectItem, |
| 31 | SelectSeparator, |
| 32 | SelectTrigger, |
| 33 | SelectValue, |
| 34 | } from "@/components/ui/select"; |
| 35 | import type { components |
| 36 | import { |
| 37 | type CollaboratorRole, |
| 38 | collaboratorRoles, |
| 39 | } from "@/lib/repository-role"; |
| 40 | |
| 41 | import { removeTeamAccess, setTeamRole |
| 42 | |
| 43 | type Team |
| 44 | |
| 45 | const NO_ACCESS = " |
| 46 | |
| 47 | /** Every team in the organization, each with a role here or none. Choosing a role grants it to all of the team's members. */ |
| 48 | export function RepositoryTeams({ |
| 49 | username, |
| 50 | slug, |
| 51 | teams, |
| 52 | }: { |
| 53 | username: string |
| 54 | slug: string |
| 55 | teams: Team |
| 56 | }) { |
| 57 | const router = |
| 58 | const callbacks = |
| 59 | onSuccess: () => |
| 60 | onError: ({ error |
| 61 | toast.error(error |
| 62 | }; |
| 63 | const grant = |
| 64 | const revoke = |
| 65 | const busy = |
| 66 | |
| 67 | return ( |
| 68 | <section> |
| 69 | <h2 className="mb-3 text-sm font-semibold |
| 70 | <Card className=" |
| 71 | {teams.length === |
| 72 | <Empty className=" |
| 73 | <EmptyHeader> |
| 74 | <EmptyMedia variant=" |
| 75 | <Users /> |
| 76 | </EmptyMedia> |
| 77 | <EmptyTitle>No teams yet</EmptyTitle |
| 78 | <EmptyDescription> |
| 79 | Teams created in the organization's settings show up here. |
| 80 | </EmptyDescription> |
| 81 | </EmptyHeader> |
| 82 | </Empty> |
| 83 | ) : ( |
| 84 | <ItemGroup className=" |
| 85 | {teams.map(( |
| 86 | <Fragment key={ |
| 87 | {index > 0 |
| 88 | <Item> |
| 89 | <ItemMedia variant=" |
| 90 | <Users /> |
| 91 | </ItemMedia> |
| 92 | <ItemContent> |
| 93 | <ItemTitle>{team. |
| 94 | <ItemDescription> |
| 95 | {team.memberCount === |
| 96 | ? "1 member" |
| 97 | : `${team.memberCount |
| 98 | </ItemDescription> |
| 99 | </ItemContent> |
| 100 | <ItemActions> |
| 101 | <Select |
| 102 | value={team.role |
| 103 | disabled={busy} |
| 104 | onValueChange={(next) |
| 105 | next === NO_ACCESS |
| 106 | ? revoke.execute( |
| 107 | : grant.execute( |
| 108 | username, |
| 109 | slug, |
| 110 | teamId: team.id |
| 111 | role: next as |
| 112 | }) |
| 113 | } |
| 114 | > |
| 115 | <SelectTrigger |
| 116 | aria-label={`Access for ${team |
| 117 | className="w-36 capitalize" |
| 118 | > |
| 119 | <SelectValue /> |
| 120 | </SelectTrigger> |
| 121 | <SelectContent> |
| 122 | <SelectItem value={ |
| 123 | <SelectSeparator /> |
| 124 | {collaboratorRoles.map(( |
| 125 | <SelectItem |
| 126 | key={role} |
| 127 | value={role} |
| 128 | className="capitalize" |
| 129 | > |
| 130 | {role} |
| 131 | </SelectItem> |
| 132 | ))} |
| 133 | </SelectContent> |
| 134 | </Select> |
| 135 | </ItemActions> |
| 136 | </Item> |
| 137 | </Fragment> |
| 138 | ))} |
| 139 | </ItemGroup> |
| 140 | )} |
| 141 | </Card> |
| 142 | </section> |
| 143 | ); |
| 144 | } |