fix(challenge flow): abandon publish immediately on modal close

This commit is contained in:
plebeius
2026-03-05 20:47:49 +08:00
parent 59851a4b01
commit 9cc6876887
3 changed files with 11 additions and 10 deletions
+5 -4
View File
@@ -17,6 +17,9 @@ const usePublishPost = ({ subplebbitAddress }: { subplebbitAddress?: string }) =
const resetPublishPostStore = usePublishPostStore((state) => state.resetPublishPostStore); const resetPublishPostStore = usePublishPostStore((state) => state.resetPublishPostStore);
const addChallenge = useChallengesStore((state) => state.addChallenge); const addChallenge = useChallengesStore((state) => state.addChallenge);
const abandonPublishRef = useRef<(() => Promise<void>) | undefined>(); const abandonPublishRef = useRef<(() => Promise<void>) | undefined>();
const abandonCurrentPublish = useCallback(async () => {
await abandonPublishRef.current?.();
}, []);
const createBaseOptions = useCallback(() => { const createBaseOptions = useCallback(() => {
const baseOptions: Comment = { const baseOptions: Comment = {
@@ -58,12 +61,10 @@ const usePublishPost = ({ subplebbitAddress }: { subplebbitAddress?: string }) =
() => ({ () => ({
...publishCommentOptions, ...publishCommentOptions,
onChallenge: async (...args: any[]) => { onChallenge: async (...args: any[]) => {
addChallenge(args, async () => { addChallenge(args, abandonCurrentPublish);
await abandonPublishRef.current?.();
});
}, },
}), }),
[addChallenge, publishCommentOptions], [abandonCurrentPublish, addChallenge, publishCommentOptions],
); );
const { index, publishComment, abandonPublish } = usePublishComment(publishOptionsWithAbandon); const { index, publishComment, abandonPublish } = usePublishComment(publishOptionsWithAbandon);
+5 -4
View File
@@ -20,6 +20,9 @@ const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; sub
const resetPublishReplyStore = usePublishReplyStore((state) => state.resetPublishReplyStore); const resetPublishReplyStore = usePublishReplyStore((state) => state.resetPublishReplyStore);
const addChallenge = useChallengesStore((state) => state.addChallenge); const addChallenge = useChallengesStore((state) => state.addChallenge);
const abandonPublishRef = useRef<(() => Promise<void>) | undefined>(); const abandonPublishRef = useRef<(() => Promise<void>) | undefined>();
const abandonCurrentPublish = useCallback(async () => {
await abandonPublishRef.current?.();
}, []);
const createBaseOptions = useCallback(() => { const createBaseOptions = useCallback(() => {
const baseOptions: Comment = { const baseOptions: Comment = {
@@ -66,12 +69,10 @@ const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; sub
() => ({ () => ({
...mergedPublishOptions, ...mergedPublishOptions,
onChallenge: async (...args: any[]) => { onChallenge: async (...args: any[]) => {
addChallenge(args, async () => { addChallenge(args, abandonCurrentPublish);
await abandonPublishRef.current?.();
});
}, },
}), }),
[addChallenge, mergedPublishOptions], [abandonCurrentPublish, addChallenge, mergedPublishOptions],
); );
const { index, publishComment, abandonPublish } = usePublishComment(publishOptionsWithAbandon); const { index, publishComment, abandonPublish } = usePublishComment(publishOptionsWithAbandon);
+1 -2
View File
@@ -26,12 +26,11 @@ const useChallengesStore = create<State>((set, get) => ({
}, },
abandonCurrentChallenge: async () => { abandonCurrentChallenge: async () => {
const currentChallenge = get().challenges[0]; const currentChallenge = get().challenges[0];
get().removeChallenge();
try { try {
await currentChallenge?.onAbandon?.(); await currentChallenge?.onAbandon?.();
} catch (error) { } catch (error) {
console.error('Failed to abandon challenge publication:', error); console.error('Failed to abandon challenge publication:', error);
} finally {
get().removeChallenge();
} }
}, },
})); }));