From 7ffb3ce8c5ef5f898c615292a74e50bfb97989e0 Mon Sep 17 00:00:00 2001 From: "Tom (plebeius.eth)" Date: Mon, 20 Jan 2025 12:38:08 +0100 Subject: [PATCH] fix: emptying fields could fail when publishing reply --- src/hooks/use-publish-reply.ts | 74 +++++++++++++++------------ src/stores/use-publish-reply-store.ts | 12 +++-- 2 files changed, 49 insertions(+), 37 deletions(-) diff --git a/src/hooks/use-publish-reply.ts b/src/hooks/use-publish-reply.ts index 90e1f2f4..ed1fda60 100644 --- a/src/hooks/use-publish-reply.ts +++ b/src/hooks/use-publish-reply.ts @@ -6,11 +6,10 @@ import usePublishReplyStore from '../stores/use-publish-reply-store'; const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; subplebbitAddress: string; postCid?: string }) => { const parentCid = cid; const account = useAccount(); - const { anonMode } = useAnonMode(); + const { anonMode } = useAnonMode(postCid); const { author, signer, content, link, spoiler, publishCommentOptions } = usePublishReplyStore((state) => ({ - author: state.author[parentCid] || (account?.author ? { displayName: account.author.displayName || undefined } : undefined), - displayName: state.displayName[parentCid] || account?.author?.displayName, + author: state.author[parentCid], signer: state.signer[parentCid] || undefined, content: state.content[parentCid] || undefined, link: state.link[parentCid] || undefined, @@ -21,47 +20,54 @@ const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; sub const setReplyStore = usePublishReplyStore((state) => state.setReplyStore); const resetReplyStore = usePublishReplyStore((state) => state.resetReplyStore); + const createBaseOptions = useCallback(() => { + const baseOptions: Comment = { + subplebbitAddress, + parentCid, + postCid: postCid ?? parentCid, + content, + link, + spoiler, + }; + + const authorOptions = { + displayName: author?.displayName, + address: anonMode ? signer?.address : account?.author?.address, + }; + + baseOptions.author = authorOptions; + + if (anonMode) { + baseOptions.signer = signer; + } + + return baseOptions; + }, [anonMode, author, content, link, parentCid, postCid, signer, spoiler, subplebbitAddress, account]); + const setPublishReplyOptions = useCallback( - (options: Comment) => { - const newOptions: Comment = { - subplebbitAddress, - parentCid, - postCid: postCid ?? parentCid, - content: options.content === '' ? undefined : options.content ?? content, - link: options.link === '' ? undefined : options.link ?? link, - spoiler: options.spoiler ?? spoiler, - displayName: options.displayName === '' ? undefined : options.displayName ?? author?.displayName ?? account?.author?.displayName, - }; - - if ('displayName' in options) { - newOptions.displayName = options.displayName === '' ? undefined : options.displayName; - } - - if (anonMode) { - newOptions.signer = signer || options.signer; - newOptions.author = { - ...(author || {}), - address: newOptions.signer?.address, - ...('displayName' in options && { displayName: options.displayName }), - }; - } else { - newOptions.signer = undefined; - newOptions.author = { - address: account?.author?.address, - ...('displayName' in options && { displayName: options.displayName }), - }; - } + (options: Partial) => { + const baseOptions = createBaseOptions(); + const sanitizedOptions = Object.entries(options).reduce((acc, [key, value]) => { + acc[key] = value === '' ? undefined : value; + return acc; + }, {} as Partial); + const newOptions = { ...baseOptions, ...sanitizedOptions }; setReplyStore(newOptions); }, - [subplebbitAddress, parentCid, author, signer, content, link, spoiler, setReplyStore, anonMode, account, postCid], + [createBaseOptions, setReplyStore], ); const resetPublishReplyOptions = useCallback(() => resetReplyStore(parentCid), [parentCid, resetReplyStore]); const { index, publishComment } = usePublishComment(publishCommentOptions); - return { setPublishReplyOptions, resetPublishReplyOptions, replyIndex: index, publishReply: publishComment, setReplyStore }; + return { + setPublishReplyOptions, + resetPublishReplyOptions, + replyIndex: index, + publishReply: publishComment, + }; }; export default usePublishReply; diff --git a/src/stores/use-publish-reply-store.ts b/src/stores/use-publish-reply-store.ts index e563456a..e0fc19c8 100644 --- a/src/stores/use-publish-reply-store.ts +++ b/src/stores/use-publish-reply-store.ts @@ -28,8 +28,14 @@ const usePublishReplyStore = create((set) => ({ setReplyStore: (comment: Comment) => set((state) => { - const { subplebbitAddress, parentCid, author, displayName, content, link, signer, spoiler } = comment; - const updatedAuthor = displayName ? { ...author, displayName } : author; + const { subplebbitAddress, parentCid, author, content, link, signer, spoiler } = comment; + + const displayName = 'displayName' in comment ? comment.displayName || undefined : author?.displayName; + + const baseAuthor = author ? { ...author } : {}; + delete baseAuthor.displayName; + + const updatedAuthor = displayName ? { ...baseAuthor, displayName } : baseAuthor; const publishCommentOptions: PublishCommentOptions = { subplebbitAddress, @@ -48,7 +54,7 @@ const usePublishReplyStore = create((set) => ({ }, }; - if (updatedAuthor) { + if (Object.keys(updatedAuthor).length > 0) { publishCommentOptions.author = updatedAuthor; }