import { useState } from 'react' import { Link } from 'react-router' import { useQuery } from '@tanstack/react-query' import { Pin } from 'lucide-react' import { api } from '../lib/api' import type { NoteItem, NoteList, TagCount } from '../lib/api' import { formatDate } from '../lib/utils' function useNotes(page: number, tag?: string) { // 不传 page_size:服务端默认值即站点设置(管理员可改),首页随设置生效 const qs = new URLSearchParams({ page: String(page) }) if (tag) qs.set('tag', tag) return useQuery({ queryKey: ['notes', page, tag ?? ''], queryFn: () => api(`/api/notes?${qs}`), }) } function useTags() { return useQuery({ queryKey: ['tags'], queryFn: () => api<{ tags: TagCount[] }>('/api/tags'), }) } function NoteCard({ note }: { note: NoteItem }) { return (
{note.pinned && ( 置顶 )}

{note.title}

{note.summary && (

{note.summary}

)} {note.tags.length > 0 && (
{note.tags.map((t) => ( #{t} ))}
)}
) } export default function Home() { const [page, setPage] = useState(1) const { data, isLoading } = useNotes(page) const { data: tagsData } = useTags() const totalPages = data ? Math.max(1, Math.ceil(data.total / data.page_size)) : 1 return (
{isLoading ? (

加载中…

) : !data || data.items.length === 0 ? (

还没有公开的笔记

) : ( data.items.map((n) => ) )} {totalPages > 1 && ( )}
) }