| 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: { |
| 33 | usedBytes: number |
| 34 | quotaBytes: number |
| 35 | maxAssetBytes: number |
| 36 | } | null |
| 37 | problem: string |
| 38 | disabled: boolean |
| 39 | }) { |
| 40 | const router = |
| 41 | const remove = |
| 42 | onSuccess: () => |
| 43 | }); |
| 44 | |
| 45 | return ( |
| 46 | <div className="flex flex-col gap-2 |
| 47 | {(existing.length > |
| 48 | <ul className="divide-y rounded-lg border |
| 49 | {existing.map(( |
| 50 | <li |
| 51 | key={asset.id |
| 52 | className="flex items-center gap-3 px-3 py-2 text-sm" |
| 53 | > |
| 54 | <Package className=" |
| 55 | <span className="min-w-0 flex-1 truncate |
| 56 | <span className="text-xs text-muted-foreground tabular-nums |
| 57 | {formatBytes(asset.size |
| 58 | </span> |
| 59 | <Button |
| 60 | type="button" |
| 61 | variant="ghost" |
| 62 | size="icon-sm" |
| 63 | aria-label={`Delete ${asset |
| 64 | disabled={remove.isExecuting |
| 65 | onClick={() => |
| 66 | remove.execute({ |
| 67 | } |
| 68 | > |
| 69 | {remove.isExecuting && |
| 70 | <Spinner /> |
| 71 | ) : ( |
| 72 | <X /> |
| 73 | )} |
| 74 | </Button> |
| 75 | </li> |
| 76 | ))} |
| 77 | {files.map(( |
| 78 | <li |
| 79 | key={`${file |
| 80 | className="flex items-center gap-3 px-3 py-2 text-sm" |
| 81 | > |
| 82 | <Paperclip className=" |
| 83 | <span className="min-w-0 flex-1 truncate |
| 84 | <span className="text-xs text-muted-foreground tabular-nums |
| 85 | {formatBytes(file.size |
| 86 | </span> |
| 87 | <Button |
| 88 | type="button" |
| 89 | variant="ghost" |
| 90 | size="icon-sm" |
| 91 | aria-label={`Remove ${file |
| 92 | onClick={() => |
| 93 | onFilesChange(files.filter |
| 94 | } |
| 95 | > |
| 96 | <X /> |
| 97 | </Button> |
| 98 | </li> |
| 99 | ))} |
| 100 | </ul> |
| 101 | )} |
| 102 | |
| 103 | <Dropzone |
| 104 | maxFiles={0} |
| 105 | disabled={disabled} |
| 106 | onDrop={(dropped) |
| 107 | > |
| 108 | <DropzoneEmptyState /> |
| 109 | </Dropzone> |
| 110 | {storage && ( |
| 111 | <FieldDescription> |
| 112 | Up to {formatBytes(storage.maxAssetBytes |
| 113 | {storage.quotaBytes !== |
| 114 | ` · ${formatBytes(storage |
| 115 | </FieldDescription> |
| 116 | )} |
| 117 | |
| 118 | {problem && <FieldError |
| 119 | {remove.result.serverError |
| 120 | <FieldError>{remove. |
| 121 | )} |
| 122 | </div> |
| 123 | ); |
| 124 | } |