From 9cc68768872940bfca272fb29a28d0fae9611a30 Mon Sep 17 00:00:00 2001 From: plebeius Date: Thu, 5 Mar 2026 20:47:49 +0800 Subject: [PATCH] fix(challenge flow): abandon publish immediately on modal close --- src/hooks/use-publish-post.ts | 9 +++++---- src/hooks/use-publish-reply.ts | 9 +++++---- src/stores/use-challenges-store.ts | 3 +-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/hooks/use-publish-post.ts b/src/hooks/use-publish-post.ts index fd586d16..90e2d59e 100644 --- a/src/hooks/use-publish-post.ts +++ b/src/hooks/use-publish-post.ts @@ -17,6 +17,9 @@ const usePublishPost = ({ subplebbitAddress }: { subplebbitAddress?: string }) = const resetPublishPostStore = usePublishPostStore((state) => state.resetPublishPostStore); const addChallenge = useChallengesStore((state) => state.addChallenge); const abandonPublishRef = useRef<(() => Promise) | undefined>(); + const abandonCurrentPublish = useCallback(async () => { + await abandonPublishRef.current?.(); + }, []); const createBaseOptions = useCallback(() => { const baseOptions: Comment = { @@ -58,12 +61,10 @@ const usePublishPost = ({ subplebbitAddress }: { subplebbitAddress?: string }) = () => ({ ...publishCommentOptions, onChallenge: async (...args: any[]) => { - addChallenge(args, async () => { - await abandonPublishRef.current?.(); - }); + addChallenge(args, abandonCurrentPublish); }, }), - [addChallenge, publishCommentOptions], + [abandonCurrentPublish, addChallenge, publishCommentOptions], ); const { index, publishComment, abandonPublish } = usePublishComment(publishOptionsWithAbandon); diff --git a/src/hooks/use-publish-reply.ts b/src/hooks/use-publish-reply.ts index f231dce4..a73802e5 100644 --- a/src/hooks/use-publish-reply.ts +++ b/src/hooks/use-publish-reply.ts @@ -20,6 +20,9 @@ const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; sub const resetPublishReplyStore = usePublishReplyStore((state) => state.resetPublishReplyStore); const addChallenge = useChallengesStore((state) => state.addChallenge); const abandonPublishRef = useRef<(() => Promise) | undefined>(); + const abandonCurrentPublish = useCallback(async () => { + await abandonPublishRef.current?.(); + }, []); const createBaseOptions = useCallback(() => { const baseOptions: Comment = { @@ -66,12 +69,10 @@ const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; sub () => ({ ...mergedPublishOptions, onChallenge: async (...args: any[]) => { - addChallenge(args, async () => { - await abandonPublishRef.current?.(); - }); + addChallenge(args, abandonCurrentPublish); }, }), - [addChallenge, mergedPublishOptions], + [abandonCurrentPublish, addChallenge, mergedPublishOptions], ); const { index, publishComment, abandonPublish } = usePublishComment(publishOptionsWithAbandon); diff --git a/src/stores/use-challenges-store.ts b/src/stores/use-challenges-store.ts index c0bf8c7c..d73e5a6e 100644 --- a/src/stores/use-challenges-store.ts +++ b/src/stores/use-challenges-store.ts @@ -26,12 +26,11 @@ const useChallengesStore = create((set, get) => ({ }, abandonCurrentChallenge: async () => { const currentChallenge = get().challenges[0]; + get().removeChallenge(); try { await currentChallenge?.onAbandon?.(); } catch (error) { console.error('Failed to abandon challenge publication:', error); - } finally { - get().removeChallenge(); } }, }));