mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
refactor publish post/reply stores, naming was confusing
This commit is contained in:
@@ -1,102 +1,14 @@
|
||||
import { useCallback } from 'react';
|
||||
import { ChallengeVerification, Comment, PublishCommentOptions, useAccount, usePublishComment } from '@plebbit/plebbit-react-hooks';
|
||||
import { create } from 'zustand';
|
||||
import { alertChallengeVerificationFailed } from '../lib/utils/challenge-utils';
|
||||
import useChallengesStore from '../stores/use-challenges-store';
|
||||
import { useAccount, usePublishComment } from '@plebbit/plebbit-react-hooks';
|
||||
import useAnonMode from './use-anon-mode';
|
||||
import usePublishReplyStore, { SetReplyStoreData } from '../stores/use-publish-reply-store';
|
||||
|
||||
type SetReplyStoreData = {
|
||||
subplebbitAddress: string;
|
||||
parentCid: string;
|
||||
author?: any;
|
||||
displayName?: string;
|
||||
content: string;
|
||||
link?: string;
|
||||
signer?: any;
|
||||
spoiler: boolean;
|
||||
};
|
||||
|
||||
type ReplyState = {
|
||||
author: { [parentCid: string]: any | undefined };
|
||||
displayName: { [parentCid: string]: string | undefined };
|
||||
content: { [parentCid: string]: string | undefined };
|
||||
link: { [parentCid: string]: string | undefined };
|
||||
signer: { [parentCid: string]: any | undefined };
|
||||
spoiler: { [parentCid: string]: boolean | undefined };
|
||||
publishCommentOptions: { [parentCid: string]: PublishCommentOptions | undefined };
|
||||
setReplyStore: (data: SetReplyStoreData) => void;
|
||||
resetReplyStore: (parentCid: string) => void;
|
||||
};
|
||||
|
||||
const { addChallenge } = useChallengesStore.getState();
|
||||
|
||||
const useReplyStore = create<ReplyState>((set) => ({
|
||||
author: {},
|
||||
displayName: {},
|
||||
content: {},
|
||||
link: {},
|
||||
signer: {},
|
||||
spoiler: {},
|
||||
publishCommentOptions: {},
|
||||
|
||||
setReplyStore: (data: SetReplyStoreData) =>
|
||||
set((state) => {
|
||||
const { subplebbitAddress, parentCid, author, displayName, content, link, signer, spoiler } = data;
|
||||
const updatedAuthor = displayName ? { ...author, displayName } : author;
|
||||
|
||||
const publishCommentOptions: PublishCommentOptions = {
|
||||
subplebbitAddress,
|
||||
parentCid,
|
||||
content,
|
||||
link,
|
||||
spoiler,
|
||||
onChallenge: (...args: any) => addChallenge(args),
|
||||
onChallengeVerification: (challengeVerification: ChallengeVerification, comment: Comment) => {
|
||||
alertChallengeVerificationFailed(challengeVerification, comment);
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
console.error(error);
|
||||
alert(error.message);
|
||||
},
|
||||
};
|
||||
|
||||
if (updatedAuthor) {
|
||||
publishCommentOptions.author = updatedAuthor;
|
||||
}
|
||||
|
||||
if (signer) {
|
||||
publishCommentOptions.signer = signer;
|
||||
}
|
||||
|
||||
return {
|
||||
author: { ...state.author, [parentCid]: updatedAuthor },
|
||||
displayName: { ...state.displayName, [parentCid]: displayName },
|
||||
content: { ...state.content, [parentCid]: content },
|
||||
link: { ...state.link, [parentCid]: link },
|
||||
signer: { ...state.signer, [parentCid]: signer },
|
||||
spoiler: { ...state.spoiler, [parentCid]: spoiler },
|
||||
publishCommentOptions: { ...state.publishCommentOptions, [parentCid]: publishCommentOptions },
|
||||
};
|
||||
}),
|
||||
|
||||
resetReplyStore: (parentCid) =>
|
||||
set((state) => ({
|
||||
author: { ...state.author, [parentCid]: undefined },
|
||||
displayName: { ...state.displayName, [parentCid]: undefined },
|
||||
content: { ...state.content, [parentCid]: undefined },
|
||||
link: { ...state.link, [parentCid]: undefined },
|
||||
signer: { ...state.signer, [parentCid]: undefined },
|
||||
spoiler: { ...state.spoiler, [parentCid]: undefined },
|
||||
publishCommentOptions: { ...state.publishCommentOptions, [parentCid]: undefined },
|
||||
})),
|
||||
}));
|
||||
|
||||
const useReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress: string }) => {
|
||||
const usePublishReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress: string }) => {
|
||||
const parentCid = cid;
|
||||
const account = useAccount();
|
||||
const { anonMode } = useAnonMode();
|
||||
|
||||
const { author, signer, content, link, spoiler, publishCommentOptions } = useReplyStore((state) => ({
|
||||
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,
|
||||
signer: state.signer[parentCid],
|
||||
@@ -106,8 +18,8 @@ const useReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress:
|
||||
publishCommentOptions: state.publishCommentOptions[parentCid],
|
||||
}));
|
||||
|
||||
const setReplyStore = useReplyStore((state) => state.setReplyStore);
|
||||
const resetReplyStore = useReplyStore((state) => state.resetReplyStore);
|
||||
const setReplyStore = usePublishReplyStore((state) => state.setReplyStore);
|
||||
const resetReplyStore = usePublishReplyStore((state) => state.resetReplyStore);
|
||||
|
||||
const setPublishReplyOptions = useCallback(
|
||||
(options: Partial<SetReplyStoreData>) => {
|
||||
@@ -151,4 +63,4 @@ const useReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress:
|
||||
return { setPublishReplyOptions, resetPublishReplyOptions, replyIndex: index, publishReply: publishComment, setReplyStore };
|
||||
};
|
||||
|
||||
export default useReply;
|
||||
export default usePublishReply;
|
||||
|
||||
Reference in New Issue
Block a user