Elffuss BraidHub

kikocis/utopia public

A hook after an early return crashes Library on a full page load (#228)

Closes #220

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Changesetdaaf32c13c876f4b6c5e557c6d8c0df042c11e3eff899644d068805fe980d30e
AuthorWaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-09-03 05:43 +08:00
CommitterGitHub <noreply@github.com> · 2026-09-03 05:43 +08:00
Parents846550111ecc

Browse the files at this changeset

1 file changed

modified web/src/pages/Library.tsx +10 −5
@@ -483,17 +483,22 @@
483483 [kb, upload, canUpload],
484484 );
485485
486- if (!kb) return <Loading>{S.nav.loading}</Loading>;
487-
488- // 服务端已经切好了这一页:筛选、作用域、页码都在请求里
489- const pagedDocs = docs.data?.docs ?? [];
490- const totalDocs = docs.data?.total ?? 0;
491486 // 整库可抽的篇数。**重建是库级动作**,不该显示当前来源的数字
492487 const kbStats = useQuery({
493488 queryKey: ["docCount", kb?.id],
494489 queryFn: () => api.documents(kb!.id, { limit: 1, offset: 0 }),
495490 enabled: !!kb,
496491 });
492+
493+ // **最后一个 hook 之后才能提前返回。** 这一行从前排在 kbStats 前面:整页
494+ // 刷新或直接打开链接时第一次渲染 kb 还没到,提前返回跳过了后面的 hook,
495+ // 下一次渲染多出一个 hook,React 直接抛 "Rendered more hooks"。站内导航
496+ // 时 kb 已在缓存里,所以只有深链和刷新会撞上
497+ if (!kb) return <Loading>{S.nav.loading}</Loading>;
498+
499+ // 服务端已经切好了这一页:筛选、作用域、页码都在请求里
500+ const pagedDocs = docs.data?.docs ?? [];
501+ const totalDocs = docs.data?.total ?? 0;
497502 const kbReady = kbStats.data?.ready ?? 0;
498503 const sourceList = sources.data?.sources ?? [];
499504 const selectedSource = sourceList.find((s) => s.id === selection);