2024-08-06 20:12:07 +02:00
|
|
|
import { useCallback } from 'react';
|
2024-09-19 17:33:00 +02:00
|
|
|
import { ChallengeVerification, Comment, PublishCommentOptions, useAccount, usePublishComment } from '@plebbit/plebbit-react-hooks';
|
2024-04-27 12:41:54 +02:00
|
|
|
import { create } from 'zustand';
|
|
|
|
|
import { alertChallengeVerificationFailed } from '../lib/utils/challenge-utils';
|
2024-05-31 10:28:57 +02:00
|
|
|
import useChallengesStore from '../stores/use-challenges-store';
|
2024-08-07 16:25:53 +02:00
|
|
|
import useAnonMode from './use-anon-mode';
|
2024-04-27 12:41:54 +02:00
|
|
|
|
|
|
|
|
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;
|
2024-04-27 12:41:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ReplyState = {
|
2024-08-06 20:12:07 +02:00
|
|
|
author: { [parentCid: string]: any | undefined };
|
2024-08-25 18:58:38 +02:00
|
|
|
displayName: { [parentCid: string]: string | undefined };
|
2024-04-27 12:41:54 +02:00
|
|
|
content: { [parentCid: string]: string | undefined };
|
|
|
|
|
link: { [parentCid: string]: string | undefined };
|
2024-08-06 20:12:07 +02:00
|
|
|
signer: { [parentCid: string]: any | undefined };
|
2024-04-27 12:41:54 +02:00
|
|
|
spoiler: { [parentCid: string]: boolean | undefined };
|
2024-08-06 20:12:07 +02:00
|
|
|
publishCommentOptions: { [parentCid: string]: PublishCommentOptions | undefined };
|
2024-04-27 12:41:54 +02:00
|
|
|
setReplyStore: (data: SetReplyStoreData) => void;
|
|
|
|
|
resetReplyStore: (parentCid: string) => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { addChallenge } = useChallengesStore.getState();
|
|
|
|
|
|
|
|
|
|
const useReplyStore = create<ReplyState>((set) => ({
|
2024-08-06 20:12:07 +02:00
|
|
|
author: {},
|
2024-08-25 18:58:38 +02:00
|
|
|
displayName: {},
|
2024-04-27 12:41:54 +02:00
|
|
|
content: {},
|
|
|
|
|
link: {},
|
2024-08-06 20:12:07 +02:00
|
|
|
signer: {},
|
2024-04-27 12:41:54 +02:00
|
|
|
spoiler: {},
|
|
|
|
|
publishCommentOptions: {},
|
2024-08-12 21:55:52 +02:00
|
|
|
|
2024-04-27 12:41:54 +02:00
|
|
|
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-12 21:55:52 +02:00
|
|
|
|
2024-08-25 18:58:38 +02:00
|
|
|
const publishCommentOptions: PublishCommentOptions = {
|
2024-04-27 12:41:54 +02:00
|
|
|
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);
|
|
|
|
|
},
|
|
|
|
|
};
|
2024-08-12 21:55:52 +02:00
|
|
|
|
2024-09-19 17:33:00 +02:00
|
|
|
if (updatedAuthor) {
|
|
|
|
|
publishCommentOptions.author = updatedAuthor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (signer) {
|
|
|
|
|
publishCommentOptions.signer = signer;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 12:41:54 +02:00
|
|
|
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 },
|
2024-04-27 12:41:54 +02:00
|
|
|
content: { ...state.content, [parentCid]: content },
|
|
|
|
|
link: { ...state.link, [parentCid]: link },
|
2024-08-12 21:55:52 +02:00
|
|
|
signer: { ...state.signer, [parentCid]: signer },
|
2024-04-27 12:41:54 +02:00
|
|
|
spoiler: { ...state.spoiler, [parentCid]: spoiler },
|
|
|
|
|
publishCommentOptions: { ...state.publishCommentOptions, [parentCid]: publishCommentOptions },
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
resetReplyStore: (parentCid) =>
|
|
|
|
|
set((state) => ({
|
2024-08-06 20:12:07 +02:00
|
|
|
author: { ...state.author, [parentCid]: undefined },
|
2024-08-25 18:58:38 +02:00
|
|
|
displayName: { ...state.displayName, [parentCid]: undefined },
|
2024-04-27 12:41:54 +02:00
|
|
|
content: { ...state.content, [parentCid]: undefined },
|
|
|
|
|
link: { ...state.link, [parentCid]: undefined },
|
2024-08-12 21:55:52 +02:00
|
|
|
signer: { ...state.signer, [parentCid]: undefined },
|
2024-04-27 12:41:54 +02:00
|
|
|
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-20 11:49:29 +02:00
|
|
|
author: state.author[parentCid] || (account?.author ? { displayName: account.author.displayName || undefined } : undefined),
|
2024-09-19 21:07:45 +02:00
|
|
|
displayName: state.displayName[parentCid] || account?.author?.displayName,
|
2024-08-06 20:12:07 +02:00
|
|
|
signer: state.signer[parentCid],
|
2024-09-19 21:07:45 +02:00
|
|
|
content: state.content[parentCid] || '',
|
2024-04-27 12:41:54 +02:00
|
|
|
link: state.link[parentCid],
|
2024-09-19 21:07:45 +02:00
|
|
|
spoiler: state.spoiler[parentCid] || false,
|
2024-04-27 12:41:54 +02:00
|
|
|
publishCommentOptions: state.publishCommentOptions[parentCid],
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const setReplyStore = useReplyStore((state) => state.setReplyStore);
|
|
|
|
|
const resetReplyStore = useReplyStore((state) => state.resetReplyStore);
|
|
|
|
|
|
2024-08-06 20:12:07 +02:00
|
|
|
const setPublishReplyOptions = useCallback(
|
|
|
|
|
(options: Partial<SetReplyStoreData>) => {
|
2024-08-25 18:58:38 +02:00
|
|
|
const newOptions: Partial<SetReplyStoreData> = {
|
2024-08-06 20:12:07 +02:00
|
|
|
subplebbitAddress,
|
|
|
|
|
parentCid,
|
2024-09-20 12:25:22 +02:00
|
|
|
content: options.content === '' ? undefined : options.content ?? content,
|
|
|
|
|
link: options.link === '' ? undefined : options.link ?? link,
|
2024-08-25 18:58:38 +02:00
|
|
|
spoiler: options.spoiler ?? spoiler,
|
2024-09-20 12:25:22 +02:00
|
|
|
displayName: options.displayName === '' ? undefined : 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) {
|
2024-09-20 12:25:22 +02:00
|
|
|
newOptions.displayName = options.displayName === '' ? undefined : options.displayName;
|
2024-08-30 12:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (anonMode) {
|
2024-09-19 17:33:00 +02:00
|
|
|
newOptions.signer = signer || options.signer;
|
2024-08-30 12:08:53 +02:00
|
|
|
newOptions.author = {
|
2024-09-19 17:33:00 +02:00
|
|
|
...(author || {}),
|
|
|
|
|
address: newOptions.signer?.address,
|
2024-08-30 12:08:53 +02:00
|
|
|
...('displayName' in options && { displayName: options.displayName }),
|
|
|
|
|
};
|
|
|
|
|
} else {
|
2024-09-19 17:33:00 +02:00
|
|
|
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);
|
2024-08-06 20:12:07 +02:00
|
|
|
},
|
2024-09-19 17:33:00 +02:00
|
|
|
[subplebbitAddress, parentCid, author, signer, content, link, spoiler, setReplyStore, anonMode, account],
|
2024-04-27 12:41:54 +02:00
|
|
|
);
|
|
|
|
|
|
2024-08-06 20:12:07 +02:00
|
|
|
const resetPublishReplyOptions = useCallback(() => resetReplyStore(parentCid), [parentCid, resetReplyStore]);
|
2024-04-27 12:41:54 +02:00
|
|
|
|
|
|
|
|
const { index, publishComment } = usePublishComment(publishCommentOptions);
|
|
|
|
|
|
2024-08-06 20:12:07 +02:00
|
|
|
return { setPublishReplyOptions, resetPublishReplyOptions, replyIndex: index, publishReply: publishComment, setReplyStore };
|
2024-04-27 12:41:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useReply;
|