| 1 | "use client"; |
| 2 | |
| 3 | import { useAuth } from |
| 4 | import { Trash2, Upload |
| 5 | import { useRouter } |
| 6 | import { type ChangeEvent, |
| 7 | import { toast } |
| 8 | |
| 9 | import { SettingCard } |
| 10 | import { |
| 11 | AlertDialog, |
| 12 | AlertDialogCancel, |
| 13 | AlertDialogContent, |
| 14 | AlertDialogDescription, |
| 15 | AlertDialogFooter, |
| 16 | AlertDialogHeader, |
| 17 | AlertDialogMedia, |
| 18 | AlertDialogTitle, |
| 19 | AlertDialogTrigger, |
| 20 | } from "@/components/ui/alert-dialog"; |
| 21 | import { Avatar, AvatarFallback |
| 22 | import { Button } |
| 23 | import { Card } |
| 24 | import { Field, FieldLabel |
| 25 | import { Input } |
| 26 | import { |
| 27 | Item, |
| 28 | ItemActions, |
| 29 | ItemContent, |
| 30 | ItemDescription, |
| 31 | ItemMedia, |
| 32 | ItemTitle, |
| 33 | } from "@/components/ui/item"; |
| 34 | import { Spinner } |
| 35 | import { apiClient, apiErrorMessage |
| 36 | import type { components |
| 37 | import { authClient } |
| 38 | import { deleteImage, putImage |
| 39 | |
| 40 | import { |
| 41 | OrganizationPrivileges, |
| 42 | OrganizationProfileSettings, |
| 43 | PinnedRepositoriesSetting, |
| 44 | } from "./organization-policies"; |
| 45 | import { useOrganization } |
| 46 | |
| 47 | type OrganizationSettings |
| 48 | |
| 49 | export function OrganizationGeneral({ |
| 50 | slug, |
| 51 | name, |
| 52 | isOwner, |
| 53 | settings, |
| 54 | repositories, |
| 55 | pinned, |
| 56 | }: { |
| 57 | slug: string |
| 58 | name: string |
| 59 | /** Deleting the organization is its owners' alone. */ |
| 60 | isOwner: boolean |
| 61 | settings: OrganizationSettings |
| 62 | /** Slugs of every repository it owns, which have to go before it can. */ |
| 63 | repositories: string |
| 64 | pinned: string |
| 65 | }) { |
| 66 | const router = |
| 67 | const { organization, |
| 68 | const [displayName, |
| 69 | const [newSlug, |
| 70 | const organizationId = |
| 71 | |
| 72 | return ( |
| 73 | <div className="flex flex-col gap-8 |
| 74 | <LogoSetting slug={ |
| 75 | |
| 76 | <SettingCard |
| 77 | title="Display name" |
| 78 | hint="Shown on the organization's profile." |
| 79 | onSubmit={() => |
| 80 | change( |
| 81 | () => |
| 82 | authClient.organization.update |
| 83 | organizationId, |
| 84 | data: { name: displayName |
| 85 | }), |
| 86 | "Name saved", |
| 87 | ).then((ok |
| 88 | } |
| 89 | pending={busy} |
| 90 | canSave={Boolean(organization |
| 91 | > |
| 92 | <Field> |
| 93 | <FieldLabel htmlFor=" |
| 94 | Display name |
| 95 | </FieldLabel> |
| 96 | <Input |
| 97 | id="organization-name" |
| 98 | value={displayName} |
| 99 | onChange={(event) |
| 100 | className="max-w-md" |
| 101 | /> |
| 102 | </Field> |
| 103 | </SettingCard> |
| 104 | |
| 105 | <SettingCard |
| 106 | title="Organization name" |
| 107 | hint="The name in its URLs. Renaming changes the address of every repository it owns, and git remotes need updating." |
| 108 | onSubmit={() => |
| 109 | change( |
| 110 | () => |
| 111 | authClient.organization.update |
| 112 | organizationId, |
| 113 | data: { slug: newSlug |
| 114 | }), |
| 115 | "Organization renamed", |
| 116 | ).then((ok |
| 117 | } |
| 118 | pending={busy} |
| 119 | canSave={ |
| 120 | Boolean(organization) && |
| 121 | Boolean(newSlug.trim |
| 122 | newSlug.trim() !== |
| 123 | } |
| 124 | submitText="Rename" |
| 125 | > |
| 126 | <Field> |
| 127 | <FieldLabel htmlFor=" |
| 128 | Organization name |
| 129 | </FieldLabel> |
| 130 | <Input |
| 131 | id="organization-slug" |
| 132 | autoComplete="off" |
| 133 | value={newSlug} |
| 134 | onChange={(event) |
| 135 | className="max-w-md" |
| 136 | /> |
| 137 | </Field> |
| 138 | </SettingCard> |
| 139 | |
| 140 | <OrganizationProfileSettings slug={ |
| 141 | <OrganizationPrivileges slug={ |
| 142 | <PinnedRepositoriesSetting |
| 143 | slug={slug} |
| 144 | repositories={repositories} |
| 145 | pinned={pinned} |
| 146 | /> |
| 147 | |
| 148 | {isOwner && ( |
| 149 | <section> |
| 150 | <h2 className="mb-3 text-sm font-semibold text-destructive |
| 151 | Danger zone |
| 152 | </h2> |
| 153 | <Card className=" |
| 154 | <Item> |
| 155 | <ItemContent> |
| 156 | <ItemTitle>Delete this organization</ItemTitle |
| 157 | <ItemDescription> |
| 158 | Its members, teams and invitations go with it, and so do its |
| 159 | repositories unless you transfer them first. |
| 160 | </ItemDescription> |
| 161 | </ItemContent> |
| 162 | <ItemActions> |
| 163 | <DeleteOrganizationDialog |
| 164 | slug={slug} |
| 165 | repositories={repositories} |
| 166 | disabled={!organization |
| 167 | onDelete={async () |
| 168 | // Each repository goes through its own delete, which purges what it stored; the organization refuses to go while any remain. |
| 169 | for (const repository |
| 170 | const { error |
| 171 | "/api/repositories/{username}/{slug}", |
| 172 | { |
| 173 | params: { |
| 174 | path: { username: slug |
| 175 | }, |
| 176 | }, |
| 177 | ); |
| 178 | if (error) { |
| 179 | toast.error(`${ |
| 180 | router.refresh(); |
| 181 | return; |
| 182 | } |
| 183 | } |
| 184 | const ok = |
| 185 | () => authClient. |
| 186 | "Organization deleted", |
| 187 | ); |
| 188 | if (ok) router. |
| 189 | }} |
| 190 | /> |
| 191 | </ItemActions> |
| 192 | </Item> |
| 193 | </Card> |
| 194 | </section> |
| 195 | )} |
| 196 | </div> |
| 197 | ); |
| 198 | } |
| 199 | |
| 200 | function DeleteOrganizationDialog({ |
| 201 | slug, |
| 202 | repositories, |
| 203 | disabled, |
| 204 | onDelete, |
| 205 | }: { |
| 206 | slug: string |
| 207 | repositories: string |
| 208 | disabled: boolean |
| 209 | onDelete: () |
| 210 | }) { |
| 211 | const [confirmation, |
| 212 | const [deleting, |
| 213 | |
| 214 | return ( |
| 215 | <AlertDialog onOpenChange={ |
| 216 | <AlertDialogTrigger asChild> |
| 217 | <Button variant=" |
| 218 | Delete |
| 219 | </Button> |
| 220 | </AlertDialogTrigger> |
| 221 | <AlertDialogContent> |
| 222 | <AlertDialogHeader> |
| 223 | <AlertDialogMedia> |
| 224 | <Trash2 /> |
| 225 | </AlertDialogMedia> |
| 226 | <AlertDialogTitle>Delete {slug |
| 227 | <AlertDialogDescription> |
| 228 | This cannot be undone.{" "} |
| 229 | {repositories.length > |
| 230 | ? `Its ${repositories. |
| 231 | : "It owns no repositories."} |
| 232 | </AlertDialogDescription> |
| 233 | </AlertDialogHeader> |
| 234 | {repositories.length > |
| 235 | <ul className="max-h-40 overflow-y-auto rounded-md border px-3 py-2 font-mono text-xs |
| 236 | {repositories.map(( |
| 237 | <li key={repository |
| 238 | {slug}/{repository |
| 239 | </li> |
| 240 | ))} |
| 241 | </ul> |
| 242 | )} |
| 243 | <Field> |
| 244 | <FieldLabel htmlFor=" |
| 245 | Type <span className="font-mono |
| 246 | </FieldLabel> |
| 247 | <Input |
| 248 | id="delete-organization-confirmation" |
| 249 | autoComplete="off" |
| 250 | value={confirmation} |
| 251 | onChange={(event) |
| 252 | /> |
| 253 | </Field> |
| 254 | <AlertDialogFooter> |
| 255 | <AlertDialogCancel disabled={ |
| 256 | <Button |
| 257 | variant="destructive" |
| 258 | disabled={deleting || |
| 259 | onClick={() => |
| 260 | setDeleting(true); |
| 261 | onDelete().finally(() |
| 262 | }} |
| 263 | > |
| 264 | {deleting && <Spinner |
| 265 | Delete organization |
| 266 | </Button> |
| 267 | </AlertDialogFooter> |
| 268 | </AlertDialogContent> |
| 269 | </AlertDialog> |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | /** Upload or remove the logo. Uploads are resized to the same square as account avatars before they are sent. */ |
| 274 | function LogoSetting({ slug |
| 275 | const router = |
| 276 | const { avatar |
| 277 | const { organization, |
| 278 | const fileInput = |
| 279 | const path = |
| 280 | |
| 281 | const run = |
| 282 | change(async () |
| 283 | try { |
| 284 | await action(); |
| 285 | return { error: |
| 286 | } catch (error) |
| 287 | return { |
| 288 | error: { |
| 289 | message: error instanceof |
| 290 | }, |
| 291 | }; |
| 292 | } |
| 293 | }, done).then |
| 294 | |
| 295 | async function upload |
| 296 | const file = |
| 297 | event.target.value |
| 298 | if (!file) |
| 299 | |
| 300 | const resized = |
| 301 | (await avatar.resize |
| 302 | await run(() |
| 303 | } |
| 304 | |
| 305 | return ( |
| 306 | <section> |
| 307 | <h2 className="mb-3 text-sm font-semibold |
| 308 | <Card className=" |
| 309 | <Item> |
| 310 | <ItemMedia> |
| 311 | <Avatar className=" |
| 312 | <AvatarImage src={ |
| 313 | <AvatarFallback className=" |
| 314 | {slug.slice(0 |
| 315 | </AvatarFallback> |
| 316 | </Avatar> |
| 317 | </ItemMedia> |
| 318 | <ItemContent> |
| 319 | <ItemDescription> |
| 320 | Shown on the organization's profile and next to its |
| 321 | repositories. PNG or JPEG. |
| 322 | </ItemDescription> |
| 323 | </ItemContent> |
| 324 | <ItemActions> |
| 325 | <input |
| 326 | ref={fileInput} |
| 327 | type="file" |
| 328 | accept="image/png,image/jpeg" |
| 329 | className="hidden" |
| 330 | onChange={upload} |
| 331 | /> |
| 332 | <Button |
| 333 | size="sm" |
| 334 | variant="outline" |
| 335 | disabled={!organization |
| 336 | onClick={() => |
| 337 | > |
| 338 | {busy ? <Spinner |
| 339 | Upload |
| 340 | </Button> |
| 341 | {organization?.logo && |
| 342 | <Button |
| 343 | size="sm" |
| 344 | variant="ghost" |
| 345 | disabled={busy} |
| 346 | onClick={() => |
| 347 | > |
| 348 | Remove |
| 349 | </Button> |
| 350 | )} |
| 351 | </ItemActions> |
| 352 | </Item> |
| 353 | </Card> |
| 354 | </section> |
| 355 | ); |
| 356 | } |