"use client"; import { useCallback, useEffect, useState } from "react"; import { BookOpen, Grid3X3, LayoutList, Loader2, MoreHorizontal, Plus, RefreshCw, } from "lucide-react"; import { useNavigate } from "react-router-dom"; import { useTheme } from "@/contexts/ThemeContext"; import { listSearchSpaces, type SearchSpace } from "@/core/note/api"; import { useNoteAuth } from "@/hooks/useNoteAuth"; import { cn } from "@/lib/utils"; type ViewMode = "grid" | "list"; function formatCreatedAt(value: string): string { const date = new Date(value); if (Number.isNaN(date.getTime())) { return value; } return date.toLocaleDateString("zh-CN", { year: "numeric", month: "2-digit", day: "2-digit", }); } function getSourceCount(note: SearchSpace): number { return note.source_count ?? note.sources_used ?? 0; } const cardThemes = [ "bg-[#e4f2f6]", "bg-[#f0eaf3]", "bg-[#edf4e6]", "bg-[#f5eadf]", "bg-[#e9edf8]", ]; function NoteIcon({ isDark }: { isDark: boolean }) { return (
); } export default function LightAppNoteListPage() { const { theme } = useTheme(); const isDark = theme === "dark"; const navigate = useNavigate(); const { loading: authLoading, error: authError } = useNoteAuth(); const [notes, setNotes] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [viewMode, setViewMode] = useState("grid"); const loadNotes = useCallback(async () => { setLoading(true); setError(null); try { const data = await listSearchSpaces({ owned_only: true, limit: 1000, order_by: "created_at", }); setNotes(data); } catch (err) { const message = err instanceof Error ? err.message : "获取笔记列表失败,请稍后重试"; setError(message); } finally { setLoading(false); } }, []); useEffect(() => { if (authLoading || authError) { return; } void loadNotes(); }, [authError, authLoading, loadNotes]); const isBusy = authLoading || loading; const createButtonClass = cn( "group text-center transition-all hover:-translate-y-0.5", isDark ? "border-slate-700 bg-slate-900/70 text-slate-300 hover:border-slate-500 hover:bg-slate-900" : "border-slate-200 bg-white text-slate-600 shadow-sm hover:border-sky-200 hover:shadow-md", ); const renderCreateButton = (layout: ViewMode) => { if (layout === "grid") { return ( ); } return ( ); }; const renderNoteCard = (note: SearchSpace, index: number) => (

{note.name}

{formatCreatedAt(note.created_at)} · {getSourceCount(note)} 个来源

); const renderNoteRow = (note: SearchSpace) => (

{note.name}

{formatCreatedAt(note.created_at)} · {getSourceCount(note)} 个来源

); return (

我的笔记本

{authError && (
{authError}
)} {error && !authError && (
{error}
)} {isBusy && !authError && (
)} {!isBusy && !authError && !error && viewMode === "grid" && (
{renderCreateButton("grid")} {notes.map((note, index) => renderNoteCard(note, index))} {notes.length === 0 && (
暂无笔记
)}
)} {!isBusy && !authError && !error && viewMode === "list" && (
{renderCreateButton("list")} {notes.map((note) => renderNoteRow(note))} {notes.length === 0 && (
暂无笔记
)}
)}
); }