diff --git a/src/hooks/use-publish-post.ts b/src/hooks/use-publish-post.ts index d2fa5dee..366422bb 100644 --- a/src/hooks/use-publish-post.ts +++ b/src/hooks/use-publish-post.ts @@ -10,9 +10,9 @@ const usePublishPost = ({ subplebbitAddress }: { subplebbitAddress?: string }) = const { author, signer, title, content, link, spoiler, publishCommentOptions } = usePublishPostStore((state) => ({ author: state.author, signer: state.signer, - title: state.title === '' ? undefined : state.title, - content: state.content === '' ? undefined : state.content, - link: state.link === '' ? undefined : state.link, + title: state.title || undefined, + content: state.content || undefined, + link: state.link || undefined, spoiler: state.spoiler || false, publishCommentOptions: state.publishCommentOptions, })); diff --git a/src/hooks/use-publish-reply.ts b/src/hooks/use-publish-reply.ts index 2a04bb5e..9e682392 100644 --- a/src/hooks/use-publish-reply.ts +++ b/src/hooks/use-publish-reply.ts @@ -17,8 +17,8 @@ const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; sub publishCommentOptions: state.publishCommentOptions[parentCid], })); - const setReplyStore = usePublishReplyStore((state) => state.setReplyStore); - const resetReplyStore = usePublishReplyStore((state) => state.resetReplyStore); + const setPublishReplyStore = usePublishReplyStore((state) => state.setPublishReplyStore); + const resetPublishReplyStore = usePublishReplyStore((state) => state.resetPublishReplyStore); const createBaseOptions = useCallback(() => { const baseOptions: Comment = { @@ -55,12 +55,12 @@ const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; sub }, {} as Partial); const newOptions = { ...baseOptions, ...sanitizedOptions }; - setReplyStore(newOptions); + setPublishReplyStore(newOptions); }, - [createBaseOptions, setReplyStore], + [createBaseOptions, setPublishReplyStore], ); - const resetPublishReplyOptions = useCallback(() => resetReplyStore(parentCid), [parentCid, resetReplyStore]); + const resetPublishReplyOptions = useCallback(() => resetPublishReplyStore(parentCid), [parentCid, resetPublishReplyStore]); const { index, publishComment } = usePublishComment(publishCommentOptions); diff --git a/src/stores/use-publish-post-store.ts b/src/stores/use-publish-post-store.ts index 31a9a8cc..1f625c4b 100644 --- a/src/stores/use-publish-post-store.ts +++ b/src/stores/use-publish-post-store.ts @@ -1,4 +1,4 @@ -import { Comment, PublishCommentOptions } from '@plebbit/plebbit-react-hooks'; +import { ChallengeVerification, Comment, PublishCommentOptions } from '@plebbit/plebbit-react-hooks'; import { create } from 'zustand'; import { alertChallengeVerificationFailed } from '../lib/utils/challenge-utils'; import useChallengesStore from './use-challenges-store'; @@ -30,7 +30,7 @@ const usePublishPostStore = create((set) => ({ spoiler: undefined, publishCommentOptions: {}, setPublishPostStore: (comment: Comment) => - set((state) => { + set(() => { const { subplebbitAddress, author, content, link, signer, spoiler, title } = comment; const displayName = 'displayName' in comment ? comment.displayName || undefined : author?.displayName; @@ -40,24 +40,16 @@ const usePublishPostStore = create((set) => ({ const updatedAuthor = displayName ? { ...baseAuthor, displayName } : baseAuthor; - const nextState = { ...state }; - if (author !== undefined) nextState.author = author; - if (displayName !== undefined) nextState.displayName = displayName; - if (signer !== undefined) nextState.signer = signer; - if (subplebbitAddress !== undefined) nextState.subplebbitAddress = subplebbitAddress; - if (title !== undefined) nextState.title = title || undefined; - if (content !== undefined) nextState.content = content || undefined; - if (link !== undefined) nextState.link = link || undefined; - if (spoiler !== undefined) nextState.spoiler = spoiler || undefined; - const publishCommentOptions: PublishCommentOptions = { - subplebbitAddress: nextState.subplebbitAddress, - title: nextState.title, - content: nextState.content, - link: nextState.link, - spoiler: nextState.spoiler, + subplebbitAddress, + title, + content, + link, + spoiler, onChallenge: (...args: any) => addChallenge(args), - onChallengeVerification: alertChallengeVerificationFailed, + onChallengeVerification: (challengeVerification: ChallengeVerification, comment: Comment) => { + alertChallengeVerificationFailed(challengeVerification, comment); + }, onError: (error: Error) => { console.error(error); alert(error.message); @@ -68,12 +60,21 @@ const usePublishPostStore = create((set) => ({ publishCommentOptions.author = updatedAuthor; } - if (nextState.signer) { - publishCommentOptions.signer = nextState.signer; + if (signer) { + publishCommentOptions.signer = signer; } - nextState.publishCommentOptions = publishCommentOptions; - return nextState; + return { + author: updatedAuthor, + displayName, + signer, + subplebbitAddress, + title, + content, + link, + spoiler, + publishCommentOptions, + }; }), resetPublishPostStore: () => set({ diff --git a/src/stores/use-publish-reply-store.ts b/src/stores/use-publish-reply-store.ts index e0fc19c8..ed6bc9f3 100644 --- a/src/stores/use-publish-reply-store.ts +++ b/src/stores/use-publish-reply-store.ts @@ -11,8 +11,8 @@ type ReplyState = { signer: { [parentCid: string]: any | undefined }; spoiler: { [parentCid: string]: boolean | undefined }; publishCommentOptions: { [parentCid: string]: PublishCommentOptions | undefined }; - setReplyStore: (comment: Comment) => void; - resetReplyStore: (parentCid: string) => void; + setPublishReplyStore: (comment: Comment) => void; + resetPublishReplyStore: (parentCid: string) => void; }; const { addChallenge } = useChallengesStore.getState(); @@ -26,7 +26,7 @@ const usePublishReplyStore = create((set) => ({ spoiler: {}, publishCommentOptions: {}, - setReplyStore: (comment: Comment) => + setPublishReplyStore: (comment: Comment) => set((state) => { const { subplebbitAddress, parentCid, author, content, link, signer, spoiler } = comment; @@ -73,7 +73,7 @@ const usePublishReplyStore = create((set) => ({ }; }), - resetReplyStore: (parentCid) => + resetPublishReplyStore: (parentCid) => set((state) => ({ author: { ...state.author, [parentCid]: undefined }, displayName: { ...state.displayName, [parentCid]: undefined },