| 1 | "use client"; |
| 2 | |
| 3 | import { ProfileAvatar } from |
| 4 | import { useMutation, useQuery |
| 5 | import Link from "next/link |
| 6 | import { Fragment, useState |
| 7 | import { toast } |
| 8 | |
| 9 | import { Badge } |
| 10 | import { Button } |
| 11 | import { |
| 12 | Item, |
| 13 | ItemActions, |
| 14 | ItemContent, |
| 15 | ItemGroup, |
| 16 | ItemMedia, |
| 17 | ItemSeparator, |
| 18 | ItemTitle, |
| 19 | } from "@/components/ui/item"; |
| 20 | import { |
| 21 | Select, |
| 22 | SelectContent, |
| 23 | SelectItem, |
| 24 | SelectTrigger, |
| 25 | SelectValue, |
| 26 | } from "@/components/ui/select"; |
| 27 | import { apiClient, unwrap |
| 28 | |
| 29 | /** One team's details, shared by the team page and the organization's team settings. */ |
| 30 | export function useTeam(organization |
| 31 | return useQuery({ |
| 32 | queryKey: ["organization-team", |
| 33 | queryFn: async () |
| 34 | const data = |
| 35 | apiClient.GET("/api/organizations/{slug}/teams/{team} |
| 36 | params: { path: { |
| 37 | }), |
| 38 | ); |
| 39 | return data; |
| 40 | }, |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | /** The organization's members, for picking whom to add. Only members may list them all. */ |
| 45 | function useOrganizationMembers(organization: |
| 46 | return useQuery({ |
| 47 | queryKey: ["organization-members", |
| 48 | queryFn: async () |
| 49 | const data = |
| 50 | apiClient.GET("/api/organizations/{slug} |
| 51 | params: { path: { |
| 52 | }), |
| 53 | ); |
| 54 | return data.members |
| 55 | }, |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | /** A team's members. Its maintainers and the organization's admins add and remove them; admins also appoint maintainers. */ |
| 60 | export function TeamMembers({ |
| 61 | organization, |
| 62 | team, |
| 63 | isAdmin, |
| 64 | }: { |
| 65 | organization: string |
| 66 | team: string |
| 67 | isAdmin: boolean |
| 68 | }) { |
| 69 | const queryClient = |
| 70 | const { data |
| 71 | const { data: |
| 72 | const [adding, |
| 73 | |
| 74 | // Written out per endpoint so openapi-fetch can type each path. |
| 75 | const send = |
| 76 | mutationFn: async ( |
| 77 | kind, |
| 78 | username, |
| 79 | remove, |
| 80 | }: { |
| 81 | kind: "members |
| 82 | username: string |
| 83 | remove: boolean |
| 84 | done: string |
| 85 | }) => { |
| 86 | const params = |
| 87 | await unwrap( |
| 88 | kind === "members" |
| 89 | ? remove |
| 90 | ? apiClient.DELETE( |
| 91 | "/api/organizations/{slug}/teams/{team}/members/{username}", |
| 92 | { params }, |
| 93 | ) |
| 94 | : apiClient.PUT( |
| 95 | "/api/organizations/{slug}/teams/{team}/members/{username}", |
| 96 | { params }, |
| 97 | ) |
| 98 | : remove |
| 99 | ? apiClient.DELETE( |
| 100 | "/api/organizations/{slug}/teams/{team}/maintainers/{username}", |
| 101 | { params }, |
| 102 | ) |
| 103 | : apiClient.PUT( |
| 104 | "/api/organizations/{slug}/teams/{team}/maintainers/{username}", |
| 105 | { params }, |
| 106 | ), |
| 107 | ); |
| 108 | }, |
| 109 | onSuccess: async ( |
| 110 | toast.success(done |
| 111 | setAdding(""); |
| 112 | await queryClient.invalidateQueries |
| 113 | queryKey: ["organization-team", |
| 114 | }); |
| 115 | await queryClient.invalidateQueries |
| 116 | queryKey: ["organization-teams", |
| 117 | }); |
| 118 | }, |
| 119 | onError: (error: |
| 120 | }); |
| 121 | |
| 122 | if (!data) |
| 123 | const onTeam = |
| 124 | const candidates = |
| 125 | |
| 126 | return ( |
| 127 | <div className="flex flex-col |
| 128 | {data.members.length |
| 129 | <p className="px-4 py-3 text-sm text-muted-foreground |
| 130 | Nobody is on this team yet. |
| 131 | </p> |
| 132 | ) : ( |
| 133 | <ItemGroup className=" |
| 134 | {data.members.map |
| 135 | <Fragment key={ |
| 136 | {index > 0 |
| 137 | <Item size=" |
| 138 | <ItemMedia> |
| 139 | <ProfileAvatar |
| 140 | name={member.username |
| 141 | image={member.image |
| 142 | className="size-7" |
| 143 | /> |
| 144 | </ItemMedia> |
| 145 | <ItemContent className=" |
| 146 | <ItemTitle> |
| 147 | <Link |
| 148 | href={`/${member |
| 149 | className="hover:underline" |
| 150 | > |
| 151 | {member.name} |
| 152 | </Link> |
| 153 | {member.maintainer && |
| 154 | <Badge variant=" |
| 155 | )} |
| 156 | </ItemTitle> |
| 157 | </ItemContent> |
| 158 | {data.viewerCanManage && |
| 159 | <ItemActions> |
| 160 | {isAdmin && ( |
| 161 | <Button |
| 162 | size="sm" |
| 163 | variant="ghost" |
| 164 | disabled={send.isPending |
| 165 | onClick={() => |
| 166 | send.mutate({ |
| 167 | kind: "maintainers", |
| 168 | username: member.username |
| 169 | remove: member.maintainer |
| 170 | done: member.maintainer |
| 171 | ? `${member.name |
| 172 | : `${member.name |
| 173 | }) |
| 174 | } |
| 175 | > |
| 176 | {member.maintainer |
| 177 | ? "Remove maintainer" |
| 178 | : "Make maintainer"} |
| 179 | </Button> |
| 180 | )} |
| 181 | <Button |
| 182 | size="sm" |
| 183 | variant="ghost" |
| 184 | disabled={send.isPending |
| 185 | onClick={() => |
| 186 | send.mutate({ |
| 187 | kind: "members", |
| 188 | username: member.username |
| 189 | remove: true, |
| 190 | done: `${member. |
| 191 | }) |
| 192 | } |
| 193 | > |
| 194 | Remove |
| 195 | </Button> |
| 196 | </ItemActions> |
| 197 | )} |
| 198 | </Item> |
| 199 | </Fragment> |
| 200 | ))} |
| 201 | </ItemGroup> |
| 202 | )} |
| 203 | |
| 204 | {data.viewerCanManage && |
| 205 | <div className="flex flex-col gap-2 border-t p-4 sm:flex-row |
| 206 | <Select value={ |
| 207 | <SelectTrigger |
| 208 | aria-label={`Member to add to ${data |
| 209 | className="w-full sm:flex-1" |
| 210 | > |
| 211 | <SelectValue placeholder=" |
| 212 | </SelectTrigger> |
| 213 | <SelectContent> |
| 214 | {candidates.map(( |
| 215 | <SelectItem key={ |
| 216 | {member.name} ( |
| 217 | </SelectItem> |
| 218 | ))} |
| 219 | </SelectContent> |
| 220 | </Select> |
| 221 | <Button |
| 222 | size="sm" |
| 223 | variant="outline" |
| 224 | disabled={send.isPending |
| 225 | onClick={() => |
| 226 | send.mutate({ |
| 227 | kind: "members", |
| 228 | username: adding, |
| 229 | remove: false, |
| 230 | done: `Added to ${data |
| 231 | }) |
| 232 | } |
| 233 | > |
| 234 | Add |
| 235 | </Button> |
| 236 | </div> |
| 237 | )} |
| 238 | </div> |
| 239 | ); |
| 240 | } |