"use client"; import type { Metadata } from "next"; import { useState, useEffect } from "react"; import { useSession } from "next-auth/react"; import { useRouter } from "next/navigation"; import { Card, CardContent } from "@/components/ui/card"; import { FolderOpen, Plus, ChevronRight, Loader2, X } from "lucide-react"; interface Collection { id: number; name: string; description: string | null; createdAt: string; _count: { items: number }; } export const metadata: Metadata = { title: "我的收藏夹 - 追光AI", }; export default function CollectionsPage() { const { data: session, status } = useSession(); const router = useRouter(); const [collections, setCollections] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); const [showCreate, setShowCreate] = useState(false); const [newName, setNewName] = useState(""); const [newDesc, setNewDesc] = useState(""); const [creating, setCreating] = useState(false); const [createError, setCreateError] = useState(""); useEffect(() => { if (status === "loading") return; if (status === "unauthenticated") { setLoading(false); return; } fetchCollections(); }, [status]); const fetchCollections = () => { setLoading(true); setError(""); fetch("/api/user/collections") .then((r) => r.json()) .then((data) => setCollections(data.collections || [])) .catch(() => setError("加载收藏夹失败")) .finally(() => setLoading(false)); }; const handleCreate = async (e: React.FormEvent) => { e.preventDefault(); if (!newName.trim()) return; setCreating(true); setCreateError(""); try { const res = await fetch("/api/user/collections", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: newName.trim(), description: newDesc.trim() || undefined }), }); if (!res.ok) { const data = await res.json(); throw new Error(data.error || "创建失败"); } setShowCreate(false); setNewName(""); setNewDesc(""); fetchCollections(); } catch (err: any) { setCreateError(err.message); } finally { setCreating(false); } }; if (status === "loading") { return (
); } if (status === "unauthenticated") { return (

请先登录

登录后即可管理收藏夹

); } return (
我的收藏夹

我的收藏夹

{collections.length}
{showCreate && (

新建收藏夹

setNewName(e.target.value)} maxLength={50} required className="w-full px-3 py-2 bg-white rounded-lg border border-border/60 text-sm focus:outline-none focus:border-primary/50 focus:ring-2 focus:ring-primary/15 transition-colors" autoFocus /> setNewDesc(e.target.value)} className="w-full px-3 py-2 bg-white rounded-lg border border-border/60 text-sm focus:outline-none focus:border-primary/50 focus:ring-2 focus:ring-primary/15 transition-colors" /> {createError &&

{createError}

}
)} {error ? (

加载失败

{error}

) : loading ? (
{Array.from({ length: 4 }).map((_, i) => (
))}
) : collections.length === 0 ? (

暂无收藏夹

创建收藏夹来整理你的AI工具和技能

) : (
{collections.map((c, i) => ( router.push(`/user/collections/${c.id}`)} >

{c.name}

{c.description && (

{c.description}

)}
{c._count.items} 项 {new Date(c.createdAt).toLocaleDateString("zh-CN")} 创建
))}
)}
); }