Merge branch 'codex/fix/mod-queue-post-order'

This commit is contained in:
Tommaso Casaburi
2026-04-27 15:13:38 +07:00
4 changed files with 168 additions and 14 deletions
@@ -12,6 +12,7 @@ type TestComment = {
content?: string;
index?: number;
number?: number;
pendingApproval?: boolean;
communityAddress?: string;
};
@@ -144,4 +145,65 @@ describe('useFreshReplies', () => {
expect(latestValue[0]).toBe(testState.accountComments[0] as never);
expect(testState.accountCommentsCalls).toContainEqual({ commentIndices: [0] });
});
it('orders replies by final post number after a pending reply is approved', () => {
testState.replies = [
{
cid: 'reply-8',
index: 8,
number: 8,
communityAddress: 'music.eth',
},
{
cid: 'reply-12',
index: 12,
number: 12,
communityAddress: 'music.eth',
},
{
cid: 'pending-reply',
index: 11,
number: undefined,
pendingApproval: true,
communityAddress: 'music.eth',
},
];
testState.accountComments = [
{
cid: 'reply-11',
index: 11,
number: 11,
communityAddress: 'music.eth',
},
];
renderHook();
expect(latestValue.map((reply) => reply.cid)).toEqual(['reply-8', 'reply-11', 'reply-12']);
expect(testState.accountCommentsCalls).toContainEqual({ commentIndices: [8, 12, 11] });
});
it('orders already-numbered replies when an approved queued reply was appended', () => {
testState.replies = [
{
cid: 'reply-8',
number: 8,
communityAddress: 'music.eth',
},
{
cid: 'reply-12',
number: 12,
communityAddress: 'music.eth',
},
{
cid: 'reply-11',
number: 11,
communityAddress: 'music.eth',
},
];
renderHook();
expect(latestValue.map((reply) => reply.cid)).toEqual(['reply-8', 'reply-11', 'reply-12']);
});
});
+8 -3
View File
@@ -1,5 +1,6 @@
import { useMemo } from 'react';
import { Comment, useAccountComments } from '@bitsocial/bitsocial-react-hooks';
import { sortRepliesForDisplay } from '../lib/utils/replies-preview-utils';
// Keep the hook on its indexed fast path when there are no reply indices to resolve.
const EMPTY_ACCOUNT_COMMENT_LOOKUP = { commentIndices: [-1] };
@@ -13,10 +14,14 @@ const useFreshReplies = (replies: Comment[] = []) => {
const { accountComments } = useAccountComments(accountCommentLookupOptions);
return useMemo(() => {
if (!replies.length || !accountComments?.length) {
if (!replies.length) {
return replies;
}
if (!accountComments?.length) {
return sortRepliesForDisplay(replies);
}
const accountCommentsByIndex = new Map<number, Comment>();
for (const accountComment of accountComments) {
if (typeof accountComment?.index === 'number') {
@@ -40,7 +45,7 @@ const useFreshReplies = (replies: Comment[] = []) => {
});
if (!hasFreshReplies) {
return replies;
return sortRepliesForDisplay(replies);
}
const seenReplyIndices = new Set<number>();
@@ -59,7 +64,7 @@ const useFreshReplies = (replies: Comment[] = []) => {
return true;
});
return hasDuplicateReplyIndices ? dedupedReplies : nextReplies;
return sortRepliesForDisplay(hasDuplicateReplyIndices ? dedupedReplies : nextReplies);
}, [accountComments, replies]);
};