"use client"; import { useEffect, useId, useRef, useState } from "react"; import { useFocusTrap } from "@/hooks/use-focus-trap"; import { useTranslations } from "next-intl"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; interface PromptDialogProps { isOpen: boolean; onClose: () => void; onSubmit: (value: string) => void; title: string; message?: string; placeholder?: string; defaultValue?: string; confirmText?: string; cancelText?: string; } export function PromptDialog({ isOpen, onClose, onSubmit, title, message, placeholder, defaultValue = "", confirmText, cancelText, }: PromptDialogProps) { const t = useTranslations("confirm_dialog"); const id = useId(); const [value, setValue] = useState(defaultValue); const inputRef = useRef(null); const dialogRef = useFocusTrap({ isActive: isOpen, onEscape: onClose, restoreFocus: true, }); useEffect(() => { if (isOpen) { setValue(defaultValue); const t = setTimeout(() => { inputRef.current?.focus(); inputRef.current?.select(); }, 50); return () => clearTimeout(t); } }, [isOpen, defaultValue]); useEffect(() => { if (!isOpen) return; const handleBackdropClick = (e: MouseEvent) => { if (dialogRef.current && !dialogRef.current.contains(e.target as Node)) { onClose(); } }; document.addEventListener("mousedown", handleBackdropClick); return () => document.removeEventListener("mousedown", handleBackdropClick); }, [isOpen, onClose, dialogRef]); if (!isOpen) return null; const resolvedConfirmText = confirmText || t("confirm"); const resolvedCancelText = cancelText || t("cancel"); const trimmed = value.trim(); const canSubmit = trimmed.length > 0; const handleSubmit = (e?: React.FormEvent) => { e?.preventDefault(); if (!canSubmit) return; try { onSubmit(trimmed); } finally { onClose(); } }; return (

{title}

{message && (

{message}

)} setValue(e.target.value)} placeholder={placeholder} className="mt-4" />
); }