"use client"; import Image from "next/image"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { PlusIcon, RefreshCwIcon, RotateCcwIcon, XCircleIcon, Trash2Icon, } from "lucide-react"; import { useEffect, useState, useTransition } from "react"; import { toast } from "sonner"; import { deleteStructuredGuide, cancelCatalogSync, getCatalogSyncProgress, moveGuideToTrash, restoreStructuredGuide, startCatalogSync, } from "@/app/admin/actions"; import { DeleteConfirmationDialog } from "@/components/admin/delete-confirmation-dialog"; import { GuideVisibilityControl } from "@/components/admin/guide-visibility-control"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardAction, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyTitle, } from "@/components/ui/empty"; import { Spinner } from "@/components/ui/spinner"; import { useAdminPresence } from "@/hooks/use-presence"; import type { CatalogSyncJob } from "@/lib/catalog/sync"; import { cn } from "@/lib/utils"; type GuideCard = { id: string; characterKey: string; name: string; overview: string; coverUrl: string; version: number; updatedAt: Date; trashedAt: Date | null; isPublic: boolean; }; export function StructuredDashboard({ guides, trash, catalogVersion, latestCatalogVersion, }: { guides: GuideCard[]; trash: GuideCard[]; catalogVersion: string | null; latestCatalogVersion: string | null; }) { const router = useRouter(); const [pending, startTransition] = useTransition(); const [syncJob, setSyncJob] = useState(null); const presence = useAdminPresence(null); const catalogReady = Boolean(catalogVersion); const updateAvailable = Boolean( catalogVersion && latestCatalogVersion && catalogVersion !== latestCatalogVersion, ); const syncJobId = syncJob?.id; const syncJobStatus = syncJob?.status; useEffect(() => { if (!syncJobId || !syncJobStatus || ["completed", "unchanged", "cancelled", "error"].includes(syncJobStatus)) return; let stopped = false; const poll = async () => { const latest = await getCatalogSyncProgress(syncJobId); if (!stopped && latest) setSyncJob(latest); }; void poll(); const timer = window.setInterval(() => void poll(), 1000); return () => { stopped = true; window.clearInterval(timer); }; }, [syncJobId, syncJobStatus]); function startSync() { startTransition(async () => { const result = await startCatalogSync(); if (result.status === "error") toast.error(result.message); else setSyncJob(result.job); }); } function cancelSync() { if (!syncJob) return; startTransition(async () => { const result = await cancelCatalogSync(syncJob.id); if (result) setSyncJob(result); }); } function run( action: () => Promise<{ status: string; message?: string }>, success: string, ) { startTransition(async () => { const result = await action(); if (result.status === "error") toast.error(result.message); else { toast.success(success); router.refresh(); } }); } return (
Lunaris ที่ Sync:{" "} {catalogVersion ? `v${catalogVersion}` : "ยังไม่ Sync"} {latestCatalogVersion ? updateAvailable ? `มีอัปเดต: v${latestCatalogVersion}` : `ล่าสุด: v${latestCatalogVersion}` : "ตรวจสอบเวอร์ชันล่าสุดไม่ได้"}
{syncJob && !["completed", "unchanged", "cancelled", "error"].includes(syncJob.status) ? ( ) : null}
{syncJob ? (
{syncJob.message} {syncJob.total ? `${syncJob.completed}/${syncJob.total}` : "กำลังเริ่ม…"}
{syncJob.status === "error" ?

{syncJob.error ?? "Sync ไม่สำเร็จ"}

: null} ) : null} {guides.length ? (
{guides.map((guide) => ( แก้ไข Guide {guide.name}
{`Guide
{guide.name} {guide.overview}
v{guide.version} {(presence?.editorsByGuide[guide.id] ?? []).map((editor) => ( {editor.name} {editor.sections.length ? ` · ${editor.sections.join(", ")}` : ""} ))}

แก้ไขล่าสุด{" "} {new Date(guide.updatedAt).toLocaleString("th-TH")}

} title={`ย้าย ${guide.name} ไปถังขยะ?`} description="Guide จะหายจากหน้าเว็บไซต์ทันที และสามารถกู้คืนได้ภายหลัง" confirmLabel="ย้ายไปถังขยะ" onConfirm={() => run(() => moveGuideToTrash(guide.id), "ลบสำเร็จ!") } > ย้าย {guide.name} ไปถังขยะ
))}
) : ( ยังไม่มี Guide Sync Catalog แล้วเลือกตัวละครเพื่อสร้าง Guide แรก )} {trash.length ? (

ถังขยะ

Guide ที่ซ่อนไว้จากเว็บไซต์

{trash.map((guide) => ( {guide.name} {guide.overview}
} title={`ลบ ${guide.name} ถาวร?`} description="ข้อมูล Weapons, Artifacts, Constellations, Team และ Extra ทั้งหมดจะกู้คืนไม่ได้" confirmLabel="ยืนยันลบถาวร" onConfirm={() => run( () => deleteStructuredGuide(guide.id), "ลบสำเร็จ!", ) } > ลบถาวร
))}
) : null}
); }