import { Controller, useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { createNotification } from "@app/components/notifications"; import { Button, FormControl, Modal, ModalContent, Select, SelectItem } from "@app/components/v2"; import { useWorkspace } from "@app/context"; import { useRevokeCert } from "@app/hooks/api"; import { crlReasons } from "@app/hooks/api/certificates/constants"; import { CrlReason } from "@app/hooks/api/certificates/enums"; import { UsePopUpState } from "@app/hooks/usePopUp"; const schema = z.object({ revocationReason: z.enum([ CrlReason.UNSPECIFIED, CrlReason.KEY_COMPROMISE, CrlReason.CA_COMPROMISE, CrlReason.AFFILIATION_CHANGED, CrlReason.SUPERSEDED, CrlReason.CESSATION_OF_OPERATION, CrlReason.CERTIFICATE_HOLD, CrlReason.PRIVILEGE_WITHDRAWN, CrlReason.A_A_COMPROMISE ]) }); export type FormData = z.infer; type Props = { popUp: UsePopUpState<["revokeCertificate"]>; handlePopUpToggle: ( popUpName: keyof UsePopUpState<["revokeCertificate"]>, state?: boolean ) => void; }; export const CertificateRevocationModal = ({ popUp, handlePopUpToggle }: Props) => { const { currentWorkspace } = useWorkspace(); const { mutateAsync: revokeCertificate } = useRevokeCert(); const { control, handleSubmit, reset, formState: { isSubmitting } } = useForm({ resolver: zodResolver(schema) }); const onFormSubmit = async ({ revocationReason }: FormData) => { try { if (!currentWorkspace?.slug) return; const { serialNumber } = popUp.revokeCertificate.data as { serialNumber: string }; await revokeCertificate({ projectSlug: currentWorkspace.slug, serialNumber, revocationReason }); reset(); handlePopUpToggle("revokeCertificate", false); createNotification({ text: "Successfully revoked certificate", type: "success" }); } catch (err) { console.error(err); createNotification({ text: "Failed to revoke certificate", type: "error" }); } }; return ( { handlePopUpToggle("revokeCertificate", isOpen); reset(); }} >
( )} />
); };