"use client"; /** * Template picker (slash "Insert template"): lists the workspace templates * and applies one — the server duplicates the template into the page's * parent, the caller links to the new page. */ import { useEffect, useState } from "react"; import { Wand2 } from "lucide-react"; import { templateApi } from "@/lib/api"; import { toast } from "@/stores/ui-store"; import type { TemplateInfo } from "@/lib/types"; export interface TemplateDialogProps { workspaceId: string; onClose: () => void; /** Invoked when the user picks a template; resolves when applied. */ onApply: (templateId: string, title: string) => Promise; } export function TemplateDialog({ workspaceId, onClose, onApply }: TemplateDialogProps) { const [templates, setTemplates] = useState([]); const [loading, setLoading] = useState(true); const [busyId, setBusyId] = useState(null); useEffect(() => { if (!workspaceId) { setLoading(false); return; } templateApi .list(workspaceId) .then(setTemplates) .catch(() => setTemplates([])) .finally(() => setLoading(false)); }, [workspaceId]); useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [onClose]); const apply = async (t: TemplateInfo) => { setBusyId(t.id); try { await onApply(t.id, t.name || "Untitled"); } catch (err) { toast("error", "Could not insert template", err instanceof Error ? err.message : undefined); } finally { setBusyId(null); } }; return (

Insert template

{loading ? (

Loading…

) : templates.length === 0 ? (

No templates yet — save one from the Templates page.

) : (
    {templates.map((t) => (
  • ))}
)}
); }