"use client"; import { LayoutTemplateIcon } from "lucide-react"; import { useRouter } from "next/navigation"; import { useState, useTransition } from "react"; import { toast } from "sonner"; import { applyEditorTemplate } from "@/app/admin/actions"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Field, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Spinner } from "@/components/ui/spinner"; import { Textarea } from "@/components/ui/textarea"; import type { TemplateBlueprint } from "@/lib/templates"; export function TemplateGallery({ templates }: { templates: readonly TemplateBlueprint[] }) { const router = useRouter(); const [selected, setSelected] = useState(null); const [pending, startTransition] = useTransition(); function submit(formData: FormData) { if (!selected) return; startTransition(async () => { const result = await applyEditorTemplate({ templateSlug: selected.slug, characterSlug: String(formData.get("slug") ?? ""), nameTh: String(formData.get("nameTh") ?? ""), nameEn: String(formData.get("nameEn") ?? ""), description: String(formData.get("description") ?? ""), }); if (result.status === "created") { toast.success("สร้างคู่มือจากเทมเพลตแล้ว"); router.push(result.location); router.refresh(); } else if (result.status === "validation-error") { toast.error(result.issues[0] ?? "ข้อมูลไม่ถูกต้อง"); } else { toast.error("สร้างคู่มือไม่สำเร็จ โปรดตรวจสอบ slug ที่ซ้ำกัน"); } }); } return ( <>
{templates.map((template) => (
{template.name} {template.description}
{template.pages.length} หน้า {template.dataSources.length} แหล่งข้อมูล {template.category}
))}
{ if (!open) setSelected(null); }}> สร้างจาก {selected?.name} ระบบจะสร้างตัวละคร หน้า บล็อก และแหล่งข้อมูลแยกอิสระ โดยเริ่มต้นในสถานะซ่อน
ชื่อภาษาไทย ชื่อภาษาอังกฤษ Slugใช้ใน URL และเปลี่ยนภายหลังได้โดยลิงก์เดิมยังเปลี่ยนเส้นทางให้ คำอธิบาย