| 1 | "use client"; |
| 2 | |
| 3 | import { zodResolver } from |
| 4 | import Link from "next/link |
| 5 | import { useAction } |
| 6 | import { Controller, useForm |
| 7 | |
| 8 | import { Button } |
| 9 | import { |
| 10 | Field, |
| 11 | FieldError, |
| 12 | FieldGroup, |
| 13 | FieldLabel, |
| 14 | FieldSeparator, |
| 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 | |
| 27 | import { forkRepository } |
| 28 | import { type ForkRepositoryInput, |
| 29 | import { VisibilityField } |
| 30 | |
| 31 | export function ForkRepositoryForm({ |
| 32 | parentUsername, |
| 33 | parentSlug, |
| 34 | name, |
| 35 | description, |
| 36 | visibility, |
| 37 | owners, |
| 38 | organizations, |
| 39 | defaultOwner, |
| 40 | }: { |
| 41 | parentUsername: string |
| 42 | parentSlug: string |
| 43 | name: string |
| 44 | description?: string |
| 45 | visibility: "public |
| 46 | /** Owners without a fork of this repository yet. Empty means every owner has one. */ |
| 47 | owners: string |
| 48 | /** Which of `owners` are organizations, forked into by slug. */ |
| 49 | organizations: string |
| 50 | defaultOwner: string |
| 51 | }) { |
| 52 | const { |
| 53 | control, |
| 54 | register, |
| 55 | handleSubmit, |
| 56 | formState: { errors |
| 57 | } = useForm<ForkRepositoryInput |
| 58 | resolver: zodResolver(forkRepositorySchema |
| 59 | defaultValues: { |
| 60 | owner: defaultOwner, |
| 61 | name, |
| 62 | description: description ?? |
| 63 | visibility, |
| 64 | }, |
| 65 | }); |
| 66 | |
| 67 | const { execute, |
| 68 | |
| 69 | return ( |
| 70 | <form |
| 71 | onSubmit={handleSubmit(( |
| 72 | execute({ |
| 73 | ...input, |
| 74 | parentUsername, |
| 75 | parentSlug, |
| 76 | organization: organizations.includes |
| 77 | ? input.owner |
| 78 | : undefined, |
| 79 | }), |
| 80 | )} |
| 81 | className="contents" |
| 82 | > |
| 83 | <FieldGroup> |
| 84 | <div className="flex flex-col items-stretch gap-2 sm:flex-row sm:items-start |
| 85 | <Field className=" |
| 86 | <FieldLabel htmlFor=" |
| 87 | <Controller |
| 88 | control={control} |
| 89 | name="owner" |
| 90 | render={({ field |
| 91 | <Select value={ |
| 92 | <SelectTrigger id=" |
| 93 | <SelectValue /> |
| 94 | </SelectTrigger> |
| 95 | <SelectContent> |
| 96 | {owners.map(( |
| 97 | <SelectItem key={ |
| 98 | {owner} |
| 99 | </SelectItem> |
| 100 | ))} |
| 101 | </SelectContent> |
| 102 | </Select> |
| 103 | )} |
| 104 | /> |
| 105 | <FieldError errors={ |
| 106 | </Field> |
| 107 | |
| 108 | <span className="hidden pt-6 text-lg text-muted-foreground sm:inline |
| 109 | / |
| 110 | </span> |
| 111 | |
| 112 | <Field className=" |
| 113 | <FieldLabel htmlFor=" |
| 114 | <Input |
| 115 | id="fork-name" |
| 116 | placeholder="my-project" |
| 117 | autoComplete="off" |
| 118 | aria-invalid={Boolean(errors |
| 119 | {...register("name |
| 120 | /> |
| 121 | <FieldError errors={ |
| 122 | </Field> |
| 123 | </div> |
| 124 | |
| 125 | <Field> |
| 126 | <FieldLabel htmlFor=" |
| 127 | Description |
| 128 | <span className="font-normal text-muted-foreground |
| 129 | (optional) |
| 130 | </span> |
| 131 | </FieldLabel> |
| 132 | <Textarea |
| 133 | id="fork-description" |
| 134 | rows={3} |
| 135 | aria-invalid={Boolean(errors |
| 136 | {...register("description |
| 137 | /> |
| 138 | <FieldError errors={ |
| 139 | </Field> |
| 140 | |
| 141 | <FieldSeparator /> |
| 142 | |
| 143 | <Controller |
| 144 | control={control} |
| 145 | name="visibility" |
| 146 | render={({ field |
| 147 | <VisibilityField |
| 148 | id="fork-visibility" |
| 149 | value={field.value |
| 150 | onChange={field.onChange |
| 151 | /> |
| 152 | )} |
| 153 | /> |
| 154 | |
| 155 | {result.serverError && |
| 156 | </FieldGroup> |
| 157 | |
| 158 | <div className="mt-6 flex flex-col-reverse gap-2 sm:flex-row sm:justify-end |
| 159 | <Button type=" |
| 160 | <Link href={ |
| 161 | </Button> |
| 162 | <Button type=" |
| 163 | {isExecuting && <Spinner |
| 164 | Create fork |
| 165 | </Button> |
| 166 | </div> |
| 167 | </form> |
| 168 | ); |
| 169 | } |