import { useState, useRef, useEffect } from "react"; import { useTranslation } from "react-i18next"; import { BookTemplate, Save, Trash2 } from "lucide-react"; import type { BudgetTemplate } from "../../shared/types"; interface TemplateActionsProps { templates: BudgetTemplate[]; onApply: (templateId: number) => void; onSave: (name: string, description?: string) => void; onDelete: (templateId: number) => void; disabled?: boolean; } export default function TemplateActions({ templates, onApply, onSave, onDelete, disabled, }: TemplateActionsProps) { const { t } = useTranslation(); const [showApply, setShowApply] = useState(false); const [showSave, setShowSave] = useState(false); const [templateName, setTemplateName] = useState(""); const applyRef = useRef(null); const saveRef = useRef(null); useEffect(() => { if (!showApply && !showSave) return; const handler = (e: MouseEvent) => { if (showApply && applyRef.current && !applyRef.current.contains(e.target as Node)) { setShowApply(false); } if (showSave && saveRef.current && !saveRef.current.contains(e.target as Node)) { setShowSave(false); } }; document.addEventListener("mousedown", handler); return () => document.removeEventListener("mousedown", handler); }, [showApply, showSave]); const handleSave = () => { if (!templateName.trim()) return; onSave(templateName.trim()); setTemplateName(""); setShowSave(false); }; const handleDelete = (e: React.MouseEvent, templateId: number) => { e.stopPropagation(); if (confirm(t("budget.deleteTemplateConfirm"))) { onDelete(templateId); } }; return (
{/* Apply template */}
{showApply && (
{templates.length === 0 ? (

{t("budget.noTemplates")}

) : ( templates.map((tmpl) => (
{ onApply(tmpl.id); setShowApply(false); }} > {tmpl.name}
)) )}
)}
{/* Save as template */}
{showSave && (
setTemplateName(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") handleSave(); }} placeholder={t("budget.templateName")} className="w-full bg-[var(--background)] border border-[var(--border)] rounded-lg px-3 py-1.5 text-sm focus:outline-none focus:ring-1 focus:ring-[var(--primary)] mb-3" autoFocus />
)}
); }