fix(pending-post): keep retrying failed posts on the pending route (#1161)

* fix(pending-post): keep retrying failed posts on the pending route

Retrying a failed post deletes the pending row before republishing, which
briefly leaves the pending comment non-addressable with no active challenge —
the same state PendingPost's abandoned-challenge guard uses to redirect back to
the board. A shared use-failed-post-retry-store marks the index being retried so
PendingPost skips that redirect until the republished row is created and its own
navigation to the new pending route takes over.

* fix(pending-post): harden retry flag lifecycle against abandoned-challenge race

Address Cursor Bugbot review on the retry flow. Clear the retry flag (and
isRetryRedirectPending) when the republish challenge is abandoned so an abandon
mid-retry falls through to the normal board redirect instead of stranding an
empty pending view. Also navigate to the new pending row before clearing the
flag in the redirect effect so PendingPost never observes an old-index route
with the flag already cleared.
This commit is contained in:
Tommaso Casaburi
2026-06-08 16:08:51 +07:00
committed by GitHub
parent c2b222c4d5
commit bd12b47db6
5 changed files with 122 additions and 7 deletions
@@ -22,6 +22,7 @@ const testState = vi.hoisted(() => ({
locationState: null as { boardPath?: string } | null,
navigateMock: vi.fn(),
post: undefined as TestComment | undefined,
retryingAccountCommentIndex: null as number | null,
}));
vi.mock('react-router-dom', async () => {
@@ -58,6 +59,10 @@ vi.mock('../../../stores/use-challenges-store', () => ({
default: (selector: (state: { challenges: unknown[] }) => unknown) => selector({ challenges: Array.from({ length: testState.challengeCount }) }),
}));
vi.mock('../../../stores/use-failed-post-retry-store', () => ({
default: (selector: (state: { retryingAccountCommentIndex: number | null }) => unknown) => selector({ retryingAccountCommentIndex: testState.retryingAccountCommentIndex }),
}));
vi.mock('../../post', () => ({
Post: ({ post }: { post?: TestComment }) => createElement('div', { 'data-testid': 'post-view' }, post?.cid ?? 'no-post'),
}));
@@ -93,6 +98,7 @@ describe('PendingPost', () => {
testState.locationState = null;
testState.navigateMock.mockReset();
testState.post = undefined;
testState.retryingAccountCommentIndex = null;
window.scrollTo = scrollToMock;
@@ -181,6 +187,19 @@ describe('PendingPost', () => {
expect(testState.navigateMock).toHaveBeenCalledWith('/mu', { replace: true });
});
it('keeps a mid-retry pending post in place while the failed row is deleted and republished', async () => {
testState.accountCommentIndex = '0';
testState.accountComments = [];
testState.challengeCount = 0;
testState.locationState = { boardPath: 'mu' };
testState.post = undefined;
testState.retryingAccountCommentIndex = 0;
await renderPendingPost();
expect(testState.navigateMock).not.toHaveBeenCalled();
});
it('redirects missing sparse pending account comment indices to not found', async () => {
testState.accountCommentIndex = '0';
testState.accountComments = [{ index: 1 }];
+11 -3
View File
@@ -6,6 +6,7 @@ import useSafeAccountComment from '../../hooks/use-safe-account-comment';
import { getCommentCommunityAddress } from '../../lib/utils/comment-utils';
import { getBoardPath } from '../../lib/utils/route-utils';
import useChallengesStore from '../../stores/use-challenges-store';
import useFailedPostRetryStore from '../../stores/use-failed-post-retry-store';
import { Post } from '../post';
type PendingAccountComment = {
@@ -56,6 +57,8 @@ const PendingPost = () => {
const postBoardPath = postCommunityAddress ? getBoardPath(postCommunityAddress, directories) : undefined;
const pendingBoardPath = postBoardPath || routeBoardPath;
const hasActiveChallenge = useChallengesStore((state) => state.challenges.length > 0);
const retryingAccountCommentIndex = useFailedPostRetryStore((state) => state.retryingAccountCommentIndex);
const isRetryingThisPendingPost = retryingAccountCommentIndex !== null && retryingAccountCommentIndex === normalizedAccountCommentIndex;
const lastPendingBoardRef = useRef<{ accountCommentIndex: number; boardPath: string } | null>(null);
useEffect(() => window.scrollTo(0, 0), []);
@@ -74,13 +77,18 @@ const PendingPost = () => {
hasPendingAccountCommentIndex(accountComments, normalizedAccountCommentIndex));
useEffect(() => {
// A retry deletes this pending row before republishing, briefly invalidating the index. Stay put;
// useDeleteFailedPost redirects to the new pending row once the republished comment is created.
if (isRetryingThisPendingPost) {
return;
}
if (!isValidAccountCommentIndex) {
const lastPendingBoard = lastPendingBoardRef.current;
const abandonedBoardPath =
!hasActiveChallenge && lastPendingBoard && lastPendingBoard.accountCommentIndex === normalizedAccountCommentIndex ? lastPendingBoard.boardPath : undefined;
navigate(abandonedBoardPath ? `/${abandonedBoardPath}` : '/not-found', { replace: true });
}
}, [hasActiveChallenge, isValidAccountCommentIndex, navigate, normalizedAccountCommentIndex]);
}, [hasActiveChallenge, isRetryingThisPendingPost, isValidAccountCommentIndex, navigate, normalizedAccountCommentIndex]);
useEffect(() => {
if (post?.cid && postBoardPath) {
@@ -89,7 +97,7 @@ const PendingPost = () => {
}, [post?.cid, postBoardPath, navigate]);
useEffect(() => {
if (hasAddressablePost || !isValidAccountCommentIndex) {
if (isRetryingThisPendingPost || hasAddressablePost || !isValidAccountCommentIndex) {
return;
}
@@ -99,7 +107,7 @@ const PendingPost = () => {
if (abandonedBoardPath) {
navigate(`/${abandonedBoardPath}`, { replace: true });
}
}, [hasActiveChallenge, hasAddressablePost, isValidAccountCommentIndex, navigate, normalizedAccountCommentIndex]);
}, [hasActiveChallenge, hasAddressablePost, isRetryingThisPendingPost, isValidAccountCommentIndex, navigate, normalizedAccountCommentIndex]);
return <Post post={post} />;
};