feat(ui): shelf rebuilt — sticky toolbar (search/sort/group in URL), rail, menu cards, alert-dialog delete
This commit is contained in:
+225
-210
@@ -1,88 +1,84 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { api } from "../api/client";
|
||||
import type { Book } from "../api/types";
|
||||
import { useAuth } from "../auth/AuthContext";
|
||||
import { Cover } from "../components/Cover";
|
||||
import { IconAlert, IconBookOpen, IconClock, IconFolder, IconRefresh, IconScan, IconSearch, IconX } from "../components/icons";
|
||||
import { TopBar } from "../components/TopBar";
|
||||
import { toast } from "../components/Toaster";
|
||||
import { btn, card, formatBadge, formatSize, input, pill, pillActive } from "../components/ui";
|
||||
import { groupByDir } from "../lib/group";
|
||||
import { useNavigate, useSearchParams } from "react-router-dom";
|
||||
import { ArrowDownUp, Ellipsis, FolderOpen, LibraryBig, RefreshCw, ScanLine, Search, TriangleAlert } from "lucide-react";
|
||||
import { api } from "@/api/client";
|
||||
import type { Book } from "@/api/types";
|
||||
import { useAuth } from "@/auth/AuthContext";
|
||||
import { Cover } from "@/components/Cover";
|
||||
import { toast } from "@/components/Toaster";
|
||||
import { formatBadge } from "@/components/ui";
|
||||
import {
|
||||
AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent,
|
||||
AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle,
|
||||
} from "@/components/ui/alert-dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
import { formatSize } from "@/lib/format";
|
||||
import { sections, SORT_LABEL, type GroupMode, type SortKey } from "@/lib/shelf";
|
||||
import { GROUPS, LIB0, SORTS } from "@/lib/shelfMeta";
|
||||
|
||||
function BookCard({
|
||||
book,
|
||||
index,
|
||||
onOpen,
|
||||
onDelete,
|
||||
}: {
|
||||
book: Book;
|
||||
index: number;
|
||||
onOpen: () => void;
|
||||
onDelete: () => void;
|
||||
}) {
|
||||
const GRID = "grid grid-cols-[repeat(auto-fill,minmax(7.5rem,1fr))] gap-x-4 gap-y-6 sm:grid-cols-[repeat(auto-fill,minmax(9.5rem,1fr))]";
|
||||
|
||||
function BookCard({ book, index, onOpen, onDelete }: { book: Book; index: number; onOpen: () => void; onDelete: () => void }) {
|
||||
const { isAdmin } = useAuth();
|
||||
const pct = Math.round(book.percent * 100);
|
||||
return (
|
||||
<div
|
||||
className="group fx-rise relative flex flex-col"
|
||||
style={{ animationDelay: `${Math.min(index, 23) * 25}ms` }}
|
||||
>
|
||||
<button onClick={onOpen} className="block w-full text-left focus-visible:outline-none">
|
||||
<div className="relative aspect-[3/4] w-full overflow-hidden rounded-lg shadow-md ring-1 ring-white/10 transition duration-200 will-change-transform group-hover:-translate-y-1 group-hover:shadow-xl group-hover:shadow-black/40 group-hover:ring-amber-500/40 group-focus-visible:-translate-y-1 group-focus-visible:ring-amber-500">
|
||||
<div className="group fx-rise relative flex flex-col" style={{ animationDelay: `${Math.min(index, 23) * 25}ms` }}>
|
||||
<button onClick={onOpen} className="block w-full p-0 text-left focus-visible:outline-none">
|
||||
<span className="relative block aspect-[3/4] overflow-hidden rounded-md border bg-muted shadow-sm transition group-hover:shadow-md group-hover:ring-2 group-hover:ring-accent/50">
|
||||
<Cover book={book} className="size-full" />
|
||||
<span className={formatBadge(book.format, "absolute left-1.5 top-1.5 backdrop-blur-sm")}>{book.format}</span>
|
||||
<span className={formatBadge(book.format, "absolute left-1.5 top-1.5")}>{book.format}</span>
|
||||
{book.state === "error" && (
|
||||
<span
|
||||
className="absolute right-1.5 top-1.5 rounded bg-red-950/90 px-1.5 py-0.5 text-[10px] font-semibold text-red-200 ring-1 ring-red-500/40"
|
||||
title={book.error}
|
||||
>
|
||||
损坏
|
||||
<span className="absolute right-1.5 top-1.5 flex items-center gap-1 rounded bg-destructive px-1.5 py-0.5 text-[10px] font-semibold text-primary-foreground" title={book.error}>
|
||||
<TriangleAlert className="size-3" /> 损坏
|
||||
</span>
|
||||
)}
|
||||
<span className="pointer-events-none absolute inset-x-0 bottom-0 flex items-end justify-between gap-1 bg-linear-to-t from-black/80 via-black/30 to-transparent p-1.5 pt-6 text-[10px] tabular-nums text-stone-200 opacity-0 transition-opacity duration-200 group-hover:opacity-100 group-focus-visible:opacity-100">
|
||||
<span>{book.pages > 0 ? `${book.pages} 页` : ""}</span>
|
||||
<span>{formatSize(book.size)}</span>
|
||||
</span>
|
||||
{pct > 0 && (
|
||||
<span className="absolute inset-x-0 bottom-0 h-1 bg-black/50" role="presentation">
|
||||
<span className="block h-full bg-amber-500" style={{ width: `${pct}%` }} />
|
||||
<span className="absolute inset-x-0 bottom-0 h-1 bg-black/30" role="presentation">
|
||||
<span className="block h-full bg-accent" style={{ width: `${pct}%` }} />
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<p className="mt-2 truncate text-xs font-medium text-stone-200 transition-colors group-hover:text-amber-300 group-focus-visible:text-amber-300">
|
||||
{book.title}
|
||||
</p>
|
||||
<p className="truncate text-[10px] text-stone-500">{book.path.includes("/") ? book.path.slice(0, book.path.lastIndexOf("/")) : "根目录"}</p>
|
||||
</span>
|
||||
<span className="mt-2 block truncate text-xs font-medium">{book.title}</span>
|
||||
<span className="block truncate text-[11px] text-muted-foreground">
|
||||
{book.path.includes("/") ? book.path.slice(0, book.path.lastIndexOf("/")) : `${book.pages > 0 ? book.pages + " 页 · " : ""}${formatSize(book.size)}`}
|
||||
</span>
|
||||
</button>
|
||||
{isAdmin && (
|
||||
<button
|
||||
className="absolute right-1.5 top-1.5 z-10 rounded-md bg-stone-950/80 p-1 text-stone-300 opacity-0 ring-1 ring-white/10 backdrop-blur-sm transition hover:bg-red-950 hover:text-red-300 focus-visible:opacity-100 group-hover:opacity-100"
|
||||
title="删除(连磁盘文件)"
|
||||
aria-label={`删除《${book.title}》`}
|
||||
onClick={() => {
|
||||
if (confirm(`删除《${book.title}》?磁盘文件一并删除,阅读进度保留`)) onDelete();
|
||||
}}
|
||||
>
|
||||
<IconX className="size-3.5" />
|
||||
</button>
|
||||
)}
|
||||
<div className="absolute right-1 top-1 z-10 opacity-0 transition-opacity focus-within:opacity-100 group-hover:opacity-100">
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="secondary" size="icon" className="size-7 bg-background/80 backdrop-blur" aria-label={`《${book.title}》操作`}>
|
||||
<Ellipsis className="size-4" />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onClick={onOpen}>打开</DropdownMenuItem>
|
||||
{isAdmin && (
|
||||
<>
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem className="text-destructive focus:text-destructive" onClick={onDelete}>
|
||||
删除
|
||||
</DropdownMenuItem>
|
||||
</>
|
||||
)}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const GRID = "grid grid-cols-[repeat(auto-fill,minmax(7.5rem,1fr))] gap-x-4 gap-y-6 sm:grid-cols-[repeat(auto-fill,minmax(9rem,1fr))]";
|
||||
|
||||
function Section({ title, icon, children }: { title: string; icon?: React.ReactNode; children: React.ReactNode }) {
|
||||
function SectionHead({ label, count }: { label: string; count: number }) {
|
||||
if (!label) return null;
|
||||
return (
|
||||
<section className="mb-10">
|
||||
<h2 className="mb-3 flex items-center gap-1.5 text-sm font-semibold tracking-wide text-stone-300">
|
||||
{icon}
|
||||
{title}
|
||||
</h2>
|
||||
{children}
|
||||
</section>
|
||||
<h2 className="sticky top-0 z-10 mb-3 flex items-center gap-2 bg-background/90 py-1.5 text-sm font-medium backdrop-blur">
|
||||
<FolderOpen className="size-4 text-muted-foreground" />
|
||||
{label}
|
||||
<span className="text-xs tabular-nums text-muted-foreground">{count}</span>
|
||||
</h2>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -90,23 +86,30 @@ export default function Shelf() {
|
||||
const qc = useQueryClient();
|
||||
const nav = useNavigate();
|
||||
const { isAdmin } = useAuth();
|
||||
const [lib, setLib] = useState(0); // 0 = 全部库
|
||||
const [qRaw, setQRaw] = useState("");
|
||||
const [q, setQ] = useState("");
|
||||
const [sp, setSp] = useSearchParams();
|
||||
const lib = Number(sp.get("lib")) || LIB0;
|
||||
const sort = (SORTS.includes(sp.get("sort") as SortKey) ? (sp.get("sort") as SortKey) : "added") satisfies SortKey;
|
||||
const group: GroupMode = sp.get("group") === "dir" ? "dir" : "flat";
|
||||
const serverQ = (sp.get("q") ?? "").trim();
|
||||
const [qRaw, setQRaw] = useState(serverQ);
|
||||
const [confirmDel, setConfirmDel] = useState<Book | null>(null);
|
||||
|
||||
useEffect(() => setQRaw(serverQ), [serverQ]);
|
||||
useEffect(() => {
|
||||
const t = setTimeout(() => setQ(qRaw.trim()), 300); // 搜索防抖
|
||||
if (qRaw.trim() === serverQ) return;
|
||||
const t = setTimeout(() => {
|
||||
const next = new URLSearchParams(sp);
|
||||
if (qRaw.trim()) next.set("q", qRaw.trim());
|
||||
else next.delete("q");
|
||||
setSp(next, { replace: true });
|
||||
}, 300);
|
||||
return () => clearTimeout(t);
|
||||
}, [qRaw]);
|
||||
}, [qRaw]); // eslint-disable-line
|
||||
|
||||
const libsQ = useQuery({ queryKey: ["libraries"], queryFn: api.listLibraries });
|
||||
const booksQ = useQuery({
|
||||
queryKey: ["books", lib, q],
|
||||
queryFn: () => api.listBooks({ library: lib || undefined, q }),
|
||||
});
|
||||
const booksQ = useQuery({ queryKey: ["books", lib, serverQ], queryFn: () => api.listBooks({ library: lib || undefined, q: serverQ }) });
|
||||
const progQ = useQuery({ queryKey: ["progress"], queryFn: api.listProgress });
|
||||
// 继续阅读行 → book id 解析用(键与 booksQ 默认态一致,天然共享缓存)
|
||||
const allQ = useQuery({ queryKey: ["books", 0, ""], queryFn: () => api.listBooks() });
|
||||
const allQ = useQuery({ queryKey: ["books", LIB0, ""], queryFn: () => api.listBooks() });
|
||||
|
||||
const del = useMutation({
|
||||
mutationFn: (id: number) => api.deleteBook(id),
|
||||
@@ -120,162 +123,174 @@ export default function Shelf() {
|
||||
mutationFn: (id: number) => api.scanLibrary(id),
|
||||
onSuccess: () => {
|
||||
toast("ok", "扫描已触发");
|
||||
setTimeout(() => qc.invalidateQueries({ queryKey: ["books"] }), 3000); // 给周期扫描留出时间
|
||||
setTimeout(() => qc.invalidateQueries({ queryKey: ["books"] }), 3000);
|
||||
},
|
||||
onError: (e) => toast("err", e instanceof Error ? e.message : "扫描失败"),
|
||||
});
|
||||
|
||||
const groups = useMemo(() => groupByDir(booksQ.data ?? []), [booksQ.data]);
|
||||
const booksByKey = useMemo(() => {
|
||||
const m = new Map<string, Book>();
|
||||
for (const b of allQ.data ?? []) m.set(`${b.library_id}/${b.path}`, b);
|
||||
return m;
|
||||
}, [allQ.data]);
|
||||
|
||||
const secs = useMemo(() => sections(booksQ.data ?? [], sort, group), [booksQ.data, sort, group]);
|
||||
const searching = serverQ.length > 0;
|
||||
const continueRows = (progQ.data ?? []).slice(0, 12);
|
||||
const libs = libsQ.data ?? [];
|
||||
const searching = q.length > 0;
|
||||
const setParam = (k: string, v: string) => {
|
||||
const next = new URLSearchParams(sp);
|
||||
if (v) next.set(k, v);
|
||||
else next.delete(k);
|
||||
setSp(next, { replace: true });
|
||||
};
|
||||
const libName = lib === LIB0 ? "全部" : libsQ.data?.find((l) => l.id === lib)?.name ?? "书库";
|
||||
|
||||
return (
|
||||
<div className="flex h-full flex-col">
|
||||
<TopBar
|
||||
right={
|
||||
<>
|
||||
<div className="relative">
|
||||
<IconSearch className="pointer-events-none absolute left-2.5 top-1/2 size-4 -translate-y-1/2 text-stone-500" />
|
||||
<input
|
||||
className={input + " w-36 pl-8 sm:w-52"}
|
||||
type="search"
|
||||
aria-label="搜索标题"
|
||||
placeholder="搜索标题…"
|
||||
value={qRaw}
|
||||
onChange={(e) => setQRaw(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
{isAdmin && lib > 0 && (
|
||||
<button className={btn} disabled={scan.isPending} onClick={() => scan.mutate(lib)}>
|
||||
<IconScan className="size-4" />
|
||||
{scan.isPending ? "扫描中…" : "扫描此库"}
|
||||
</button>
|
||||
)}
|
||||
<button className={btn} onClick={() => qc.invalidateQueries({ queryKey: ["books"] })} aria-label="刷新书目">
|
||||
<IconRefresh className="size-4" />
|
||||
<span className="hidden sm:inline">刷新</span>
|
||||
</button>
|
||||
</>
|
||||
}
|
||||
/>
|
||||
<div className="flex-1 overflow-y-auto px-4 py-5 sm:px-6">
|
||||
{libs.length === 0 && !libsQ.isPending && (
|
||||
<p className="mb-6 flex items-center gap-2 rounded-lg bg-amber-500/10 px-3 py-2 text-sm text-amber-200 ring-1 ring-amber-500/25">
|
||||
<IconAlert className="size-4 shrink-0" />
|
||||
还没有书库。admin 请到「库管理」建库并扫描。
|
||||
</p>
|
||||
)}
|
||||
{(libs.length > 0 || libsQ.isPending) && (
|
||||
<nav aria-label="按库筛选" className="mb-8 flex w-fit flex-wrap gap-1 rounded-full bg-white/[0.04] p-1 ring-1 ring-white/[0.07]">
|
||||
<button className={`${pill} ${lib === 0 ? pillActive : ""}`} aria-pressed={lib === 0} onClick={() => setLib(0)}>
|
||||
全部
|
||||
</button>
|
||||
{libs.map((l) => (
|
||||
<button
|
||||
key={l.id}
|
||||
className={`${pill} ${lib === l.id ? pillActive : ""}`}
|
||||
aria-pressed={lib === l.id}
|
||||
onClick={() => setLib(l.id)}
|
||||
>
|
||||
{l.name}
|
||||
</button>
|
||||
<div className="mx-auto flex w-full max-w-6xl flex-col px-4 pb-20 sm:px-6 md:pb-6">
|
||||
<div className="sticky top-0 z-20 -mx-4 mb-6 flex flex-wrap items-center gap-2 border-b bg-background/90 px-4 py-3 backdrop-blur sm:-mx-6 sm:px-6">
|
||||
<div className="relative min-w-0 flex-1 sm:max-w-64">
|
||||
<Search className="pointer-events-none absolute left-2.5 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
|
||||
<Input className="pl-8" type="search" aria-label="搜索标题" placeholder="搜索标题…" value={qRaw} onChange={(e) => setQRaw(e.target.value)} />
|
||||
</div>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="outline" size="sm" className="gap-1.5">
|
||||
<ArrowDownUp className="size-4" />
|
||||
{SORT_LABEL[sort]}
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuLabel>排序</DropdownMenuLabel>
|
||||
{SORTS.map((k) => (
|
||||
<DropdownMenuItem key={k} onClick={() => setParam("sort", k === "added" ? "" : k)} className={k === sort ? "font-medium text-accent" : ""}>
|
||||
{SORT_LABEL[k]}
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</nav>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
<div role="group" aria-label="分组视图" className="flex rounded-md border p-0.5">
|
||||
{GROUPS.map((g) => (
|
||||
<Button key={g.v} size="sm" variant={group === g.v ? "secondary" : "ghost"} aria-pressed={group === g.v} className="px-2.5" onClick={() => setParam("group", g.v === "flat" ? "" : g.v)}>
|
||||
{g.label}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
{isAdmin && lib > LIB0 && (
|
||||
<Button variant="outline" size="sm" disabled={scan.isPending} onClick={() => scan.mutate(lib)} className="gap-1.5">
|
||||
<ScanLine className="size-4" />
|
||||
{scan.isPending ? "扫描中…" : "扫描此库"}
|
||||
</Button>
|
||||
)}
|
||||
<Button variant="ghost" size="icon" aria-label="刷新书目" onClick={() => qc.invalidateQueries({ queryKey: ["books"] })}>
|
||||
<RefreshCw className="size-4" />
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{continueRows.length > 0 && !searching && (
|
||||
<Section title="继续阅读" icon={<IconClock className="size-4 text-amber-500/80" />}>
|
||||
<ul className="flex gap-3 overflow-x-auto pb-2" role="list">
|
||||
{continueRows.map((r, i) => {
|
||||
const b = booksByKey.get(`${r.library_id}/${r.path}`);
|
||||
if (!b)
|
||||
return (
|
||||
<li key={`${r.library_id}/${r.path}`} className={`fx-rise w-44 shrink-0 ${card} p-3`} style={{ animationDelay: `${i * 40}ms` }}>
|
||||
<p className="truncate text-xs text-stone-400">{r.title || "已删除的书"}</p>
|
||||
<p className="mt-1 text-[10px] text-stone-600">#{r.library_id}/{r.path} · 进度已保留</p>
|
||||
</li>
|
||||
);
|
||||
const pct = Math.round(r.percent * 100);
|
||||
{lib === LIB0 && <p className="sr-only" aria-live="polite">{`当前筛选:${libName}`}</p>}
|
||||
|
||||
{(libsQ.data ?? []).length === 0 && !libsQ.isPending && !searching && (
|
||||
<p className="mb-6 flex items-center gap-2 rounded-md border border-accent/30 bg-accent/10 px-3 py-2 text-sm text-accent">
|
||||
<LibraryBig className="size-4 shrink-0" />
|
||||
还没有书库。admin 请到「库管理」建库并扫描。
|
||||
</p>
|
||||
)}
|
||||
|
||||
{continueRows.length > 0 && !searching && (
|
||||
<section className="mb-8">
|
||||
<h2 className="mb-3 text-sm font-medium text-muted-foreground">继续阅读</h2>
|
||||
<ul className="flex gap-3 overflow-x-auto pb-2" role="list">
|
||||
{continueRows.map((r, i) => {
|
||||
const b = booksByKey.get(`${r.library_id}/${r.path}`);
|
||||
if (!b)
|
||||
return (
|
||||
<li key={`${r.library_id}/${r.path}`} className="fx-rise shrink-0" style={{ animationDelay: `${i * 40}ms` }}>
|
||||
<button
|
||||
className="group flex w-64 items-center gap-3 rounded-xl p-2 text-left ring-1 ring-white/[0.07] transition-colors hover:bg-white/[0.05] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-amber-500"
|
||||
onClick={() => nav(`/book/${b.id}`)}
|
||||
>
|
||||
<Cover book={b} className="aspect-[3/4] w-14 shrink-0 rounded-md shadow ring-1 ring-white/10" />
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block truncate text-sm font-medium text-stone-100 group-hover:text-amber-300">{b.title}</span>
|
||||
<span className="mt-0.5 flex items-center gap-1.5 text-[11px] text-stone-500">
|
||||
{r.library}
|
||||
<span className="tabular-nums text-stone-400">{pct}%</span>
|
||||
</span>
|
||||
<span className="mt-1.5 block h-1 overflow-hidden rounded-full bg-white/10">
|
||||
<span className="block h-full rounded-full bg-amber-500" style={{ width: `${pct}%` }} />
|
||||
</span>
|
||||
</span>
|
||||
<IconBookOpen className="size-4 shrink-0 text-stone-600 transition-colors group-hover:text-amber-400" />
|
||||
</button>
|
||||
<li key={`${r.library_id}/${r.path}`} className="fx-rise w-40 shrink-0 rounded-md border bg-card p-3 text-xs text-muted-foreground" style={{ animationDelay: `${i * 40}ms` }}>
|
||||
<p className="truncate">{r.title || "已删除的书"}</p>
|
||||
<p className="mt-1 text-[10px]">进度已保留</p>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
</Section>
|
||||
)}
|
||||
const pct = Math.round(r.percent * 100);
|
||||
return (
|
||||
<li key={`${r.library_id}/${r.path}`} className="fx-rise shrink-0" style={{ animationDelay: `${i * 40}ms` }}>
|
||||
<button onClick={() => nav(`/book/${b.id}`)} className="group flex w-56 items-center gap-3 rounded-md border bg-card p-2 text-left transition-colors hover:bg-secondary focus-visible:outline-2 focus-visible:outline-accent">
|
||||
<Cover book={b} className="aspect-[3/4] w-11 shrink-0 rounded" />
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block truncate text-sm font-medium group-hover:text-accent">{b.title}</span>
|
||||
<span className="mt-0.5 flex justify-between text-[11px] text-muted-foreground">
|
||||
<span>{r.library}</span>
|
||||
<span className="tabular-nums">{pct}%</span>
|
||||
</span>
|
||||
<span className="mt-1.5 block h-0.5 overflow-hidden rounded-full bg-muted">
|
||||
<span className="block h-full rounded-full bg-accent" style={{ width: `${pct}%` }} />
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
</section>
|
||||
)}
|
||||
|
||||
{booksQ.isError && (
|
||||
<div className={`fx-rise mb-8 flex flex-wrap items-center gap-3 rounded-xl bg-red-950/40 px-4 py-3 text-sm text-red-200 ring-1 ring-red-500/25`}>
|
||||
<IconAlert className="size-4 shrink-0" />
|
||||
<span>加载失败:{booksQ.error instanceof Error ? booksQ.error.message : "未知错误"}</span>
|
||||
<button className={btn} onClick={() => booksQ.refetch()}>
|
||||
<IconRefresh className="size-4" />
|
||||
重试
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{booksQ.isError && (
|
||||
<div className="fx-rise mb-8 flex flex-wrap items-center gap-3 rounded-md border border-destructive/40 px-4 py-3 text-sm">
|
||||
<TriangleAlert className="size-4 shrink-0 text-destructive" />
|
||||
<span>加载失败:{booksQ.error instanceof Error ? booksQ.error.message : "未知错误"}</span>
|
||||
<Button variant="outline" size="sm" onClick={() => booksQ.refetch()}>
|
||||
<RefreshCw className="size-4" /> 重试
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{booksQ.isPending && !booksQ.isError && (
|
||||
<div className={GRID} aria-busy="true" aria-label="加载书目中">
|
||||
{Array.from({ length: 12 }).map((_, i) => (
|
||||
<div key={i} className="space-y-2">
|
||||
<div className="aspect-[3/4] animate-pulse rounded-lg bg-stone-800/70" />
|
||||
<div className="h-3 w-3/4 animate-pulse rounded bg-stone-800/70" />
|
||||
<div className="h-2 w-1/2 animate-pulse rounded bg-stone-800/50" />
|
||||
</div>
|
||||
{booksQ.isPending && !booksQ.isError && (
|
||||
<div className={GRID} aria-busy="true" aria-label="加载书目中">
|
||||
{Array.from({ length: 12 }).map((_, i) => (
|
||||
<div key={i} className="space-y-2">
|
||||
<Skeleton className="aspect-[3/4] w-full rounded-md" />
|
||||
<Skeleton className="h-3 w-3/4" />
|
||||
<Skeleton className="h-2 w-1/2" />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!booksQ.isPending && secs.every((s) => s.books.length === 0) && !booksQ.isError && (
|
||||
<div className="fx-rise flex flex-col items-center py-20 text-center">
|
||||
<FolderOpen className="size-10 text-muted-foreground/60" />
|
||||
<h3 className="mt-3 text-sm font-medium">{searching ? `没有匹配「${serverQ}」的书` : `${libName}暂无书目`}</h3>
|
||||
<p className="mt-1 text-sm text-muted-foreground">{searching ? "换个关键词试试。" : "放入文件后等待扫描,或用侧栏「库管理」上传。"}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{secs.map((s) => (
|
||||
<section key={s.label || "//"} className="mb-10">
|
||||
<SectionHead label={s.label} count={s.books.length} />
|
||||
<div className={GRID}>
|
||||
{s.books.map((b, i) => (
|
||||
<BookCard key={b.id} book={b} index={i} onOpen={() => nav(`/book/${b.id}`)} onDelete={() => setConfirmDel(b)} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
))}
|
||||
|
||||
{!booksQ.isPending && groups.length === 0 && !booksQ.isError && (
|
||||
<div className="fx-rise flex flex-col items-center py-16 text-center">
|
||||
<IconFolder className="size-10 text-stone-700" />
|
||||
<h3 className="mt-3 text-sm font-medium text-stone-300">{searching ? `没有匹配「${q}」的书` : "此库暂无书目"}</h3>
|
||||
<p className="mt-1 text-sm text-stone-500">
|
||||
{searching ? "换个关键词试试。" : "放入文件后等待扫描,或点上方「扫描此库」。"}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{groups.map((g) => (
|
||||
<Section
|
||||
key={g.dir || "//"}
|
||||
title={g.dir || "根目录"}
|
||||
icon={<IconFolder className="size-4 text-stone-500" />}
|
||||
>
|
||||
<div className={GRID}>
|
||||
{g.books.map((b, i) => (
|
||||
<BookCard key={b.id} book={b} index={i} onOpen={() => nav(`/book/${b.id}`)} onDelete={() => del.mutate(b.id)} />
|
||||
))}
|
||||
</div>
|
||||
</Section>
|
||||
))}
|
||||
</div>
|
||||
<AlertDialog open={confirmDel !== null} onOpenChange={(o) => !o && setConfirmDel(null)}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>删除《{confirmDel?.title}》?</AlertDialogTitle>
|
||||
<AlertDialogDescription>磁盘文件一并删除,阅读进度保留。</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>取消</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
className="bg-destructive text-primary-foreground hover:bg-destructive/90"
|
||||
onClick={() => {
|
||||
if (confirmDel) del.mutate(confirmDel.id);
|
||||
setConfirmDel(null);
|
||||
}}
|
||||
>
|
||||
删除
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user