| 1 | "use client"; |
| 2 | |
| 3 | import { zodResolver } from |
| 4 | import { useQuery } |
| 5 | import { useAction } |
| 6 | import type { ReactNode |
| 7 | import { Controller, useForm |
| 8 | |
| 9 | import { Button } |
| 10 | import { |
| 11 | Dialog, |
| 12 | DialogClose, |
| 13 | DialogContent, |
| 14 | DialogDescription, |
| 15 | DialogFooter, |
| 16 | DialogHeader, |
| 17 | DialogTitle, |
| 18 | DialogTrigger, |
| 19 | } from "@/components/ui/dialog"; |
| 20 | import { |
| 21 | Field, |
| 22 | FieldError, |
| 23 | FieldGroup, |
| 24 | FieldLabel, |
| 25 | FieldSeparator, |
| 26 | } from "@/components/ui/field"; |
| 27 | import { Input } |
| 28 | import { |
| 29 | Select, |
| 30 | SelectContent, |
| 31 | SelectItem, |
| 32 | SelectTrigger, |
| 33 | SelectValue, |
| 34 | } from "@/components/ui/select"; |
| 35 | import { Spinner } |
| 36 | import { Textarea } |
| 37 | import { apiClient, apiErrorMessage |
| 38 | |
| 39 | import { createRepository, importRepository |
| 40 | import { newRepositorySchema, type |
| 41 | import { GitHubSourceField } |
| 42 | import { VisibilityField } |
| 43 | |
| 44 | export function NewRepositoryDialog({ |
| 45 | children, |
| 46 | owners, |
| 47 | defaultOwner, |
| 48 | }: { |
| 49 | children: ReactNode |
| 50 | owners: string |
| 51 | defaultOwner: string |
| 52 | }) { |
| 53 | const { |
| 54 | control, |
| 55 | register, |
| 56 | handleSubmit, |
| 57 | getValues, |
| 58 | setValue, |
| 59 | formState: { errors |
| 60 | } = useForm<NewRepositoryInput |
| 61 | resolver: zodResolver(newRepositorySchema |
| 62 | defaultValues: { |
| 63 | owner: defaultOwner, |
| 64 | name: "", |
| 65 | description: "", |
| 66 | visibility: "public", |
| 67 | source: "", |
| 68 | }, |
| 69 | }); |
| 70 | |
| 71 | const create = |
| 72 | const startImport = |
| 73 | const importing = |
| 74 | const isExecuting = |
| 75 | const { result |
| 76 | |
| 77 | const { data: |
| 78 | queryKey: ["github-import"], |
| 79 | queryFn: async () |
| 80 | const { data, |
| 81 | if (error) throw |
| 82 | return data; |
| 83 | }, |
| 84 | }); |
| 85 | |
| 86 | // Organizations whose policy lets the viewer create repositories there, on top of the accounts the caller passed. |
| 87 | const { data: |
| 88 | queryKey: ["my-organizations"], |
| 89 | enabled: owners.length |
| 90 | queryFn: async () |
| 91 | const { data, |
| 92 | if (error) throw |
| 93 | return data.organizations |
| 94 | .filter((organization) |
| 95 | .map((organization) |
| 96 | }, |
| 97 | }); |
| 98 | const choices = |
| 99 | |
| 100 | return ( |
| 101 | <Dialog> |
| 102 | <DialogTrigger asChild>{ |
| 103 | |
| 104 | <DialogContent className=" |
| 105 | <DialogHeader> |
| 106 | <DialogTitle>Create a new repository</DialogTitle |
| 107 | <DialogDescription> |
| 108 | A repository contains all project files, including the revision |
| 109 | history. |
| 110 | </DialogDescription> |
| 111 | </DialogHeader> |
| 112 | |
| 113 | <form |
| 114 | onSubmit={handleSubmit(( |
| 115 | const organization = |
| 116 | ? input.owner |
| 117 | : undefined; |
| 118 | if (source) startImport. |
| 119 | else create.execute( |
| 120 | })} |
| 121 | className="contents" |
| 122 | > |
| 123 | <FieldGroup> |
| 124 | {github?.enabled && |
| 125 | <> |
| 126 | <Controller |
| 127 | control={control} |
| 128 | name="source" |
| 129 | render={({ field |
| 130 | <GitHubSourceField |
| 131 | id="repository-source" |
| 132 | connected={github.connected |
| 133 | error={errors.source |
| 134 | value={field.value |
| 135 | onChange={(source) |
| 136 | field.onChange(source |
| 137 | // The GitHub name is the likeliest name here too. |
| 138 | const name = |
| 139 | if (name && ! |
| 140 | }} |
| 141 | /> |
| 142 | )} |
| 143 | /> |
| 144 | <FieldSeparator /> |
| 145 | </> |
| 146 | )} |
| 147 | |
| 148 | <div className="flex items-start gap-2 |
| 149 | <Field className=" |
| 150 | <FieldLabel htmlFor=" |
| 151 | <Controller |
| 152 | control={control} |
| 153 | name="owner" |
| 154 | render={({ field |
| 155 | <Select value={ |
| 156 | <SelectTrigger id=" |
| 157 | <SelectValue /> |
| 158 | </SelectTrigger> |
| 159 | <SelectContent> |
| 160 | {choices.map(( |
| 161 | <SelectItem key={ |
| 162 | {owner} |
| 163 | </SelectItem> |
| 164 | ))} |
| 165 | </SelectContent> |
| 166 | </Select> |
| 167 | )} |
| 168 | /> |
| 169 | <FieldError errors={ |
| 170 | </Field> |
| 171 | |
| 172 | <span className="pt-6 text-lg text-muted-foreground |
| 173 | |
| 174 | <Field> |
| 175 | <FieldLabel htmlFor=" |
| 176 | Repository name |
| 177 | </FieldLabel> |
| 178 | <Input |
| 179 | id="repository-name" |
| 180 | placeholder="my-project" |
| 181 | autoComplete="off" |
| 182 | aria-invalid={Boolean(errors |
| 183 | {...register("name |
| 184 | /> |
| 185 | <FieldError errors={ |
| 186 | </Field> |
| 187 | </div> |
| 188 | |
| 189 | <Field> |
| 190 | <FieldLabel htmlFor=" |
| 191 | Description |
| 192 | <span className="font-normal text-muted-foreground |
| 193 | (optional) |
| 194 | </span> |
| 195 | </FieldLabel> |
| 196 | <Textarea |
| 197 | id="repository-description" |
| 198 | placeholder="What is this project about?" |
| 199 | rows={3} |
| 200 | aria-invalid={Boolean(errors |
| 201 | {...register("description |
| 202 | /> |
| 203 | <FieldError errors={ |
| 204 | </Field> |
| 205 | |
| 206 | <FieldSeparator /> |
| 207 | |
| 208 | <Controller |
| 209 | control={control} |
| 210 | name="visibility" |
| 211 | render={({ field |
| 212 | <VisibilityField |
| 213 | id="repository-visibility" |
| 214 | value={field.value |
| 215 | onChange={field.onChange |
| 216 | /> |
| 217 | )} |
| 218 | /> |
| 219 | |
| 220 | {result.serverError && |
| 221 | <FieldError>{result. |
| 222 | )} |
| 223 | </FieldGroup> |
| 224 | |
| 225 | <DialogFooter className=" |
| 226 | <DialogClose asChild> |
| 227 | <Button type=" |
| 228 | Cancel |
| 229 | </Button> |
| 230 | </DialogClose> |
| 231 | <Button type=" |
| 232 | {isExecuting && <Spinner |
| 233 | {importing ? "Import repository |
| 234 | </Button> |
| 235 | </DialogFooter> |
| 236 | </form> |
| 237 | </DialogContent> |
| 238 | </Dialog> |
| 239 | ); |
| 240 | } |