|
| 1 | +import EyeIcon from "@/assets/icons/eye.svg?react"; |
| 2 | +import Plus from "@/assets/icons/plus.svg?react"; |
| 3 | +import Trash from "@/assets/icons/trash.svg?react"; |
| 4 | +import { useCreateSecretMutation } from "@/data/secrets/create-secret-query"; |
| 5 | +import { isFetchError } from "@/lib/fetch-error"; |
| 6 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 7 | +import { useQueryClient } from "@tanstack/react-query"; |
| 8 | +import { |
| 9 | + Button, |
| 10 | + Dialog, |
| 11 | + DialogClose, |
| 12 | + DialogContent, |
| 13 | + DialogFooter, |
| 14 | + DialogHeader, |
| 15 | + DialogTitle, |
| 16 | + DialogTrigger, |
| 17 | + Input, |
| 18 | + useToast |
| 19 | +} from "@zenml-io/react-component-library"; |
| 20 | +import { useState } from "react"; |
| 21 | +import { Controller, useFieldArray, useForm } from "react-hook-form"; |
| 22 | +import { secretFormSchema, SecretFormType } from "./form-schema"; |
| 23 | + |
| 24 | +interface Workspace { |
| 25 | + id: string; |
| 26 | +} |
| 27 | + |
| 28 | +export function AddSecretDialog({ id, workspace }: { id: string; workspace: Workspace }) { |
| 29 | + const [open, setOpen] = useState(false); |
| 30 | + |
| 31 | + return ( |
| 32 | + <Dialog |
| 33 | + open={open} |
| 34 | + onOpenChange={(val) => { |
| 35 | + setOpen(val); |
| 36 | + }} |
| 37 | + > |
| 38 | + <DialogTrigger asChild> |
| 39 | + <Button className="shrink-0" intent="primary"> |
| 40 | + Add secret |
| 41 | + </Button> |
| 42 | + </DialogTrigger> |
| 43 | + <DialogContent className="mx-auto w-[90vw] max-w-[744px]"> |
| 44 | + <DialogHeader> |
| 45 | + <DialogTitle>Register New Secret</DialogTitle> |
| 46 | + </DialogHeader> |
| 47 | + <AddSecret userId={id} setOpen={setOpen} workspaceId={workspace.id} /> |
| 48 | + </DialogContent> |
| 49 | + </Dialog> |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +export function AddSecret({ |
| 54 | + userId, |
| 55 | + setOpen, |
| 56 | + workspaceId |
| 57 | +}: { |
| 58 | + userId: string; |
| 59 | + setOpen: (open: boolean) => void; |
| 60 | + workspaceId: string; |
| 61 | +}) { |
| 62 | + const { |
| 63 | + handleSubmit, |
| 64 | + control, |
| 65 | + watch, |
| 66 | + setValue, |
| 67 | + formState: { isValid }, |
| 68 | + reset |
| 69 | + } = useForm<SecretFormType>({ |
| 70 | + resolver: zodResolver(secretFormSchema), |
| 71 | + defaultValues: { |
| 72 | + secretName: "", |
| 73 | + keysValues: [{ key: "", value: "" }] |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + const { fields, append, remove } = useFieldArray({ |
| 78 | + control, |
| 79 | + name: "keysValues" |
| 80 | + }); |
| 81 | + |
| 82 | + const { toast } = useToast(); |
| 83 | + const queryClient = useQueryClient(); |
| 84 | + const { mutate } = useCreateSecretMutation({ |
| 85 | + onError(error) { |
| 86 | + if (isFetchError(error)) { |
| 87 | + toast({ |
| 88 | + status: "error", |
| 89 | + emphasis: "subtle", |
| 90 | + description: error.message, |
| 91 | + rounded: true |
| 92 | + }); |
| 93 | + } |
| 94 | + }, |
| 95 | + onSuccess() { |
| 96 | + queryClient.invalidateQueries({ queryKey: ["secrets"] }); |
| 97 | + setOpen(false); |
| 98 | + reset(); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + const postSecret = (data: SecretFormType) => { |
| 103 | + mutate({ |
| 104 | + user: userId, |
| 105 | + workspace: workspaceId, |
| 106 | + name: data.secretName, |
| 107 | + scope: "workspace", |
| 108 | + values: data.keysValues.reduce( |
| 109 | + (acc, pair) => { |
| 110 | + if (pair.key && pair.value) acc[pair.key] = pair.value; |
| 111 | + return acc; |
| 112 | + }, |
| 113 | + {} as Record<string, string> |
| 114 | + ) |
| 115 | + }); |
| 116 | + }; |
| 117 | + |
| 118 | + const onSubmit = (data: SecretFormType) => { |
| 119 | + postSecret(data); |
| 120 | + }; |
| 121 | + |
| 122 | + return ( |
| 123 | + <> |
| 124 | + <form id="create-secret-form" className="gap-5 p-5" onSubmit={handleSubmit(onSubmit)}> |
| 125 | + <div className="space-y-1"> |
| 126 | + <div className="space-y-0.5"> |
| 127 | + <label className="font-inter text-sm text-left font-medium leading-5"> |
| 128 | + Secret Name |
| 129 | + <span className="ml-1 text-theme-text-error">*</span> |
| 130 | + </label> |
| 131 | + <Controller |
| 132 | + name="secretName" |
| 133 | + control={control} |
| 134 | + render={({ field }) => <Input {...field} className="mb-3 w-full" required />} |
| 135 | + /> |
| 136 | + </div> |
| 137 | + <div className="mt-10"> |
| 138 | + <div> |
| 139 | + <h1 className="font-inter text-lg text-left font-semibold ">Keys</h1> |
| 140 | + </div> |
| 141 | + <div className="mt-5 flex flex-row "> |
| 142 | + <div className="flex-grow"> |
| 143 | + <label className="font-inter text-sm text-left font-medium">Key</label> |
| 144 | + </div> |
| 145 | + <div className="flex-grow pr-12"> |
| 146 | + <label className="font-inter text-sm text-left font-medium">Value</label> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + </div> |
| 150 | + |
| 151 | + {fields.map((field, index) => ( |
| 152 | + <div key={field.id} className="flex flex-row items-center space-x-1 "> |
| 153 | + <div className="relative flex-grow "> |
| 154 | + <Controller |
| 155 | + name={`keysValues.${index}.key`} |
| 156 | + control={control} |
| 157 | + render={({ field }) => ( |
| 158 | + <Input {...field} className="mb-2 w-full" required placeholder="key" /> |
| 159 | + )} |
| 160 | + /> |
| 161 | + </div> |
| 162 | + <div className="relative flex-grow"> |
| 163 | + <div className="relative"> |
| 164 | + <Controller |
| 165 | + name={`keysValues.${index}.value`} |
| 166 | + control={control} |
| 167 | + render={({ field }) => ( |
| 168 | + <Input |
| 169 | + {...field} |
| 170 | + className="mb-2 w-full pr-10" |
| 171 | + required |
| 172 | + placeholder="•••••••••" |
| 173 | + type={watch(`keysValues.${index}.showPassword`) ? "text" : "password"} |
| 174 | + /> |
| 175 | + )} |
| 176 | + /> |
| 177 | + <div |
| 178 | + onClick={() => { |
| 179 | + const showPassword = watch(`keysValues.${index}.showPassword`); |
| 180 | + setValue(`keysValues.${index}.showPassword`, !showPassword); |
| 181 | + }} |
| 182 | + className="absolute inset-y-1 right-0 flex cursor-pointer items-center pb-1 pr-3" |
| 183 | + > |
| 184 | + <EyeIcon className="h-4 w-4 flex-shrink-0 cursor-pointer" /> |
| 185 | + </div> |
| 186 | + </div> |
| 187 | + </div> |
| 188 | + <div className="flex items-center"> |
| 189 | + {index === fields.length - 1 && ( |
| 190 | + <Button |
| 191 | + intent="primary" |
| 192 | + emphasis="subtle" |
| 193 | + onClick={() => append({ key: "", value: "" })} |
| 194 | + className="mb-2 flex h-7 w-7 items-center justify-center" |
| 195 | + > |
| 196 | + <Plus className="flex-shrink-0 fill-primary-600" /> |
| 197 | + </Button> |
| 198 | + )} |
| 199 | + {index !== fields.length - 1 && ( |
| 200 | + <Button |
| 201 | + intent="secondary" |
| 202 | + emphasis="minimal" |
| 203 | + onClick={() => remove(index)} |
| 204 | + className="mb-2 h-7 w-7 items-center justify-center" |
| 205 | + > |
| 206 | + <Trash className="flex-shrink-0 fill-theme-text-secondary" /> |
| 207 | + </Button> |
| 208 | + )} |
| 209 | + </div> |
| 210 | + </div> |
| 211 | + ))} |
| 212 | + </div> |
| 213 | + </form> |
| 214 | + <DialogFooter className="gap-[10px]"> |
| 215 | + <DialogClose asChild> |
| 216 | + <Button size="sm" intent="secondary"> |
| 217 | + Cancel |
| 218 | + </Button> |
| 219 | + </DialogClose> |
| 220 | + <Button intent="primary" disabled={!isValid} type="submit" form="create-secret-form"> |
| 221 | + Register Secret |
| 222 | + </Button> |
| 223 | + </DialogFooter> |
| 224 | + </> |
| 225 | + ); |
| 226 | +} |
0 commit comments