import { Controller, useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { AxiosError } from "axios"; import { z } from "zod"; import { createNotification } from "@app/components/notifications"; import { Button, FormControl, Modal, ModalContent, Select, SelectItem } from "@app/components/v2"; import { SecretPathInput } from "@app/components/v2/SecretPathInput"; import { useSubscription, useWorkspace } from "@app/context"; import { useCreateSecretImport } from "@app/hooks/api"; const typeSchema = z.object({ environment: z.string().trim(), secretPath: z .string() .trim() .transform((val) => typeof val === "string" && val.at(-1) === "/" && val.length > 1 ? val.slice(0, -1) : val ), isReplication: z.boolean().default(false) }); type TFormSchema = z.infer; type Props = { environment: string; workspaceId: string; secretPath?: string; // modal props isOpen?: boolean; onClose: () => void; onTogglePopUp: (isOpen: boolean) => void; onUpgradePlan: () => void; }; export const CreateSecretImportForm = ({ environment, workspaceId, secretPath = "/", isOpen, onClose, onTogglePopUp, onUpgradePlan }: Props) => { const { handleSubmit, control, reset, watch, formState: { isSubmitting } } = useForm({ resolver: zodResolver(typeSchema) }); const { currentWorkspace } = useWorkspace(); const environments = currentWorkspace?.environments || []; const selectedEnvironment = watch("environment"); const { subscription } = useSubscription(); const { mutateAsync: createSecretImport } = useCreateSecretImport(); const handleFormSubmit = async ({ environment: importedEnv, secretPath: importedSecPath, isReplication }: TFormSchema) => { try { if (isReplication && !subscription?.secretApproval) { onUpgradePlan(); return; } await createSecretImport({ environment, projectId: workspaceId, path: secretPath, isReplication, import: { environment: importedEnv, path: importedSecPath } }); onClose(); reset(); createNotification({ type: "success", text: `Successfully linked. ${isReplication ? "Please refresh the dashboard to view changes" : "" }` }); } catch (err) { console.error(err); const axiosError = err as AxiosError; if (axiosError?.response?.status === 401) { createNotification({ text: "You do not have access to the selected environment/path", type: "error" }); } else { createNotification({ type: "error", text: "Failed to link secrets" }); } } }; return (
( )} /> ( )} /> ( )} />
); };