import { forwardRef, Ref, useEffect, useImperativeHandle, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "~/components/ui/alert-dialog"; import { cn } from "~/lib/utils"; import { MailIcon } from "~/components/ui/icons/vendor"; export interface RefConfirmModal { setDestructive: (destructive: boolean) => void; setValue: (value: any) => void; show: (_question: string, _yesTitle?: string, _noTitle?: string, _description?: string, _inputString?: string) => void; } interface Props { destructive?: boolean; inputType?: string; onYes?: (value: any) => void; onNo?: () => void; placeholder?: string; contentUiId?: string; } const ConfirmModal = (props: Props, ref: Ref) => { const { t } = useTranslation(); const [open, setOpen] = useState(false); const [title, setTitle] = useState(); const [value, setValue] = useState(); const [description, setDescription] = useState(); const [inputString, setInputString] = useState(""); const [yesTitle, setYesTitle] = useState(""); const [noTitle, setNoTitle] = useState(""); const [isDestructive, setDestructive] = useState(props.destructive || false); const inputValue = useRef(null); useEffect(() => { setTitle(t("shared.confirm").toString()); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); function no() { setOpen(false); if (props.onNo) { props.onNo(); } } function yes() { setOpen(false); if (props.onYes) { props.onYes(value ?? inputString); } } useImperativeHandle(ref, () => ({ show, setValue, setDestructive })); function show( _question: string, _yesTitle: string = t("shared.confirm").toString(), _noTitle: string = t("shared.back").toString(), _description?: string, _inputString?: string ) { setTitle(_question.toString()); if (_yesTitle) { setYesTitle(_yesTitle); } if (_noTitle) { setNoTitle(_noTitle); } if (_description) { setDescription(_description); } setTimeout(() => { if ((props.inputType === "email" || props.inputType === "string" || props.inputType === "slug" || props.inputType === "password") && inputValue.current) { inputValue.current.focus(); inputValue.current.select(); } }, 0); setOpen(true); setInputString(_inputString); } return ( {title} {description && {description}}
{/* Subframe ConfirmDialog spacing handled by AlertDialogContent gap-6 grid; per-input top margins removed. */} {props.inputType === "email" && (
setInputString(e.target.value)} ref={inputValue} type="email" name="email" id="email" aria-label={title} className="border-border-default focus:border-brand-brand-border-solid-default block w-full rounded-md pl-10 focus:shadow-focus sm:text-sm" placeholder={props.placeholder ?? t("account.shared.emailPlaceholder")} />
)} {props.inputType === "string" && (
setInputString(e.target.value)} ref={inputValue} type="text" name="value" id="value" aria-label={title} className="border-border-default focus:border-brand-brand-border-solid-default block w-full rounded-md focus:shadow-focus sm:text-sm" placeholder={props.placeholder} />
)} {props.inputType === "slug" && (
setInputString(e.target.value.toLowerCase())} ref={inputValue} type="text" name="value" id="value" aria-label={title} className="border-border-default focus:border-brand-brand-border-solid-default block w-full rounded-md focus:shadow-focus sm:text-sm" placeholder={props.placeholder} />
)} {props.inputType === "password" && (
setInputString(e.target.value)} ref={inputValue} type="password" name="passwordCurrent" id="passwordCurrent" aria-label={title} className="border-border-default focus:border-brand-brand-border-solid-default block w-full rounded-md focus:shadow-focus sm:text-sm" placeholder={props.placeholder} />
)}
{noTitle} {yesTitle}
); }; export default forwardRef(ConfirmModal);