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) => ({
|
const { author, signer, title, content, link, spoiler, publishCommentOptions } = usePublishPostStore((state) => ({
|
||||||
author: state.author,
|
author: state.author,
|
||||||
signer: state.signer,
|
signer: state.signer,
|
||||||
title: state.title === '' ? undefined : state.title,
|
title: state.title || undefined,
|
||||||
content: state.content === '' ? undefined : state.content,
|
content: state.content || undefined,
|
||||||
link: state.link === '' ? undefined : state.link,
|
link: state.link || undefined,
|
||||||
spoiler: state.spoiler || false,
|
spoiler: state.spoiler || false,
|
||||||
publishCommentOptions: state.publishCommentOptions,
|
publishCommentOptions: state.publishCommentOptions,
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -17,8 +17,8 @@ const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; sub
|
|||||||
publishCommentOptions: state.publishCommentOptions[parentCid],
|
publishCommentOptions: state.publishCommentOptions[parentCid],
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const setReplyStore = usePublishReplyStore((state) => state.setReplyStore);
|
const setPublishReplyStore = usePublishReplyStore((state) => state.setPublishReplyStore);
|
||||||
const resetReplyStore = usePublishReplyStore((state) => state.resetReplyStore);
|
const resetPublishReplyStore = usePublishReplyStore((state) => state.resetPublishReplyStore);
|
||||||
|
|
||||||
const createBaseOptions = useCallback(() => {
|
const createBaseOptions = useCallback(() => {
|
||||||
const baseOptions: Comment = {
|
const baseOptions: Comment = {
|
||||||
@@ -55,12 +55,12 @@ const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; sub
|
|||||||
}, {} as Partial<Comment>);
|
}, {} as Partial<Comment>);
|
||||||
|
|
||||||
const newOptions = { ...baseOptions, ...sanitizedOptions };
|
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);
|
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 { 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';
|
||||||
@@ -30,7 +30,7 @@ const usePublishPostStore = create<SubmitState>((set) => ({
|
|||||||
spoiler: undefined,
|
spoiler: undefined,
|
||||||
publishCommentOptions: {},
|
publishCommentOptions: {},
|
||||||
setPublishPostStore: (comment: Comment) =>
|
setPublishPostStore: (comment: Comment) =>
|
||||||
set((state) => {
|
set(() => {
|
||||||
const { subplebbitAddress, author, content, link, signer, spoiler, title } = comment;
|
const { subplebbitAddress, author, content, link, signer, spoiler, title } = comment;
|
||||||
|
|
||||||
const displayName = 'displayName' in comment ? comment.displayName || undefined : author?.displayName;
|
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 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 = {
|
const publishCommentOptions: PublishCommentOptions = {
|
||||||
subplebbitAddress: nextState.subplebbitAddress,
|
subplebbitAddress,
|
||||||
title: nextState.title,
|
title,
|
||||||
content: nextState.content,
|
content,
|
||||||
link: nextState.link,
|
link,
|
||||||
spoiler: nextState.spoiler,
|
spoiler,
|
||||||
onChallenge: (...args: any) => addChallenge(args),
|
onChallenge: (...args: any) => addChallenge(args),
|
||||||
onChallengeVerification: alertChallengeVerificationFailed,
|
onChallengeVerification: (challengeVerification: ChallengeVerification, comment: Comment) => {
|
||||||
|
alertChallengeVerificationFailed(challengeVerification, comment);
|
||||||
|
},
|
||||||
onError: (error: Error) => {
|
onError: (error: Error) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
alert(error.message);
|
alert(error.message);
|
||||||
@@ -68,12 +60,21 @@ const usePublishPostStore = create<SubmitState>((set) => ({
|
|||||||
publishCommentOptions.author = updatedAuthor;
|
publishCommentOptions.author = updatedAuthor;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nextState.signer) {
|
if (signer) {
|
||||||
publishCommentOptions.signer = nextState.signer;
|
publishCommentOptions.signer = signer;
|
||||||
}
|
}
|
||||||
|
|
||||||
nextState.publishCommentOptions = publishCommentOptions;
|
return {
|
||||||
return nextState;
|
author: updatedAuthor,
|
||||||
|
displayName,
|
||||||
|
signer,
|
||||||
|
subplebbitAddress,
|
||||||
|
title,
|
||||||
|
content,
|
||||||
|
link,
|
||||||
|
spoiler,
|
||||||
|
publishCommentOptions,
|
||||||
|
};
|
||||||
}),
|
}),
|
||||||
resetPublishPostStore: () =>
|
resetPublishPostStore: () =>
|
||||||
set({
|
set({
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ 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: (comment: Comment) => void;
|
setPublishReplyStore: (comment: Comment) => void;
|
||||||
resetReplyStore: (parentCid: string) => void;
|
resetPublishReplyStore: (parentCid: string) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
const { addChallenge } = useChallengesStore.getState();
|
const { addChallenge } = useChallengesStore.getState();
|
||||||
@@ -26,7 +26,7 @@ const usePublishReplyStore = create<ReplyState>((set) => ({
|
|||||||
spoiler: {},
|
spoiler: {},
|
||||||
publishCommentOptions: {},
|
publishCommentOptions: {},
|
||||||
|
|
||||||
setReplyStore: (comment: Comment) =>
|
setPublishReplyStore: (comment: Comment) =>
|
||||||
set((state) => {
|
set((state) => {
|
||||||
const { subplebbitAddress, parentCid, author, content, link, signer, spoiler } = comment;
|
const { subplebbitAddress, parentCid, author, content, link, signer, spoiler } = comment;
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ const usePublishReplyStore = create<ReplyState>((set) => ({
|
|||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
|
|
||||||
resetReplyStore: (parentCid) =>
|
resetPublishReplyStore: (parentCid) =>
|
||||||
set((state) => ({
|
set((state) => ({
|
||||||
author: { ...state.author, [parentCid]: undefined },
|
author: { ...state.author, [parentCid]: undefined },
|
||||||
displayName: { ...state.displayName, [parentCid]: undefined },
|
displayName: { ...state.displayName, [parentCid]: undefined },
|
||||||
|
|||||||
Reference in New Issue
Block a user