refactor use-publish-reply

This commit is contained in:
Tom (plebeius.eth)
2024-11-05 12:02:01 +01:00
parent 82217a726c
commit aa3b3a8b5d
2 changed files with 8 additions and 19 deletions
+5 -5
View File
@@ -1,7 +1,7 @@
import { useCallback } from 'react'; import { useCallback } from 'react';
import { useAccount, usePublishComment } from '@plebbit/plebbit-react-hooks'; import { Comment, useAccount, usePublishComment } from '@plebbit/plebbit-react-hooks';
import useAnonMode from './use-anon-mode'; import useAnonMode from './use-anon-mode';
import usePublishReplyStore, { SetReplyStoreData } from '../stores/use-publish-reply-store'; import usePublishReplyStore from '../stores/use-publish-reply-store';
const usePublishReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress: string }) => { const usePublishReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress: string }) => {
const parentCid = cid; const parentCid = cid;
@@ -22,8 +22,8 @@ const usePublishReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAd
const resetReplyStore = usePublishReplyStore((state) => state.resetReplyStore); const resetReplyStore = usePublishReplyStore((state) => state.resetReplyStore);
const setPublishReplyOptions = useCallback( const setPublishReplyOptions = useCallback(
(options: Partial<SetReplyStoreData>) => { (options: Comment) => {
const newOptions: Partial<SetReplyStoreData> = { const newOptions: Comment = {
subplebbitAddress, subplebbitAddress,
parentCid, parentCid,
content: options.content === '' ? undefined : options.content ?? content, content: options.content === '' ? undefined : options.content ?? content,
@@ -51,7 +51,7 @@ const usePublishReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAd
}; };
} }
setReplyStore(newOptions as SetReplyStoreData); setReplyStore(newOptions);
}, },
[subplebbitAddress, parentCid, author, signer, content, link, spoiler, setReplyStore, anonMode, account], [subplebbitAddress, parentCid, author, signer, content, link, spoiler, setReplyStore, anonMode, account],
); );
+3 -14
View File
@@ -3,17 +3,6 @@ import { create } from 'zustand';
import { alertChallengeVerificationFailed } from '../lib/utils/challenge-utils'; import { alertChallengeVerificationFailed } from '../lib/utils/challenge-utils';
import useChallengesStore from './use-challenges-store'; import useChallengesStore from './use-challenges-store';
export type SetReplyStoreData = {
subplebbitAddress: string;
parentCid: string;
author?: any;
displayName?: string;
content: string;
link?: string;
signer?: any;
spoiler: boolean;
};
type ReplyState = { type ReplyState = {
author: { [parentCid: string]: any | undefined }; author: { [parentCid: string]: any | undefined };
displayName: { [parentCid: string]: string | undefined }; displayName: { [parentCid: string]: string | undefined };
@@ -22,7 +11,7 @@ type ReplyState = {
signer: { [parentCid: string]: any | undefined }; signer: { [parentCid: string]: any | undefined };
spoiler: { [parentCid: string]: boolean | undefined }; spoiler: { [parentCid: string]: boolean | undefined };
publishCommentOptions: { [parentCid: string]: PublishCommentOptions | undefined }; publishCommentOptions: { [parentCid: string]: PublishCommentOptions | undefined };
setReplyStore: (data: SetReplyStoreData) => void; setReplyStore: (comment: Comment) => void;
resetReplyStore: (parentCid: string) => void; resetReplyStore: (parentCid: string) => void;
}; };
@@ -37,9 +26,9 @@ const usePublishReplyStore = create<ReplyState>((set) => ({
spoiler: {}, spoiler: {},
publishCommentOptions: {}, publishCommentOptions: {},
setReplyStore: (data: SetReplyStoreData) => setReplyStore: (comment: Comment) =>
set((state) => { set((state) => {
const { subplebbitAddress, parentCid, author, displayName, content, link, signer, spoiler } = data; const { subplebbitAddress, parentCid, author, displayName, content, link, signer, spoiler } = comment;
const updatedAuthor = displayName ? { ...author, displayName } : author; const updatedAuthor = displayName ? { ...author, displayName } : author;
const publishCommentOptions: PublishCommentOptions = { const publishCommentOptions: PublishCommentOptions = {