| 1 | "use client"; |
| 2 | |
| 3 | import { BookLock, Globe |
| 4 | import { useRouter } |
| 5 | import { useAction } |
| 6 | import { useState } |
| 7 | import { toast } |
| 8 | |
| 9 | import { |
| 10 | AlertDialog, |
| 11 | AlertDialogCancel, |
| 12 | AlertDialogContent, |
| 13 | AlertDialogDescription, |
| 14 | AlertDialogFooter, |
| 15 | AlertDialogHeader, |
| 16 | AlertDialogMedia, |
| 17 | AlertDialogTitle, |
| 18 | AlertDialogTrigger, |
| 19 | } from "@/components/ui/alert-dialog"; |
| 20 | import { Button } |
| 21 | import { Card } |
| 22 | import { |
| 23 | Field, |
| 24 | FieldDescription, |
| 25 | FieldError, |
| 26 | FieldLabel, |
| 27 | } from "@/components/ui/field"; |
| 28 | import { Input } |
| 29 | import { |
| 30 | Item, |
| 31 | ItemActions, |
| 32 | ItemContent, |
| 33 | ItemDescription, |
| 34 | ItemMedia, |
| 35 | ItemSeparator, |
| 36 | ItemTitle, |
| 37 | } from "@/components/ui/item"; |
| 38 | import { |
| 39 | Select, |
| 40 | SelectContent, |
| 41 | SelectItem, |
| 42 | SelectTrigger, |
| 43 | SelectValue, |
| 44 | } from "@/components/ui/select"; |
| 45 | import { Spinner } |
| 46 | import { Textarea } |
| 47 | |
| 48 | import { |
| 49 | deleteRepository, |
| 50 | transferRepository, |
| 51 | updateRepository, |
| 52 | } from "./actions"; |
| 53 | import { type UpdateRepositoryInput, |
| 54 | import { SettingCard } |
| 55 | |
| 56 | type Visibility |
| 57 | |
| 58 | type RepositoryProps |
| 59 | username: string |
| 60 | slug: string |
| 61 | }; |
| 62 | |
| 63 | export function RepositoryGeneralSettings({ |
| 64 | username, |
| 65 | slug, |
| 66 | name, |
| 67 | description, |
| 68 | visibility, |
| 69 | defaultBranch, |
| 70 | branches, |
| 71 | isAdmin, |
| 72 | transferTargets, |
| 73 | canTransfer, |
| 74 | }: RepositoryProps |
| 75 | name: string |
| 76 | description?: string |
| 77 | visibility: Visibility |
| 78 | defaultBranch: string |
| 79 | branches: string |
| 80 | /** Visibility and deletion are an admin's; a maintainer sees the rest. */ |
| 81 | isAdmin: boolean |
| 82 | /** Where the viewer may move the repository at once. */ |
| 83 | transferTargets: string |
| 84 | /** Transferring is the owner's alone. */ |
| 85 | canTransfer: boolean |
| 86 | }) { |
| 87 | return ( |
| 88 | <div className="flex flex-col gap-8 |
| 89 | <NameSetting username={ |
| 90 | <DescriptionSetting |
| 91 | username={username} |
| 92 | slug={slug} |
| 93 | description={description ?? |
| 94 | /> |
| 95 | {branches.length > |
| 96 | <DefaultBranchSetting |
| 97 | username={username} |
| 98 | slug={slug} |
| 99 | defaultBranch={defaultBranch} |
| 100 | branches={branches} |
| 101 | /> |
| 102 | )} |
| 103 | {isAdmin && ( |
| 104 | <> |
| 105 | <VisibilitySetting |
| 106 | username={username} |
| 107 | slug={slug} |
| 108 | visibility={visibility} |
| 109 | /> |
| 110 | <DangerZone |
| 111 | username={username} |
| 112 | slug={slug} |
| 113 | transferTargets={transferTargets} |
| 114 | canTransfer={canTransfer} |
| 115 | /> |
| 116 | </> |
| 117 | )} |
| 118 | </div> |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | /** Saves one change, then follows the repository to its new URL if a rename moved it. */ |
| 123 | function useSave({ username |
| 124 | const router = |
| 125 | |
| 126 | const action = |
| 127 | onSuccess: ({ data |
| 128 | toast.success(saved |
| 129 | if (data && |
| 130 | router.replace(`/ |
| 131 | } else { |
| 132 | router.refresh(); |
| 133 | } |
| 134 | }, |
| 135 | onError: ({ error |
| 136 | toast.error(error |
| 137 | }); |
| 138 | |
| 139 | return { |
| 140 | ...action, |
| 141 | save: (changes: |
| 142 | action.execute({ |
| 143 | }; |
| 144 | } |
| 145 | |
| 146 | function NameSetting({ |
| 147 | name, |
| 148 | ...repository |
| 149 | }: RepositoryProps |
| 150 | const [value, |
| 151 | const { save, |
| 152 | const parsed = |
| 153 | const error = |
| 154 | |
| 155 | return ( |
| 156 | <SettingCard |
| 157 | title="Repository name" |
| 158 | hint="Renaming changes the URL. Links and git remotes using the old one stop working." |
| 159 | onSubmit={() => |
| 160 | pending={isExecuting} |
| 161 | canSave={value !== |
| 162 | submitText="Rename" |
| 163 | > |
| 164 | <Field data-invalid={ |
| 165 | <FieldLabel htmlFor=" |
| 166 | Repository name |
| 167 | </FieldLabel> |
| 168 | <Input |
| 169 | id="settings-name" |
| 170 | autoComplete="off" |
| 171 | value={value} |
| 172 | aria-invalid={Boolean(error |
| 173 | onChange={(event) |
| 174 | className="max-w-md" |
| 175 | /> |
| 176 | {error && <FieldError |
| 177 | </Field> |
| 178 | </SettingCard> |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | function DescriptionSetting({ |
| 183 | description, |
| 184 | ...repository |
| 185 | }: RepositoryProps |
| 186 | const [value, |
| 187 | const { save, |
| 188 | const tooLong = |
| 189 | |
| 190 | return ( |
| 191 | <SettingCard |
| 192 | title="Description" |
| 193 | hint={`Shown under the repository name and in search. ${value |
| 194 | onSubmit={() => |
| 195 | pending={isExecuting} |
| 196 | canSave={value !== |
| 197 | > |
| 198 | <Field data-invalid={ |
| 199 | <FieldLabel htmlFor=" |
| 200 | Description |
| 201 | </FieldLabel> |
| 202 | <Textarea |
| 203 | id="settings-description" |
| 204 | rows={3} |
| 205 | placeholder="What this repository is for" |
| 206 | value={value} |
| 207 | aria-invalid={tooLong} |
| 208 | onChange={(event) |
| 209 | /> |
| 210 | {tooLong && ( |
| 211 | <FieldError>Descriptions are limited to 350 characters.</FieldError |
| 212 | )} |
| 213 | </Field> |
| 214 | </SettingCard> |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | function DefaultBranchSetting({ |
| 219 | defaultBranch, |
| 220 | branches, |
| 221 | ...repository |
| 222 | }: RepositoryProps |
| 223 | const [value, |
| 224 | const { save, |
| 225 | |
| 226 | return ( |
| 227 | <SettingCard |
| 228 | title="Default branch" |
| 229 | hint="The branch the repository opens on and clones check out." |
| 230 | onSubmit={() => |
| 231 | pending={isExecuting} |
| 232 | canSave={Boolean(value |
| 233 | submitText="Update" |
| 234 | > |
| 235 | <Field> |
| 236 | <FieldLabel htmlFor=" |
| 237 | Default branch |
| 238 | </FieldLabel> |
| 239 | <Select value={ |
| 240 | <SelectTrigger |
| 241 | id="settings-default-branch" |
| 242 | className="w-full sm:w-64" |
| 243 | > |
| 244 | <SelectValue placeholder=" |
| 245 | </SelectTrigger> |
| 246 | <SelectContent> |
| 247 | {branches.map(( |
| 248 | <SelectItem key={ |
| 249 | {branch} |
| 250 | </SelectItem> |
| 251 | ))} |
| 252 | </SelectContent> |
| 253 | </Select> |
| 254 | </Field> |
| 255 | </SettingCard> |
| 256 | ); |
| 257 | } |
| 258 | |
| 259 | const visibilityCopy = { |
| 260 | public: { |
| 261 | icon: Globe, |
| 262 | title: "Public", |
| 263 | description: "Anyone on the internet can see and clone this repository.", |
| 264 | }, |
| 265 | private: { |
| 266 | icon: BookLock, |
| 267 | title: "Private", |
| 268 | description: "Only you and the collaborators you invite can see it.", |
| 269 | }, |
| 270 | } satisfies Record<Visibility |
| 271 | |
| 272 | function VisibilitySetting({ |
| 273 | visibility, |
| 274 | ...repository |
| 275 | }: RepositoryProps |
| 276 | const next: |
| 277 | const { save, |
| 278 | repository, |
| 279 | `Repository is now ${next}`, |
| 280 | ); |
| 281 | const current = |
| 282 | |
| 283 | return ( |
| 284 | <section> |
| 285 | <h2 className="mb-3 text-sm font-semibold |
| 286 | <Card className=" |
| 287 | <Item> |
| 288 | <ItemMedia variant=" |
| 289 | <current.icon /> |
| 290 | </ItemMedia> |
| 291 | <ItemContent> |
| 292 | <ItemTitle>{current. |
| 293 | <ItemDescription>{current. |
| 294 | </ItemContent> |
| 295 | <ItemActions> |
| 296 | <AlertDialog> |
| 297 | <AlertDialogTrigger asChild> |
| 298 | <Button size=" |
| 299 | {isExecuting && <Spinner |
| 300 | Make {next} |
| 301 | </Button> |
| 302 | </AlertDialogTrigger> |
| 303 | <AlertDialogContent> |
| 304 | <AlertDialogHeader> |
| 305 | <AlertDialogTitle> |
| 306 | Make {repository.username}/ |
| 307 | </AlertDialogTitle> |
| 308 | <AlertDialogDescription> |
| 309 | {next === "public |
| 310 | ? "Its code, issues and pull requests become visible to anyone, and its code becomes searchable." |
| 311 | : "Only you and its collaborators will be able to see it."} |
| 312 | </AlertDialogDescription> |
| 313 | </AlertDialogHeader> |
| 314 | <AlertDialogFooter> |
| 315 | <AlertDialogCancel>Cancel</AlertDialogCancel |
| 316 | <Button onClick={ |
| 317 | Make {next} |
| 318 | </Button> |
| 319 | </AlertDialogFooter> |
| 320 | </AlertDialogContent> |
| 321 | </AlertDialog> |
| 322 | </ItemActions> |
| 323 | </Item> |
| 324 | </Card> |
| 325 | </section> |
| 326 | ); |
| 327 | } |
| 328 | |
| 329 | function DangerZone({ |
| 330 | transferTargets, |
| 331 | canTransfer, |
| 332 | ...repository |
| 333 | }: RepositoryProps |
| 334 | return ( |
| 335 | <section> |
| 336 | <h2 className="mb-3 text-sm font-semibold text-destructive |
| 337 | Danger zone |
| 338 | </h2> |
| 339 | <Card className=" |
| 340 | {canTransfer && ( |
| 341 | <> |
| 342 | <Item> |
| 343 | <ItemContent> |
| 344 | <ItemTitle>Transfer ownership</ItemTitle |
| 345 | <ItemDescription> |
| 346 | Move it to your account or an organization you administer. The |
| 347 | URL changes, and git remotes need updating. |
| 348 | </ItemDescription> |
| 349 | </ItemContent> |
| 350 | <ItemActions> |
| 351 | <TransferRepositoryDialog |
| 352 | {...repository} |
| 353 | suggestions={transferTargets} |
| 354 | /> |
| 355 | </ItemActions> |
| 356 | </Item> |
| 357 | <ItemSeparator className=" |
| 358 | </> |
| 359 | )} |
| 360 | <Item> |
| 361 | <ItemContent> |
| 362 | <ItemTitle>Delete this repository</ItemTitle |
| 363 | <ItemDescription> |
| 364 | Its code, issues, pull requests and stars are gone for good. Forks |
| 365 | are kept. |
| 366 | </ItemDescription> |
| 367 | </ItemContent> |
| 368 | <ItemActions> |
| 369 | <DeleteRepositoryDialog {...repository |
| 370 | </ItemActions> |
| 371 | </Item> |
| 372 | </Card> |
| 373 | </section> |
| 374 | ); |
| 375 | } |
| 376 | |
| 377 | /** `suggestions` are where it moves at once: the viewer's own account and organizations they administer. Any other user or organization can be typed, and accepts it first. */ |
| 378 | function TransferRepositoryDialog({ |
| 379 | username, |
| 380 | slug, |
| 381 | suggestions, |
| 382 | }: RepositoryProps |
| 383 | const [open, |
| 384 | const [target, |
| 385 | const [confirmation, |
| 386 | const { execute, |
| 387 | onSuccess: ({ data |
| 388 | if (!data?.pending |
| 389 | toast.success(`Transfer requested; it moves once |
| 390 | setOpen(false); |
| 391 | }, |
| 392 | }); |
| 393 | const fullName = |
| 394 | const immediate = |
| 395 | |
| 396 | return ( |
| 397 | <AlertDialog |
| 398 | open={open} |
| 399 | onOpenChange={(next) |
| 400 | setOpen(next); |
| 401 | setConfirmation(""); |
| 402 | }} |
| 403 | > |
| 404 | <AlertDialogTrigger asChild> |
| 405 | <Button variant=" |
| 406 | Transfer |
| 407 | </Button> |
| 408 | </AlertDialogTrigger> |
| 409 | <AlertDialogContent> |
| 410 | <AlertDialogHeader> |
| 411 | <AlertDialogTitle>Transfer {fullName |
| 412 | <AlertDialogDescription> |
| 413 | Its issues, pull requests, stars and collaborators move with it, and |
| 414 | the old URL keeps redirecting. |
| 415 | </AlertDialogDescription> |
| 416 | </AlertDialogHeader> |
| 417 | |
| 418 | <Field> |
| 419 | <FieldLabel htmlFor=" |
| 420 | <Input |
| 421 | id="transfer-target" |
| 422 | list="transfer-suggestions" |
| 423 | autoComplete="off" |
| 424 | placeholder="A username or organization" |
| 425 | value={target} |
| 426 | onChange={(event) |
| 427 | /> |
| 428 | <datalist id="transfer-suggestions |
| 429 | {suggestions.map(( |
| 430 | <option key={owner |
| 431 | ))} |
| 432 | </datalist> |
| 433 | <FieldDescription> |
| 434 | {immediate |
| 435 | ? "It moves right away." |
| 436 | : "They have to accept it before it moves."} |
| 437 | </FieldDescription> |
| 438 | </Field> |
| 439 | <Field> |
| 440 | <FieldLabel htmlFor=" |
| 441 | Type <span className="font-mono |
| 442 | </FieldLabel> |
| 443 | <Input |
| 444 | id="transfer-confirmation" |
| 445 | autoComplete="off" |
| 446 | value={confirmation} |
| 447 | onChange={(event) |
| 448 | /> |
| 449 | {result.serverError && |
| 450 | </Field> |
| 451 | |
| 452 | <AlertDialogFooter> |
| 453 | <AlertDialogCancel disabled={ |
| 454 | <Button |
| 455 | type="button" |
| 456 | disabled={ |
| 457 | isExecuting || !target |
| 458 | } |
| 459 | onClick={() => |
| 460 | > |
| 461 | {isExecuting && <Spinner |
| 462 | {immediate ? `Transfer to |
| 463 | </Button> |
| 464 | </AlertDialogFooter> |
| 465 | </AlertDialogContent> |
| 466 | </AlertDialog> |
| 467 | ); |
| 468 | } |
| 469 | |
| 470 | function DeleteRepositoryDialog({ username |
| 471 | const [confirmation, |
| 472 | const { execute, |
| 473 | const fullName = |
| 474 | |
| 475 | return ( |
| 476 | <AlertDialog onOpenChange={ |
| 477 | <AlertDialogTrigger asChild> |
| 478 | <Button variant=" |
| 479 | Delete |
| 480 | </Button> |
| 481 | </AlertDialogTrigger> |
| 482 | <AlertDialogContent> |
| 483 | <AlertDialogHeader> |
| 484 | <AlertDialogMedia> |
| 485 | <Trash2 /> |
| 486 | </AlertDialogMedia> |
| 487 | <AlertDialogTitle>Delete {fullName |
| 488 | <AlertDialogDescription> |
| 489 | This cannot be undone. The code, issues, pull requests and stars are |
| 490 | removed. |
| 491 | </AlertDialogDescription> |
| 492 | </AlertDialogHeader> |
| 493 | |
| 494 | <Field> |
| 495 | <FieldLabel htmlFor=" |
| 496 | Type <span className="font-mono |
| 497 | </FieldLabel> |
| 498 | <Input |
| 499 | id="delete-confirmation" |
| 500 | autoComplete="off" |
| 501 | value={confirmation} |
| 502 | onChange={(event) |
| 503 | /> |
| 504 | {result.serverError && |
| 505 | </Field> |
| 506 | |
| 507 | <AlertDialogFooter> |
| 508 | <AlertDialogCancel disabled={ |
| 509 | <Button |
| 510 | type="button" |
| 511 | variant="destructive" |
| 512 | disabled={isExecuting || |
| 513 | onClick={() => |
| 514 | > |
| 515 | {isExecuting && <Spinner |
| 516 | Delete repository |
| 517 | </Button> |
| 518 | </AlertDialogFooter> |
| 519 | </AlertDialogContent> |
| 520 | </AlertDialog> |
| 521 | ); |
| 522 | } |