fix(publishing): abandon challenge close and clean failed local posts

This commit is contained in:
plebeius
2026-03-05 19:30:50 +08:00
parent 980b5ca572
commit 59851a4b01
9 changed files with 156 additions and 40 deletions
+16 -5
View File
@@ -4,16 +4,17 @@ import { Challenge } from '@bitsocialhq/bitsocial-react-hooks';
let nextChallengeId = 0;
interface State {
challenges: Array<{ challenge: Challenge; id: number }>;
addChallenge: (challenge: Challenge) => void;
challenges: Array<{ challenge: Challenge; id: number; onAbandon?: () => Promise<void> | void }>;
addChallenge: (challenge: Challenge, onAbandon?: () => Promise<void> | void) => void;
removeChallenge: () => void;
abandonCurrentChallenge: () => Promise<void>;
}
const useChallengesStore = create<State>((set) => ({
const useChallengesStore = create<State>((set, get) => ({
challenges: [],
addChallenge: (challenge: Challenge) => {
addChallenge: (challenge: Challenge, onAbandon?: () => Promise<void> | void) => {
set((state) => ({
challenges: [...state.challenges, { challenge, id: nextChallengeId++ }],
challenges: [...state.challenges, { challenge, id: nextChallengeId++, onAbandon }],
}));
},
removeChallenge: () => {
@@ -23,6 +24,16 @@ const useChallengesStore = create<State>((set) => ({
return { challenges };
});
},
abandonCurrentChallenge: async () => {
const currentChallenge = get().challenges[0];
try {
await currentChallenge?.onAbandon?.();
} catch (error) {
console.error('Failed to abandon challenge publication:', error);
} finally {
get().removeChallenge();
}
},
}));
export default useChallengesStore;
-4
View File
@@ -1,7 +1,6 @@
import { ChallengeVerification, Comment, PublishCommentOptions } from '@bitsocialhq/bitsocial-react-hooks';
import { create } from 'zustand';
import { alertChallengeVerificationFailed } from '../lib/utils/challenge-utils';
import useChallengesStore from './use-challenges-store';
type SubmitState = {
author?: any | undefined;
@@ -16,8 +15,6 @@ type SubmitState = {
resetPublishPostStore: () => void;
};
const { addChallenge } = useChallengesStore.getState();
const usePublishPostStore = create<SubmitState>((set) => ({
author: undefined,
displayName: undefined,
@@ -44,7 +41,6 @@ const usePublishPostStore = create<SubmitState>((set) => ({
content,
link,
spoiler,
onChallenge: (...args: any) => addChallenge(args),
onChallengeVerification: (challengeVerification: ChallengeVerification, comment: Comment) => {
alertChallengeVerificationFailed(challengeVerification, comment);
},
-4
View File
@@ -1,7 +1,6 @@
import { ChallengeVerification, Comment, PublishCommentOptions } from '@bitsocialhq/bitsocial-react-hooks';
import { create } from 'zustand';
import { alertChallengeVerificationFailed } from '../lib/utils/challenge-utils';
import useChallengesStore from './use-challenges-store';
type ReplyState = {
author: { [parentCid: string]: any | undefined };
@@ -14,8 +13,6 @@ type ReplyState = {
resetPublishReplyStore: (parentCid: string) => void;
};
const { addChallenge } = useChallengesStore.getState();
const usePublishReplyStore = create<ReplyState>((set) => ({
author: {},
displayName: {},
@@ -42,7 +39,6 @@ const usePublishReplyStore = create<ReplyState>((set) => ({
content,
link,
spoiler,
onChallenge: (...args: any) => addChallenge(args),
onChallengeVerification: (challengeVerification: ChallengeVerification, comment: Comment) => {
alertChallengeVerificationFailed(challengeVerification, comment);
},