| 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 } |
| 40 | import { createRepositorySchema, type |
| 41 | import { VisibilityField } |
| 42 | |
| 43 | export function NewRepositoryDialog({ |
| 44 | children, |
| 45 | owners, |
| 46 | defaultOwner, |
| 47 | }: { |
| 48 | children: ReactNode |
| 49 | owners: string |
| 50 | defaultOwner: string |
| 51 | }) { |
| 52 | const { |
| 53 | control, |
| 54 | register, |
| 55 | handleSubmit, |
| 56 | formState: { errors |
| 57 | } = useForm<CreateRepositoryInput |
| 58 | resolver: zodResolver(createRepositorySchema |
| 59 | defaultValues: { |
| 60 | owner: defaultOwner, |
| 61 | name: "", |
| 62 | description: "", |
| 63 | visibility: "public", |
| 64 | }, |
| 65 | }); |
| 66 | |
| 67 | const { execute, |
| 68 | |
| 69 | // Organizations whose policy lets the viewer create repositories there, on top of the accounts the caller passed. |
| 70 | const { data: |
| 71 | queryKey: ["my-organizations"], |
| 72 | enabled: owners.length |
| 73 | queryFn: async () |
| 74 | const { data, |
| 75 | if (error) throw |
| 76 | return data.organizations |
| 77 | .filter((organization) |
| 78 | .map((organization) |
| 79 | }, |
| 80 | }); |
| 81 | const choices = |
| 82 | |
| 83 | return ( |
| 84 | <Dialog> |
| 85 | <DialogTrigger asChild>{ |
| 86 | |
| 87 | <DialogContent className=" |
| 88 | <DialogHeader> |
| 89 | <DialogTitle>Create a new repository</DialogTitle |
| 90 | <DialogDescription> |
| 91 | A repository contains all project files, including the revision |
| 92 | history. |
| 93 | </DialogDescription> |
| 94 | </DialogHeader> |
| 95 | |
| 96 | <form |
| 97 | onSubmit={handleSubmit(( |
| 98 | execute({ |
| 99 | ...input, |
| 100 | organization: organizations.includes |
| 101 | ? input.owner |
| 102 | : undefined, |
| 103 | }), |
| 104 | )} |
| 105 | className="contents" |
| 106 | > |
| 107 | <FieldGroup> |
| 108 | <div className="flex items-start gap-2 |
| 109 | <Field className=" |
| 110 | <FieldLabel htmlFor=" |
| 111 | <Controller |
| 112 | control={control} |
| 113 | name="owner" |
| 114 | render={({ field |
| 115 | <Select value={ |
| 116 | <SelectTrigger id=" |
| 117 | <SelectValue /> |
| 118 | </SelectTrigger> |
| 119 | <SelectContent> |
| 120 | {choices.map(( |
| 121 | <SelectItem key={ |
| 122 | {owner} |
| 123 | </SelectItem> |
| 124 | ))} |
| 125 | </SelectContent> |
| 126 | </Select> |
| 127 | )} |
| 128 | /> |
| 129 | <FieldError errors={ |
| 130 | </Field> |
| 131 | |
| 132 | <span className="pt-6 text-lg text-muted-foreground |
| 133 | |
| 134 | <Field> |
| 135 | <FieldLabel htmlFor=" |
| 136 | Repository name |
| 137 | </FieldLabel> |
| 138 | <Input |
| 139 | id="repository-name" |
| 140 | placeholder="my-project" |
| 141 | autoComplete="off" |
| 142 | aria-invalid={Boolean(errors |
| 143 | {...register("name |
| 144 | /> |
| 145 | <FieldError errors={ |
| 146 | </Field> |
| 147 | </div> |
| 148 | |
| 149 | <Field> |
| 150 | <FieldLabel htmlFor=" |
| 151 | Description |
| 152 | <span className="font-normal text-muted-foreground |
| 153 | (optional) |
| 154 | </span> |
| 155 | </FieldLabel> |
| 156 | <Textarea |
| 157 | id="repository-description" |
| 158 | placeholder="What is this project about?" |
| 159 | rows={3} |
| 160 | aria-invalid={Boolean(errors |
| 161 | {...register("description |
| 162 | /> |
| 163 | <FieldError errors={ |
| 164 | </Field> |
| 165 | |
| 166 | <FieldSeparator /> |
| 167 | |
| 168 | <Controller |
| 169 | control={control} |
| 170 | name="visibility" |
| 171 | render={({ field |
| 172 | <VisibilityField |
| 173 | id="repository-visibility" |
| 174 | value={field.value |
| 175 | onChange={field.onChange |
| 176 | /> |
| 177 | )} |
| 178 | /> |
| 179 | |
| 180 | {result.serverError && |
| 181 | <FieldError>{result. |
| 182 | )} |
| 183 | </FieldGroup> |
| 184 | |
| 185 | <DialogFooter className=" |
| 186 | <DialogClose asChild> |
| 187 | <Button type=" |
| 188 | Cancel |
| 189 | </Button> |
| 190 | </DialogClose> |
| 191 | <Button type=" |
| 192 | {isExecuting && <Spinner |
| 193 | Create repository |
| 194 | </Button> |
| 195 | </DialogFooter> |
| 196 | </form> |
| 197 | </DialogContent> |
| 198 | </Dialog> |
| 199 | ); |
| 200 | } |