import { type PrimitiveAtom, useAtom } from 'jotai'; import { equals, pick, type } from 'ramda'; import { useMemo } from 'react'; import { Modal } from '..'; export interface ConfirmationModalProps { atom: PrimitiveAtom; disabled?: boolean; hasCloseButton?: boolean; isDanger?: boolean; labels: { cancel: string | ((atom: Awaited | null) => string); confirm: string | ((atom: Awaited | null) => string); description: string | ((atom: Awaited | null) => string); title: string | ((atom: Awaited | null) => string); }; onCancel?: (atomData: Awaited | null) => void; onClose?: (atomData: Awaited | null) => void; onConfirm?: (atomData: Awaited | null) => void; size?: 'small' | 'medium' | 'large' | 'xlarge' | 'fullscreen'; } interface GetLabelProps { atomData: Awaited | null; label: string | ((atom: Awaited | null) => string); } const getLabel = ({ label, atomData }: GetLabelProps): string => equals(type(label), 'String') ? (label as string) : (label as (atom: Awaited | null) => string)(atomData); export const ConfirmationModal = ({ atom, labels, onConfirm, onCancel, onClose, hasCloseButton = true, isDanger, disabled, size }: ConfirmationModalProps): JSX.Element => { const [atomData, setAtomData] = useAtom(atom); const typedAtomData = atomData as Awaited | null; const closeModal = (): void => { onClose?.(typedAtomData); setAtomData(null); }; const formattedLabels = useMemo(() => { return { cancel: getLabel({ atomData: typedAtomData, label: labels.cancel }), confirm: getLabel({ atomData: typedAtomData, label: labels.confirm }), description: getLabel({ atomData: typedAtomData, label: labels.description }), title: getLabel({ atomData: typedAtomData, label: labels.title }) }; }, [labels, typedAtomData]); const confirm = (): void => { onConfirm?.(typedAtomData); setAtomData(null); }; const cancel = (): void => { onCancel?.(typedAtomData); setAtomData(null); }; return ( {formattedLabels.title} {formattedLabels.description} ); };