241 lines
11 KiB
TypeScript
241 lines
11 KiB
TypeScript
"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<Collection[]>([]);
|
|
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 (
|
|
<div className="min-h-screen flex items-center justify-center">
|
|
<div className="h-8 w-8 animate-spin rounded-full border-2 border-primary border-t-transparent" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (status === "unauthenticated") {
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center px-4">
|
|
<Card className="w-full max-w-md p-8 text-center rounded-2xl border border-border/60 bg-white shadow-[0_4px_16px_rgba(0,0,0,0.06)]">
|
|
<div className="mx-auto mb-4 flex h-16 w-16 items-center justify-center rounded-2xl bg-primary/10 border border-primary/20">
|
|
<FolderOpen className="w-7 h-7 text-primary" />
|
|
</div>
|
|
<h2 className="text-xl font-bold">请先登录</h2>
|
|
<p className="mt-2 text-sm text-muted-foreground">登录后即可管理收藏夹</p>
|
|
<button onClick={() => router.push("/login")} className="mt-6 w-full py-2.5 bg-primary text-white rounded-lg hover:bg-primary/90 transition-colors">
|
|
去登录
|
|
</button>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="relative">
|
|
<div className="pointer-events-none absolute inset-0 overflow-hidden">
|
|
<div className="absolute -top-32 left-1/4 h-[500px] w-[600px] rounded-full bg-primary/4 blur-[120px]" />
|
|
</div>
|
|
|
|
<div className="relative mx-auto max-w-4xl px-4 py-8 sm:px-6 lg:px-8">
|
|
<div className="mb-8 animate-fade-in">
|
|
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
|
<button onClick={() => router.push("/")} className="transition-colors hover:text-foreground">首页</button>
|
|
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path strokeLinecap="round" strokeLinejoin="round" d="M9 5l7 7-7 7" /></svg>
|
|
<span className="text-foreground">我的收藏夹</span>
|
|
</div>
|
|
<div className="mt-3 flex items-center justify-between">
|
|
<div className="flex items-center gap-3">
|
|
<h1 className="text-3xl font-bold tracking-tight">
|
|
我的<span className="gradient-text">收藏夹</span>
|
|
</h1>
|
|
<span className="rounded-full bg-primary/10 px-2.5 py-0.5 text-sm font-medium text-primary">
|
|
{collections.length}
|
|
</span>
|
|
</div>
|
|
<button
|
|
onClick={() => { setShowCreate(true); setCreateError(""); }}
|
|
className="flex items-center gap-1.5 rounded-lg bg-primary px-4 py-2 text-sm font-medium text-white hover:bg-primary/90 transition-colors shadow-sm shadow-primary/20"
|
|
>
|
|
<Plus className="w-4 h-4" />
|
|
新建收藏夹
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{showCreate && (
|
|
<Card className="mb-6 border-border/60 bg-white shadow-[0_1px_3px_rgba(0,0,0,0.06)]">
|
|
<CardContent className="p-5">
|
|
<form onSubmit={handleCreate} className="space-y-4">
|
|
<div className="flex items-center justify-between">
|
|
<h3 className="font-semibold text-sm">新建收藏夹</h3>
|
|
<button type="button" onClick={() => setShowCreate(false)} className="p-1 rounded-lg text-muted-foreground hover:bg-accent transition-colors">
|
|
<X className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
<input
|
|
type="text"
|
|
placeholder="收藏夹名称"
|
|
value={newName}
|
|
onChange={(e) => 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
|
|
/>
|
|
<input
|
|
type="text"
|
|
placeholder="描述(选填)"
|
|
value={newDesc}
|
|
onChange={(e) => 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 && <p className="text-sm text-red-500">{createError}</p>}
|
|
<button
|
|
type="submit"
|
|
disabled={creating || !newName.trim()}
|
|
className="w-full py-2 bg-primary text-white rounded-lg text-sm font-medium hover:bg-primary/90 transition-colors disabled:opacity-50"
|
|
>
|
|
{creating ? <Loader2 className="w-4 h-4 animate-spin mx-auto" /> : "创建"}
|
|
</button>
|
|
</form>
|
|
</CardContent>
|
|
</Card>
|
|
)}
|
|
|
|
{error ? (
|
|
<div className="flex flex-col items-center justify-center py-24">
|
|
<div className="flex h-16 w-16 items-center justify-center rounded-2xl bg-red-50 border border-red-200/60">
|
|
<svg className="w-7 h-7 text-red-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={1.5}>
|
|
<path strokeLinecap="round" strokeLinejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126ZM12 15.75h.007v.008H12v-.008Z" />
|
|
</svg>
|
|
</div>
|
|
<p className="mt-4 text-sm font-medium text-foreground">加载失败</p>
|
|
<p className="mt-1 text-sm text-muted-foreground">{error}</p>
|
|
<button onClick={fetchCollections} className="mt-4 rounded-lg bg-primary px-4 py-2 text-sm text-white hover:bg-primary/90 transition-colors">重新加载</button>
|
|
</div>
|
|
) : loading ? (
|
|
<div className="grid gap-3 sm:grid-cols-2">
|
|
{Array.from({ length: 4 }).map((_, i) => (
|
|
<Card key={i} className="border-border/50 bg-white">
|
|
<CardContent className="p-5">
|
|
<div className="h-5 w-32 animate-pulse rounded bg-muted/40 mb-2" />
|
|
<div className="h-3 w-48 animate-pulse rounded bg-muted/30" />
|
|
</CardContent>
|
|
</Card>
|
|
))}
|
|
</div>
|
|
) : collections.length === 0 ? (
|
|
<div className="flex flex-col items-center justify-center py-24">
|
|
<div className="flex h-20 w-20 items-center justify-center rounded-2xl bg-muted/30 border border-border/30">
|
|
<FolderOpen className="w-8 h-8 text-muted-foreground/40" />
|
|
</div>
|
|
<p className="mt-5 text-base font-medium text-muted-foreground">暂无收藏夹</p>
|
|
<p className="mt-1 text-sm text-muted-foreground/50">创建收藏夹来整理你的AI工具和技能</p>
|
|
<button onClick={() => setShowCreate(true)} className="mt-4 flex items-center gap-1.5 rounded-lg border border-border/60 px-4 py-2 text-sm text-muted-foreground transition-colors hover:bg-muted/30 hover:text-foreground">
|
|
<Plus className="w-4 h-4" />
|
|
新建收藏夹
|
|
</button>
|
|
</div>
|
|
) : (
|
|
<div className="grid gap-3 sm:grid-cols-2">
|
|
{collections.map((c, i) => (
|
|
<Card
|
|
key={c.id}
|
|
className="group cursor-pointer border-border/50 bg-white transition-all duration-300 hover:border-primary/30 hover:shadow-[0_4px_12px_rgba(0,0,0,0.06)] hover:-translate-y-0.5 shadow-[0_1px_2px_rgba(0,0,0,0.04)] animate-slide-up"
|
|
style={{ animationDelay: `${i * 50}ms`, animationFillMode: "both" }}
|
|
onClick={() => router.push(`/user/collections/${c.id}`)}
|
|
>
|
|
<CardContent className="p-5">
|
|
<div className="flex items-start justify-between">
|
|
<div className="min-w-0">
|
|
<h3 className="font-semibold text-foreground truncate group-hover:text-primary transition-colors">{c.name}</h3>
|
|
{c.description && (
|
|
<p className="mt-1 text-sm text-muted-foreground truncate">{c.description}</p>
|
|
)}
|
|
<div className="mt-2 flex items-center gap-3 text-xs text-muted-foreground/60">
|
|
<span>{c._count.items} 项</span>
|
|
<span>{new Date(c.createdAt).toLocaleDateString("zh-CN")} 创建</span>
|
|
</div>
|
|
</div>
|
|
<ChevronRight className="w-4 h-4 text-muted-foreground/0 group-hover:text-muted-foreground/50 transition-all mt-0.5" />
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
} |