mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
clean code, better var names, fix bugs
This commit is contained in:
@@ -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,
|
||||
}));
|
||||
|
||||
@@ -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<Comment>);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@@ -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<SubmitState>((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<SubmitState>((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<SubmitState>((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({
|
||||
|
||||
@@ -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<ReplyState>((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<ReplyState>((set) => ({
|
||||
};
|
||||
}),
|
||||
|
||||
resetReplyStore: (parentCid) =>
|
||||
resetPublishReplyStore: (parentCid) =>
|
||||
set((state) => ({
|
||||
author: { ...state.author, [parentCid]: undefined },
|
||||
displayName: { ...state.displayName, [parentCid]: undefined },
|
||||
|
||||
Reference in New Issue
Block a user