| 1 | "use client"; |
| 2 | |
| 3 | import { zodResolver } from |
| 4 | import Link from "next/link |
| 5 | import { useRouter } |
| 6 | import { useAction } |
| 7 | import { Controller, useForm |
| 8 | |
| 9 | import { Button } |
| 10 | import { |
| 11 | Field, |
| 12 | FieldError, |
| 13 | FieldGroup, |
| 14 | FieldLabel, |
| 15 | } from "@/components/ui/field"; |
| 16 | import { Input } |
| 17 | import { |
| 18 | Select, |
| 19 | SelectContent, |
| 20 | SelectItem, |
| 21 | SelectTrigger, |
| 22 | SelectValue, |
| 23 | } from "@/components/ui/select"; |
| 24 | import { Spinner } |
| 25 | import { Textarea } |
| 26 | import type { CreatePullRequestFormProps |
| 27 | import { createPullRequest } |
| 28 | import { type CreatePullRequestInput, |
| 29 | |
| 30 | export function CreatePullRequestForm({ |
| 31 | username, |
| 32 | repo, |
| 33 | bases, |
| 34 | heads, |
| 35 | defaultBase, |
| 36 | defaultHead, |
| 37 | }: CreatePullRequestFormProps) |
| 38 | const router = |
| 39 | const { |
| 40 | control, |
| 41 | register, |
| 42 | handleSubmit, |
| 43 | watch, |
| 44 | formState: { errors |
| 45 | } = useForm<CreatePullRequestInput |
| 46 | resolver: zodResolver(createPullRequestSchema |
| 47 | defaultValues: { |
| 48 | title: "", |
| 49 | body: "", |
| 50 | base: defaultBase, |
| 51 | head: defaultHead, |
| 52 | }, |
| 53 | }); |
| 54 | |
| 55 | const { execute, |
| 56 | const base = |
| 57 | const head = |
| 58 | |
| 59 | // The diff is rendered on the server, so the pair being compared lives in the URL. |
| 60 | function preview(next |
| 61 | const params = |
| 62 | router.replace(`? |
| 63 | } |
| 64 | |
| 65 | return ( |
| 66 | <form |
| 67 | onSubmit={handleSubmit(( |
| 68 | className="contents" |
| 69 | > |
| 70 | <FieldGroup> |
| 71 | <div className="flex flex-col gap-2 sm:flex-row sm:items-start |
| 72 | <Field className=" |
| 73 | <FieldLabel htmlFor=" |
| 74 | <Controller |
| 75 | control={control} |
| 76 | name="base" |
| 77 | render={({ field |
| 78 | <Select |
| 79 | value={field.value |
| 80 | onValueChange={(value) |
| 81 | field.onChange(value |
| 82 | preview({ base: |
| 83 | }} |
| 84 | > |
| 85 | <SelectTrigger id=" |
| 86 | <SelectValue placeholder=" |
| 87 | </SelectTrigger> |
| 88 | <SelectContent> |
| 89 | {bases.map(( |
| 90 | <SelectItem key={ |
| 91 | {branch} |
| 92 | </SelectItem> |
| 93 | ))} |
| 94 | </SelectContent> |
| 95 | </Select> |
| 96 | )} |
| 97 | /> |
| 98 | <FieldError errors={ |
| 99 | </Field> |
| 100 | |
| 101 | <span className="hidden pt-8 text-muted-foreground sm:inline |
| 102 | |
| 103 | <Field className=" |
| 104 | <FieldLabel htmlFor=" |
| 105 | <Controller |
| 106 | control={control} |
| 107 | name="head" |
| 108 | render={({ field |
| 109 | <Select |
| 110 | value={field.value |
| 111 | onValueChange={(value) |
| 112 | field.onChange(value |
| 113 | preview({ head: |
| 114 | }} |
| 115 | > |
| 116 | <SelectTrigger id=" |
| 117 | <SelectValue placeholder=" |
| 118 | </SelectTrigger> |
| 119 | <SelectContent> |
| 120 | {heads |
| 121 | .filter((branch) |
| 122 | .map((branch) |
| 123 | <SelectItem key={ |
| 124 | {branch} |
| 125 | </SelectItem> |
| 126 | ))} |
| 127 | </SelectContent> |
| 128 | </Select> |
| 129 | )} |
| 130 | /> |
| 131 | <FieldError errors={ |
| 132 | </Field> |
| 133 | </div> |
| 134 | |
| 135 | <Field> |
| 136 | <FieldLabel htmlFor=" |
| 137 | <Input |
| 138 | id="pull-title" |
| 139 | placeholder="Add a rate limiter" |
| 140 | autoComplete="off" |
| 141 | aria-invalid={Boolean(errors |
| 142 | {...register("title |
| 143 | /> |
| 144 | <FieldError errors={ |
| 145 | </Field> |
| 146 | |
| 147 | <Field> |
| 148 | <FieldLabel htmlFor=" |
| 149 | Description |
| 150 | <span className="font-normal text-muted-foreground |
| 151 | (optional) |
| 152 | </span> |
| 153 | </FieldLabel> |
| 154 | <Textarea |
| 155 | id="pull-body" |
| 156 | rows={6} |
| 157 | aria-invalid={Boolean(errors |
| 158 | {...register("body |
| 159 | /> |
| 160 | <FieldError errors={ |
| 161 | </Field> |
| 162 | |
| 163 | {result.serverError && |
| 164 | </FieldGroup> |
| 165 | |
| 166 | <div className="mt-6 flex flex-col-reverse gap-2 sm:flex-row sm:justify-end |
| 167 | <Button type=" |
| 168 | <Link href={ |
| 169 | </Button> |
| 170 | <Button |
| 171 | type="button" |
| 172 | variant="outline" |
| 173 | disabled={isExecuting || |
| 174 | onClick={handleSubmit(( |
| 175 | execute({ ...input |
| 176 | )} |
| 177 | > |
| 178 | Create draft |
| 179 | </Button> |
| 180 | <Button |
| 181 | type="submit" |
| 182 | disabled={isExecuting || |
| 183 | > |
| 184 | {isExecuting && <Spinner |
| 185 | Create pull request |
| 186 | </Button> |
| 187 | </div> |
| 188 | </form> |
| 189 | ); |
| 190 | } |