mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
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:
@@ -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;
|
||||
Reference in New Issue
Block a user