mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix(publishing): abandon challenge close and clean failed local posts
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
},
|
||||
|
||||
@@ -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);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user