Pre-calc levels instead of increment on-demand

This commit is contained in:
Lim Chee Aun 2024-10-28 18:48:31 +08:00
parent dd3b064aec
commit b8f8271645

View file

@ -393,14 +393,20 @@ function StatusThread({ id, closeLink = '/', instance: propInstance }) {
console.error('Missing statuses', [...missingStatuses]); console.error('Missing statuses', [...missingStatuses]);
} }
function expandReplies(_replies) { let descendantLevelsCount = 1;
function expandReplies(_replies, level) {
const nextLevel = level + 1;
if (nextLevel > descendantLevelsCount) {
descendantLevelsCount = level;
}
return _replies?.map((_r) => ({ return _replies?.map((_r) => ({
id: _r.id, id: _r.id,
account: _r.account, account: _r.account,
repliesCount: _r.repliesCount, repliesCount: _r.repliesCount,
content: _r.content, content: _r.content,
weight: calcStatusWeight(_r), weight: calcStatusWeight(_r),
replies: expandReplies(_r.__replies), level: nextLevel,
replies: expandReplies(_r.__replies, nextLevel),
})); }));
} }
@ -426,7 +432,8 @@ function StatusThread({ id, closeLink = '/', instance: propInstance }) {
descendant: true, descendant: true,
thread: s.account.id === heroStatus.account.id, thread: s.account.id === heroStatus.account.id,
weight: calcStatusWeight(s), weight: calcStatusWeight(s),
replies: expandReplies(s.__replies), level: 1,
replies: expandReplies(s.__replies, 1),
})), })),
]; ];
@ -437,12 +444,13 @@ function StatusThread({ id, closeLink = '/', instance: propInstance }) {
}; };
// Set limit to hero's index // Set limit to hero's index
const heroLimit = allStatuses.findIndex((s) => s.id === id); // const heroLimit = allStatuses.findIndex((s) => s.id === id);
const heroLimit = ancestors.length || 0; // 0-indexed
if (heroLimit >= limit) { if (heroLimit >= limit) {
setLimit(heroLimit + 1); setLimit(heroLimit + 1);
} }
console.log({ allStatuses }); console.log({ allStatuses, descendantLevelsCount });
setStatuses(allStatuses); setStatuses(allStatuses);
cachedStatusesMap[id] = allStatuses; cachedStatusesMap[id] = allStatuses;
@ -758,6 +766,7 @@ function StatusThread({ id, closeLink = '/', instance: propInstance }) {
replies, replies,
repliesCount, repliesCount,
weight, weight,
level,
} = status; } = status;
const isHero = statusID === id; const isHero = statusID === id;
const isLinkable = isThread || ancestor; const isLinkable = isThread || ancestor;
@ -931,7 +940,7 @@ function StatusThread({ id, closeLink = '/', instance: propInstance }) {
instance={instance} instance={instance}
replies={replies} replies={replies}
hasParentThread={thread} hasParentThread={thread}
level={1} level={level}
accWeight={weight} accWeight={weight}
openAll={totalDescendants.current < SUBCOMMENTS_OPEN_ALL_LIMIT} openAll={totalDescendants.current < SUBCOMMENTS_OPEN_ALL_LIMIT}
parentLink={{ parentLink={{
@ -1523,7 +1532,7 @@ function SubComments({
<SubComments <SubComments
instance={instance} instance={instance}
replies={r.replies} replies={r.replies}
level={level + 1} level={r.level}
accWeight={!open ? r.weight : totalWeight} accWeight={!open ? r.weight : totalWeight}
openAll={openAll} openAll={openAll}
parentLink={{ parentLink={{