import { useRef, useState } from "react"; import { AnimatePresence, motion } from "framer-motion"; import { useNotificationContext } from "@app/components/context/Notifications/NotificationProvider"; import { Modal, ModalContent, Step, Stepper } from "@app/components/v2"; import { useCreateSecretRotation } from "@app/hooks/api"; import { TSecretRotationProviderTemplate } from "@app/hooks/api/types"; import { RotationInputForm } from "./steps/RotationInputForm"; import { RotationOutputForm, TFormSchema as TRotationOutputSchema } from "./steps/RotationOutputForm"; const WIZARD_STEPS = [ { title: "Inputs", description: "Provider secrets" }, { title: "Outputs", description: "Map rotated secrets to keys" } ]; type Props = { isOpen?: boolean; onToggle: (isOpen: boolean) => void; customProvider?: string; workspaceId: string; provider: TSecretRotationProviderTemplate; }; export const CreateRotationForm = ({ isOpen, onToggle, provider, workspaceId, customProvider }: Props) => { const [wizardStep, setWizardStep] = useState(0); const wizardData = useRef<{ input?: Record; output?: TRotationOutputSchema; }>({}); const { createNotification } = useNotificationContext(); const { mutateAsync: createSecretRotation } = useCreateSecretRotation(); const handleFormCancel = () => { onToggle(false); setWizardStep(0); wizardData.current = {}; }; const handleFormSubmit = async () => { if (!wizardData.current.input || !wizardData.current.output) return; try { await createSecretRotation({ workspaceId, provider: provider.name, customProvider, secretPath: wizardData.current.output.secretPath, environment: wizardData.current.output.environment, interval: wizardData.current.output.interval, inputs: wizardData.current.input, outputs: wizardData.current.output.secrets }); setWizardStep(0); onToggle(false); wizardData.current = {}; } catch (error) { console.log(error); createNotification({ type: "error", text: "Failed to create secret rotation" }); } }; return ( { onToggle(state); setWizardStep(0); wizardData.current = {}; }} > {WIZARD_STEPS.map(({ title, description }, index) => ( ))} {wizardStep === 0 && ( { wizardData.current.input = data; setWizardStep((state) => state + 1); }} inputSchema={provider.template?.inputs || {}} /> )} {wizardStep === 1 && ( { wizardData.current.output = data; await handleFormSubmit(); }} /> )} ); };