import { useEffect, useRef, useState } from "react"; import { Button, Checkbox, Divider, Group, Stack, Text, UnstyledButton, } from "@mantine/core"; import { IconArrowLeft, IconArrowRight, IconCertificate2, IconCheck, IconRefresh, } from "@tabler/icons-react"; import { useTranslation } from "react-i18next"; import { useAtom } from "jotai"; import classes from "./page-verification-modal.module.css"; import { currentUserAtom } from "@/features/user/atoms/current-user-atom"; import { useSetupVerificationMutation } from "@/ee/page-verification/queries/page-verification-query"; import { ExpirationMode, PeriodUnit, VerificationType, } from "@/ee/page-verification/types/page-verification.types"; import { ExpirationFields, PERIOD_AMOUNT_MIN, PERIOD_UNIT_MAX_AMOUNT, } from "./expiration-fields"; import { VerifierPicker } from "./verifier-picker"; import { VerifierList } from "./verifier-list"; import { MAX_VERIFIERS, UserOptionItem } from "./user-option"; type WorkflowChooserProps = { onSelect: (type: VerificationType) => void; }; function WorkflowChooser({ onSelect }: WorkflowChooserProps) { const { t } = useTranslation(); return ( {t("Choose how this page should stay accurate.")}
onSelect("expiring" as VerificationType)} >

{t("Recurring verification")}

{t("Verifiers re-confirm this page on a schedule.")}

{t("Re-verify on a schedule (e.g every 30 days )")}
{t("Best for runbooks, FAQs, living documentation")}
onSelect("qms" as VerificationType)} >

{t("Approval workflow")}

{t("Formal document lifecycle with named approvers.")}

{t("Draft → In approval → Approved → Obsolete")}
{t("Designed for ISO 9001, ISO 13485, and FDA")}
{t("Best for SOPs and controlled documents")}
); } type SetupVerificationFormProps = { pageId: string; onClose: () => void; }; export function SetupVerificationForm({ pageId, onClose, }: SetupVerificationFormProps) { const { t } = useTranslation(); const setupMutation = useSetupVerificationMutation(); const [currentUser] = useAtom(currentUserAtom); const [type, setType] = useState(null); const [mode, setMode] = useState("period"); const [periodAmount, setPeriodAmount] = useState(1); const [periodUnit, setPeriodUnit] = useState("month"); const [fixedDate, setFixedDate] = useState(""); const [confirmed, setConfirmed] = useState(false); const [selectedVerifiers, setSelectedVerifiers] = useState( [], ); const didInitCurrentUser = useRef(false); useEffect(() => { if (!didInitCurrentUser.current && currentUser?.user) { didInitCurrentUser.current = true; const u = currentUser.user; setSelectedVerifiers([ { value: u.id, label: u.name, email: u.email, avatarUrl: u.avatarUrl, }, ]); } }, [currentUser]); const isQms = type === "qms"; const canAddMore = selectedVerifiers.length < MAX_VERIFIERS; if (type === null) { return ; } const handleAddVerifier = (user: UserOptionItem) => { setSelectedVerifiers((prev) => prev.some((v) => v.value === user.value) ? prev : [...prev, user], ); }; const handleRemoveVerifier = (userId: string) => { setSelectedVerifiers((prev) => prev.filter((v) => v.value !== userId)); }; const handleSetup = () => { if (selectedVerifiers.length === 0) return; setupMutation.mutate( { pageId, type, ...(!isQms && { mode, ...(mode === "period" && { periodAmount, periodUnit, }), ...(mode === "fixed" && fixedDate && { fixedExpiresAt: new Date(fixedDate).toISOString(), }), }), verifierIds: selectedVerifiers.map((v) => v.value), }, { onSuccess: () => { if (!isQms) { onClose(); } }, }, ); }; const periodValid = mode !== "period" || (Number.isInteger(periodAmount) && periodAmount >= PERIOD_AMOUNT_MIN && periodAmount <= PERIOD_UNIT_MAX_AMOUNT[periodUnit]); const fixedDateValid = mode !== "fixed" || (!!fixedDate && new Date(fixedDate).getTime() > Date.now()); const hasVerifiers = selectedVerifiers.length > 0; const canSubmit = isQms ? hasVerifiers : hasVerifiers && confirmed && periodValid && fixedDateValid; return (
{isQms ? ( ) : ( )}
{isQms ? t("Quality management") : t("Recurring")} {isQms ? t("Pages move through draft, approval, and approved stages.") : t( "Assigned verifiers must periodically re-verify this page.", )}
{t("Verifiers")} {selectedVerifiers.length > 0 && (
({ id: v.value, name: v.label, email: v.email, avatarUrl: v.avatarUrl, }))} canManage onRemove={handleRemoveVerifier} />
)} {canAddMore && ( v.value)} onSelect={handleAddVerifier} /> )}
{!isQms && ( <>
{t("Expiration")}
{t("Confirm")} setConfirmed(event.currentTarget.checked)} color="dark" />
)}
); }