"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { formatDistanceToNow } from "date-fns"; import { zhCN } from "date-fns/locale"; import { Trash2, FileImage, Loader2, Plus, X } from "lucide-react"; import { Button } from "@/components/ui/button"; import { ScrollArea } from "@/components/ui/scroll-area"; import { canvasService } from "@/services/canvas"; import { toast } from "sonner"; import { cn } from "@/lib/utils"; import type { CanvasProject } from "@/types/canvas"; interface CanvasFileListPanelProps { currentId: string; onClose: () => void; } export function CanvasFileListPanel({ currentId, onClose }: CanvasFileListPanelProps) { const router = useRouter(); const [projects, setProjects] = useState([]); const [isLoading, setIsLoading] = useState(true); // 加载项目列表 const loadProjects = async () => { setIsLoading(true); const result = await canvasService.getProjects(); if (result.success && result.data) { // 按更新时间降序排序 const sorted = result.data.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime() ); setProjects(sorted); } setIsLoading(false); }; useEffect(() => { loadProjects(); }, []); // 创建新画布 const handleCreateNew = async () => { const newId = crypto.randomUUID(); router.push(`/canvas/${newId}`); }; // 切换画布 const handleSwitch = (id: string) => { if (id === currentId) { onClose(); return; } const targetUrl = `/canvas/${id}`; onClose(); // 关闭文件列表面板 // 强制整页跳转,避免仅更新 URL 不刷新 setTimeout(() => { window.location.assign(targetUrl); }, 100); }; // 删除画布 const handleDelete = async (e: React.MouseEvent, id: string) => { e.stopPropagation(); if (!confirm("确定要删除这个画布吗?")) return; const result = await canvasService.deleteProject(id); if (result.success) { setProjects(prev => prev.filter(p => p.id !== id)); toast.success("删除成功"); // 如果删除的是当前画布,跳转到列表第一个或创建新的 if (id === currentId) { const next = projects.find(p => p.id !== id); if (next) { router.push(`/canvas/${next.id}`); } else { handleCreateNew(); } } } else { toast.error("删除失败"); } }; return (
{/* Header */}

已生成文件列表

{/* Content */}
{isLoading ? (
) : projects.length === 0 ? (

暂无画布历史

) : (
{projects.map((project) => (
handleSwitch(project.id)} className={cn( "group flex items-center gap-3 p-3 rounded-xl cursor-pointer transition-all", "hover:bg-muted/50", project.id === currentId ? "bg-primary/5 ring-1 ring-primary/20" : "bg-card" )} > {/* 缩略图 */}
{project.thumbnail ? ( {project.title} ) : (
)}
{/* 信息 */}

{project.title || "未命名画布"}

{formatDistanceToNow(new Date(project.updatedAt), { addSuffix: true, locale: zhCN })}

{/* 删除按钮 */}
))}
到底了
)}
); }