"use client" import { Bookmark, Edit2 } from "lucide-react" import { useEffect, useState } from "react" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Switch } from "@/components/ui/switch" import { Textarea } from "@/components/ui/textarea" import { useDictionary } from "@/hooks/use-dictionary" import { type Template, updateTemplate } from "@/lib/template-storage" interface TemplateEditDialogProps { open: boolean onOpenChange: (open: boolean) => void template: Template | null onSuccess: () => void } export function TemplateEditDialog({ open, onOpenChange, template, onSuccess, }: TemplateEditDialogProps) { const dict = useDictionary() const [title, setTitle] = useState("") const [description, setDescription] = useState("") const [prompt, setPrompt] = useState("") const [pinned, setPinned] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false) const [error, setError] = useState(null) // Populate form when template changes useEffect(() => { if (template) { setTitle(template.title || "") setDescription(template.description || "") setPrompt(template.prompt || "") setPinned(template.pinned || false) setError(null) } }, [template]) const handleOpenChange = (newOpen: boolean) => { if (!newOpen) { setError(null) } onOpenChange(newOpen) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!template) return const trimmedPrompt = prompt.trim() if (!trimmedPrompt) { setError(dict.templates.promptRequired) return } setIsSubmitting(true) setError(null) try { const updates: Partial> = { prompt: trimmedPrompt, title: title.trim() || template.title, description: description.trim() || undefined, pinned, } const updated = await updateTemplate(template.id, updates) if (updated) { onSuccess() onOpenChange(false) } else { setError(dict.templates.updateFailed) } } catch (err) { console.error("Failed to update template:", err) setError(dict.templates.updateFailed) } finally { setIsSubmitting(false) } } return (
{dict.templates.editTitle} {dict.templates.editDescription}
{/* Prompt field - required */}