fix(pending-post): preserve sparse retry routes

This commit is contained in:
Tommaso Casaburi
2026-05-21 17:39:35 +07:00
parent 75d90c8264
commit e0a42e7899
4 changed files with 89 additions and 3 deletions
@@ -10,6 +10,7 @@ const act = (React as { act?: (cb: () => void | Promise<void>) => void | Promise
type TestComment = {
cid?: string;
communityAddress?: string;
index?: number;
};
const testState = vi.hoisted(() => ({
@@ -136,6 +137,29 @@ describe('PendingPost', () => {
expect(testState.navigateMock).toHaveBeenCalledWith('/not-found', { replace: true });
});
it('keeps sparse pending account comment indices addressable', async () => {
testState.accountCommentIndex = '1';
testState.accountComments = [{ index: 1 }];
testState.post = {
communityAddress: 'music-posting.eth',
index: 1,
};
await renderPendingPost();
expect(container.querySelector('[data-testid="post-view"]')?.textContent).toBe('no-post');
expect(testState.navigateMock).not.toHaveBeenCalledWith('/not-found', { replace: true });
});
it('redirects missing sparse pending account comment indices to not found', async () => {
testState.accountCommentIndex = '0';
testState.accountComments = [{ index: 1 }];
await renderPendingPost();
expect(testState.navigateMock).toHaveBeenCalledWith('/not-found', { replace: true });
});
it('redirects resolved pending posts to the canonical thread route', async () => {
testState.accountCommentIndex = '1';
testState.accountComments = [{}, {}];
+25 -1
View File
@@ -7,6 +7,30 @@ import { getCommentCommunityAddress } from '../../lib/utils/comment-utils';
import { getBoardPath } from '../../lib/utils/route-utils';
import { Post } from '../post';
type PendingAccountComment = {
index?: number;
};
const hasPendingAccountCommentIndex = (accountComments: PendingAccountComment[] | undefined, accountCommentIndex: number) => {
if (!accountComments || accountComments.length === 0) {
return true;
}
let hasExplicitIndices = false;
for (const accountComment of accountComments) {
if (typeof accountComment?.index !== 'number') {
continue;
}
hasExplicitIndices = true;
if (accountComment.index === accountCommentIndex) {
return true;
}
}
return hasExplicitIndices ? false : accountCommentIndex < accountComments.length;
};
const PendingPost = () => {
const { accountComments } = useAccountComments();
const { accountCommentIndex } = useParams<{ accountCommentIndex?: string }>();
@@ -23,7 +47,7 @@ const PendingPost = () => {
(hasNormalizedAccountCommentIndex &&
normalizedAccountCommentIndex >= 0 &&
Number.isInteger(normalizedAccountCommentIndex) &&
(accountComments?.length === 0 || normalizedAccountCommentIndex < accountComments.length));
hasPendingAccountCommentIndex(accountComments, normalizedAccountCommentIndex));
useEffect(() => {
if (!isValidAccountCommentIndex) {