fix(replies): refresh thread backlinks from live account replies (#1058)

* fix(replies): refresh thread backlinks from live account replies

* refactor(replies): extract useRegisterFreshReplies hook per CodeRabbit
This commit is contained in:
Tommaso Casaburi
2026-03-12 15:08:45 +08:00
committed by GitHub
parent b3ec6495c5
commit ea792fe8a8
7 changed files with 252 additions and 36 deletions
+38
View File
@@ -0,0 +1,38 @@
import { useMemo } from 'react';
import { Comment, useAccountComments } from '@bitsocialnet/bitsocial-react-hooks';
const useFreshReplies = (replies: Comment[] = []) => {
const { accountComments } = useAccountComments();
return useMemo(() => {
if (!replies.length || !accountComments?.length) {
return replies;
}
const accountCommentsByIndex = new Map<number, Comment>();
for (const accountComment of accountComments) {
if (typeof accountComment?.index === 'number') {
accountCommentsByIndex.set(accountComment.index, accountComment);
}
}
let hasFreshReplies = false;
const nextReplies = replies.map((reply) => {
if (typeof reply?.index !== 'number') {
return reply;
}
const freshReply = accountCommentsByIndex.get(reply.index);
if (!freshReply) {
return reply;
}
hasFreshReplies = true;
return freshReply;
});
return hasFreshReplies ? nextReplies : replies;
}, [accountComments, replies]);
};
export default useFreshReplies;