| 1 | "use client"; |
| 2 | |
| 3 | import { Package, Paperclip |
| 4 | import { useRouter } |
| 5 | import { useAction } |
| 6 | |
| 7 | import { Dropzone, DropzoneEmptyState |
| 8 | import { Button } |
| 9 | import { FieldDescription, FieldError |
| 10 | import { Spinner } |
| 11 | import { formatBytes } |
| 12 | import type { ReleaseAsset, |
| 13 | |
| 14 | import { deleteReleaseAsset } |
| 15 | |
| 16 | /** The files a release form will attach: the ones already stored, which delete at once, and the ones picked, which upload when the form is saved. */ |
| 17 | export function ReleaseAssetPicker({ |
| 18 | username, |
| 19 | repo, |
| 20 | existing, |
| 21 | files, |
| 22 | onFilesChange, |
| 23 | storage, |
| 24 | problem, |
| 25 | disabled, |
| 26 | }: { |
| 27 | username: string |
| 28 | repo: string |
| 29 | existing: ReleaseAsset |
| 30 | files: File |
| 31 | onFilesChange: (files |
| 32 | storage: StorageUsage |
| 33 | problem: string |
| 34 | disabled: boolean |
| 35 | }) { |
| 36 | const router = |
| 37 | const remove = |
| 38 | onSuccess: () => |
| 39 | }); |
| 40 | |
| 41 | return ( |
| 42 | <div className="flex flex-col gap-2 |
| 43 | {(existing.length > |
| 44 | <ul className="divide-y rounded-lg border |
| 45 | {existing.map(( |
| 46 | <li |
| 47 | key={asset.id |
| 48 | className="flex items-center gap-3 px-3 py-2 text-sm" |
| 49 | > |
| 50 | <Package className=" |
| 51 | <span className="min-w-0 flex-1 truncate |
| 52 | <span className="text-xs text-muted-foreground tabular-nums |
| 53 | {formatBytes(asset.size |
| 54 | </span> |
| 55 | <Button |
| 56 | type="button" |
| 57 | variant="ghost" |
| 58 | size="icon-sm" |
| 59 | aria-label={`Delete ${asset |
| 60 | disabled={remove.isExecuting |
| 61 | onClick={() => |
| 62 | remove.execute({ |
| 63 | } |
| 64 | > |
| 65 | {remove.isExecuting && |
| 66 | <Spinner /> |
| 67 | ) : ( |
| 68 | <X /> |
| 69 | )} |
| 70 | </Button> |
| 71 | </li> |
| 72 | ))} |
| 73 | {files.map(( |
| 74 | <li |
| 75 | key={`${file |
| 76 | className="flex items-center gap-3 px-3 py-2 text-sm" |
| 77 | > |
| 78 | <Paperclip className=" |
| 79 | <span className="min-w-0 flex-1 truncate |
| 80 | <span className="text-xs text-muted-foreground tabular-nums |
| 81 | {formatBytes(file.size |
| 82 | </span> |
| 83 | <Button |
| 84 | type="button" |
| 85 | variant="ghost" |
| 86 | size="icon-sm" |
| 87 | aria-label={`Remove ${file |
| 88 | onClick={() => |
| 89 | onFilesChange(files.filter |
| 90 | } |
| 91 | > |
| 92 | <X /> |
| 93 | </Button> |
| 94 | </li> |
| 95 | ))} |
| 96 | </ul> |
| 97 | )} |
| 98 | |
| 99 | <Dropzone |
| 100 | maxFiles={0} |
| 101 | disabled={disabled} |
| 102 | onDrop={(dropped) |
| 103 | > |
| 104 | <DropzoneEmptyState /> |
| 105 | </Dropzone> |
| 106 | {storage && ( |
| 107 | <FieldDescription> |
| 108 | Up to {formatBytes(storage.maxAssetBytes |
| 109 | {storage.quotaBytes !== |
| 110 | ` · ${formatBytes(storage |
| 111 | </FieldDescription> |
| 112 | )} |
| 113 | |
| 114 | {problem && <FieldError |
| 115 | {remove.result.serverError |
| 116 | <FieldError>{remove. |
| 117 | )} |
| 118 | </div> |
| 119 | ); |
| 120 | } |