Files
5chan/src/hooks/use-publish-reply.ts
T

155 lines
5.7 KiB
TypeScript
Raw Normal View History

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';
2024-08-07 16:25:53 +02:00
import useAnonMode from './use-anon-mode';
type SetReplyStoreData = {
subplebbitAddress: string;
parentCid: string;
2024-08-25 18:58:38 +02:00
author?: any;
displayName?: string;
content: string;
link?: string;
signer?: any;
spoiler: boolean;
};
type ReplyState = {
author: { [parentCid: string]: any | undefined };
2024-08-25 18:58:38 +02:00
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: {},
2024-08-25 18:58:38 +02:00
displayName: {},
content: {},
link: {},
signer: {},
spoiler: {},
publishCommentOptions: {},
setReplyStore: (data: SetReplyStoreData) =>
set((state) => {
2024-08-08 17:48:45 +02:00
const { subplebbitAddress, parentCid, author, displayName, content, link, signer, spoiler } = data;
2024-08-25 18:58:38 +02:00
const updatedAuthor = displayName ? { ...author, displayName } : author;
2024-08-25 18:58:38 +02:00
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 {
2024-08-08 17:48:45 +02:00
author: { ...state.author, [parentCid]: updatedAuthor },
2024-08-25 18:58:38 +02:00
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 },
2024-08-25 18:58:38 +02:00
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 parentCid = cid;
2024-09-19 21:07:45 +02:00
const account = useAccount();
const { anonMode } = useAnonMode();
2024-08-30 12:08:53 +02:00
const { author, signer, content, link, spoiler, publishCommentOptions } = useReplyStore((state) => ({
2024-09-19 21:07:45 +02:00
author: state.author[parentCid] || (account?.author ? { ...account.author, displayName: account.author.displayName || undefined } : undefined),
displayName: state.displayName[parentCid] || account?.author?.displayName,
signer: state.signer[parentCid],
2024-09-19 21:07:45 +02:00
content: state.content[parentCid] || '',
link: state.link[parentCid],
2024-09-19 21:07:45 +02:00
spoiler: state.spoiler[parentCid] || false,
publishCommentOptions: state.publishCommentOptions[parentCid],
}));
const setReplyStore = useReplyStore((state) => state.setReplyStore);
const resetReplyStore = useReplyStore((state) => state.resetReplyStore);
const setPublishReplyOptions = useCallback(
(options: Partial<SetReplyStoreData>) => {
2024-08-25 18:58:38 +02:00
const newOptions: Partial<SetReplyStoreData> = {
subplebbitAddress,
parentCid,
2024-08-30 12:08:53 +02:00
content: options.content ?? content,
link: options.link ?? link,
2024-08-25 18:58:38 +02:00
spoiler: options.spoiler ?? spoiler,
2024-09-19 21:07:45 +02:00
displayName: options.displayName ?? author?.displayName ?? account?.author?.displayName,
2024-08-25 18:58:38 +02:00
};
2024-08-30 12:08:53 +02:00
if ('displayName' in options) {
newOptions.displayName = options.displayName;
}
if (anonMode) {
newOptions.signer = signer || options.signer;
2024-08-30 12:08:53 +02:00
newOptions.author = {
...(author || {}),
address: newOptions.signer?.address,
2024-08-30 12:08:53 +02:00
...('displayName' in options && { displayName: options.displayName }),
};
} else {
newOptions.signer = undefined;
newOptions.author = {
address: account?.author?.address,
...('displayName' in options && { displayName: options.displayName }),
};
2024-08-30 12:08:53 +02:00
}
2024-08-25 18:58:38 +02:00
setReplyStore(newOptions as SetReplyStoreData);
},
[subplebbitAddress, parentCid, author, signer, content, link, spoiler, setReplyStore, anonMode, account],
);
const resetPublishReplyOptions = useCallback(() => resetReplyStore(parentCid), [parentCid, resetReplyStore]);
const { index, publishComment } = usePublishComment(publishCommentOptions);
return { setPublishReplyOptions, resetPublishReplyOptions, replyIndex: index, publishReply: publishComment, setReplyStore };
};
export default useReply;