perf: defer board reply fetching and remove nested mobile backlink fetch

Split useReplies in post-desktop.tsx and post-mobile.tsx into preview/full modes so board cards avoid full REPLIES_PER_PAGE fetches by default. Added replies-preview-utils.ts and switched mobile backlinks to use precomputed directRepliesByParentCid map instead of per-reply useReplies.
This commit is contained in:
plebeius
2026-02-22 16:53:01 +08:00
parent bdfee1b12d
commit a13c72f888
4 changed files with 179 additions and 35 deletions
+48
View File
@@ -0,0 +1,48 @@
import { BOARD_REPLIES_PREVIEW_VISIBLE_COUNT } from '../constants';
export interface CommentLike {
cid?: string | null;
}
/**
* From replies sorted by 'new' (newest first), returns the latest N in chronological
* display order (oldest of those first). Handles fewer-than-N replies.
*/
export function getPreviewDisplayReplies<T extends CommentLike>(replies: T[], visibleCount: number = BOARD_REPLIES_PREVIEW_VISIBLE_COUNT): T[] {
const slice = replies.slice(0, visibleCount);
return [...slice].reverse();
}
export interface ComputeOmittedParams {
totalReplyCount: number;
visibleCount: number;
pinned?: boolean;
}
/**
* Computes omitted reply count for board view. For pinned threads, collapsed view
* shows 0 replies, so omitted = total. For non-pinned, omitted = total - visible.
* Result is always clamped at zero.
*/
export function computeOmittedCount({ totalReplyCount, visibleCount, pinned = false }: ComputeOmittedParams): number {
if (pinned) {
return Math.max(0, totalReplyCount);
}
return Math.max(0, totalReplyCount - visibleCount);
}
export interface GetTotalReplyCountParams {
replyCount: number | undefined;
fullLoadedCount: number;
previewLoadedCount: number;
}
/**
* Returns total reply count: post.replyCount when defined, else max of loaded counts.
*/
export function getTotalReplyCount({ replyCount, fullLoadedCount, previewLoadedCount }: GetTotalReplyCountParams): number {
if (typeof replyCount === 'number' && replyCount >= 0) {
return replyCount;
}
return Math.max(fullLoadedCount, previewLoadedCount);
}