| 1 | "use client"; |
| 2 | |
| 3 | import { zodResolver } from |
| 4 | import Link from "next/link |
| 5 | import { useRouter } |
| 6 | import { useAction } |
| 7 | import { useState } |
| 8 | import { Controller, useForm |
| 9 | import { toast } |
| 10 | |
| 11 | import { BranchSelect } |
| 12 | import { Button } |
| 13 | import { Checkbox } |
| 14 | import { |
| 15 | Field, |
| 16 | FieldDescription, |
| 17 | FieldError, |
| 18 | FieldGroup, |
| 19 | FieldLabel, |
| 20 | } from "@/components/ui/field"; |
| 21 | import { Input } |
| 22 | import { MarkdownEditor } |
| 23 | import { Skeleton } |
| 24 | import { Spinner } |
| 25 | import type { Release, |
| 26 | |
| 27 | import { createRelease, updateRelease |
| 28 | import { |
| 29 | assetProblem, |
| 30 | type ReleaseInput, |
| 31 | releasePath, |
| 32 | releaseSchema, |
| 33 | } from "./common"; |
| 34 | import { ReleaseAssetPicker } |
| 35 | import { uploadReleaseAsset } |
| 36 | |
| 37 | /** Creates a release, or edits `release` when given. The tag of an existing release is fixed. Picked files upload once the release is saved. */ |
| 38 | export function ReleaseForm({ |
| 39 | username, |
| 40 | repo, |
| 41 | branches, |
| 42 | defaultBranch, |
| 43 | release, |
| 44 | isFork, |
| 45 | storage, |
| 46 | }: { |
| 47 | username: string |
| 48 | repo: string |
| 49 | branches: string |
| 50 | defaultBranch: string |
| 51 | release?: Release |
| 52 | /** A fork's assets count against its fork quota, not the asset quota. */ |
| 53 | isFork: boolean |
| 54 | /** Null when the viewer may not see the owner's usage, such as a collaborator outside the organization. */ |
| 55 | storage: StorageUsage |
| 56 | }) { |
| 57 | const { |
| 58 | register, |
| 59 | control, |
| 60 | handleSubmit, |
| 61 | formState: { errors |
| 62 | } = useForm<ReleaseInput |
| 63 | resolver: zodResolver(releaseSchema |
| 64 | defaultValues: { |
| 65 | tagName: release?.tagName |
| 66 | target: defaultBranch ?? |
| 67 | name: release?.name |
| 68 | body: release?.body |
| 69 | isPrerelease: release?.isPrerelease |
| 70 | }, |
| 71 | }); |
| 72 | |
| 73 | const router = |
| 74 | const [files, |
| 75 | const [uploading, |
| 76 | const create = |
| 77 | const update = |
| 78 | const isExecuting = |
| 79 | create.isExecuting || |
| 80 | const serverError = |
| 81 | create.result.serverError |
| 82 | const quota = |
| 83 | ...(isFork ? |
| 84 | maxAssetBytes: storage.maxAssetBytes |
| 85 | }; |
| 86 | const problem = |
| 87 | |
| 88 | const submit = |
| 89 | handleSubmit(async (input |
| 90 | if (problem) return |
| 91 | |
| 92 | const saved = |
| 93 | ? await update. |
| 94 | username, |
| 95 | repo, |
| 96 | id: release.id |
| 97 | name: input.name |
| 98 | body: input.body |
| 99 | isPrerelease: input.isPrerelease |
| 100 | isDraft, |
| 101 | }) |
| 102 | : await create. |
| 103 | if (!saved?.data |
| 104 | |
| 105 | // ponytail: one file at a time with no progress bar; XHR upload events would give one if large files feel stuck |
| 106 | for (const file |
| 107 | setUploading(file.name |
| 108 | try { |
| 109 | await uploadReleaseAsset({ |
| 110 | username, |
| 111 | repo, |
| 112 | releaseId: saved.data |
| 113 | file, |
| 114 | }); |
| 115 | // by identity: two picked files can share a name |
| 116 | setFiles((current) |
| 117 | } catch (error) |
| 118 | setUploading(null); |
| 119 | // the release is saved, so what is left to fix lives on its edit page; the files that did not make it stay picked for a retry there |
| 120 | toast.error((error |
| 121 | router.push(releasePath |
| 122 | router.refresh(); |
| 123 | return; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | router.push(releasePath |
| 128 | router.refresh(); |
| 129 | }); |
| 130 | |
| 131 | return ( |
| 132 | <form onSubmit={submit |
| 133 | <FieldGroup> |
| 134 | <div className="grid gap-4 sm:grid-cols-2 |
| 135 | <Field> |
| 136 | <FieldLabel htmlFor=" |
| 137 | <Input |
| 138 | id="release-tag" |
| 139 | placeholder="v1.0.0" |
| 140 | autoComplete="off" |
| 141 | disabled={Boolean(release |
| 142 | aria-invalid={Boolean(errors |
| 143 | {...register("tagName |
| 144 | /> |
| 145 | <FieldError errors={ |
| 146 | </Field> |
| 147 | |
| 148 | {!release && branches |
| 149 | <Field> |
| 150 | <FieldLabel htmlFor=" |
| 151 | <Controller |
| 152 | control={control} |
| 153 | name="target" |
| 154 | render={({ field |
| 155 | <BranchSelect |
| 156 | id="release-target" |
| 157 | branches={branches} |
| 158 | value={field.value |
| 159 | onValueChange={field.onChange |
| 160 | /> |
| 161 | )} |
| 162 | /> |
| 163 | <FieldDescription> |
| 164 | Used only when the tag does not exist yet. |
| 165 | </FieldDescription> |
| 166 | </Field> |
| 167 | )} |
| 168 | </div> |
| 169 | |
| 170 | <Field> |
| 171 | <FieldLabel htmlFor=" |
| 172 | Title |
| 173 | <span className="font-normal text-muted-foreground |
| 174 | (optional, defaults to the tag) |
| 175 | </span> |
| 176 | </FieldLabel> |
| 177 | <Input |
| 178 | id="release-name" |
| 179 | autoComplete="off" |
| 180 | aria-invalid={Boolean(errors |
| 181 | {...register("name |
| 182 | /> |
| 183 | <FieldError errors={ |
| 184 | </Field> |
| 185 | |
| 186 | <Field> |
| 187 | <FieldLabel htmlFor=" |
| 188 | <Controller |
| 189 | control={control} |
| 190 | name="body" |
| 191 | render={({ field |
| 192 | <MarkdownEditor |
| 193 | id="release-body" |
| 194 | value={field.value |
| 195 | onChange={field.onChange |
| 196 | repository={{ username |
| 197 | rows={12} |
| 198 | maxLength={100000} |
| 199 | placeholder="What changed in this release?" |
| 200 | invalid={Boolean(errors |
| 201 | disabled={isExecuting} |
| 202 | /> |
| 203 | )} |
| 204 | /> |
| 205 | <FieldError errors={ |
| 206 | </Field> |
| 207 | |
| 208 | <Field> |
| 209 | <FieldLabel>Assets</FieldLabel |
| 210 | <ReleaseAssetPicker |
| 211 | username={username} |
| 212 | repo={repo} |
| 213 | existing={release?.assets |
| 214 | files={files} |
| 215 | onFilesChange={setFiles} |
| 216 | storage={quota} |
| 217 | problem={problem} |
| 218 | disabled={isExecuting} |
| 219 | /> |
| 220 | </Field> |
| 221 | |
| 222 | <Controller |
| 223 | control={control} |
| 224 | name="isPrerelease" |
| 225 | render={({ field |
| 226 | <Field orientation=" |
| 227 | <Checkbox |
| 228 | id="release-prerelease" |
| 229 | checked={field.value |
| 230 | onCheckedChange={(checked) |
| 231 | /> |
| 232 | <FieldLabel htmlFor=" |
| 233 | This is a prerelease, and never marked as the latest |
| 234 | </FieldLabel> |
| 235 | </Field> |
| 236 | )} |
| 237 | /> |
| 238 | |
| 239 | {serverError && <FieldError |
| 240 | </FieldGroup> |
| 241 | |
| 242 | <div className="mt-6 flex flex-col-reverse gap-2 sm:flex-row sm:justify-end |
| 243 | <Button type=" |
| 244 | <Link |
| 245 | href={ |
| 246 | release |
| 247 | ? releasePath(username, |
| 248 | : `/${username} |
| 249 | } |
| 250 | > |
| 251 | Cancel |
| 252 | </Link> |
| 253 | </Button> |
| 254 | {(!release || |
| 255 | <Button |
| 256 | type="button" |
| 257 | variant="outline" |
| 258 | disabled={isExecuting} |
| 259 | // a button outside submit skips the form's validation, which holds back text with uploads still in it |
| 260 | onClick={(event) |
| 261 | event.currentTarget.form |
| 262 | } |
| 263 | > |
| 264 | Save draft |
| 265 | </Button> |
| 266 | )} |
| 267 | <Button type=" |
| 268 | {isExecuting && <Spinner |
| 269 | {uploading |
| 270 | ? `Uploading ${uploading} |
| 271 | : release && ! |
| 272 | ? "Update release" |
| 273 | : "Publish release"} |
| 274 | </Button> |
| 275 | </div> |
| 276 | </form> |
| 277 | ); |
| 278 | } |
| 279 | |
| 280 | export function ReleaseFormSkeleton() |
| 281 | return ( |
| 282 | <div className="flex flex-col gap-6 |
| 283 | <div className="grid gap-4 sm:grid-cols-2 |
| 284 | <Skeleton className=" |
| 285 | <Skeleton className=" |
| 286 | </div> |
| 287 | <Skeleton className=" |
| 288 | <Skeleton className=" |
| 289 | <Skeleton className=" |
| 290 | </div> |
| 291 | ); |
| 292 | } |