2024-08-06 20:12:07 +02:00
|
|
|
import { useCallback } from 'react';
|
2024-04-27 12:41:54 +02:00
|
|
|
import { ChallengeVerification, Comment, PublishCommentOptions, usePublishComment } from '@plebbit/plebbit-react-hooks';
|
|
|
|
|
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-08 17:48:45 +02:00
|
|
|
author?: any | undefined;
|
|
|
|
|
displayName?: string | undefined;
|
2024-04-27 12:41:54 +02:00
|
|
|
content: string | undefined;
|
|
|
|
|
link: string | undefined;
|
2024-08-07 16:25:53 +02:00
|
|
|
signer?: any | undefined;
|
2024-08-06 20:12:07 +02:00
|
|
|
spoiler: boolean | undefined;
|
2024-04-27 12:41:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ReplyState = {
|
2024-08-06 20:12:07 +02:00
|
|
|
author: { [parentCid: string]: any | 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-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-12 21:55:52 +02:00
|
|
|
|
|
|
|
|
const updatedAuthor = {
|
|
|
|
|
...(state.author[parentCid] || author),
|
|
|
|
|
...(displayName ? { displayName } : {}),
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-27 12:41:54 +02:00
|
|
|
const publishCommentOptions = {
|
|
|
|
|
subplebbitAddress,
|
|
|
|
|
parentCid,
|
2024-08-12 21:55:52 +02:00
|
|
|
...(updatedAuthor ? { author: updatedAuthor } : {}),
|
|
|
|
|
...(signer ? { signer } : {}),
|
2024-04-27 12:41:54 +02:00
|
|
|
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-04-27 12:41:54 +02:00
|
|
|
return {
|
2024-08-08 17:48:45 +02:00
|
|
|
author: { ...state.author, [parentCid]: updatedAuthor },
|
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-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-08-12 21:55:52 +02:00
|
|
|
const { author, displayName, signer, content, link, spoiler, publishCommentOptions } = useReplyStore((state) => ({
|
2024-08-06 20:12:07 +02:00
|
|
|
author: state.author[parentCid],
|
2024-08-12 21:55:52 +02:00
|
|
|
displayName: state.author[parentCid]?.displayName,
|
2024-08-06 20:12:07 +02:00
|
|
|
signer: state.signer[parentCid],
|
2024-04-27 12:41:54 +02:00
|
|
|
content: state.content[parentCid],
|
|
|
|
|
link: state.link[parentCid],
|
|
|
|
|
spoiler: state.spoiler[parentCid],
|
|
|
|
|
publishCommentOptions: state.publishCommentOptions[parentCid],
|
|
|
|
|
}));
|
|
|
|
|
|
2024-08-07 16:25:53 +02:00
|
|
|
const { anonMode } = useAnonMode();
|
|
|
|
|
|
2024-04-27 12:41:54 +02:00
|
|
|
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>) => {
|
|
|
|
|
setReplyStore({
|
|
|
|
|
subplebbitAddress,
|
|
|
|
|
parentCid,
|
2024-08-12 21:55:52 +02:00
|
|
|
...(anonMode ? { displayName } : {}),
|
2024-08-08 17:48:45 +02:00
|
|
|
...(anonMode ? { author } : {}),
|
2024-08-06 20:12:07 +02:00
|
|
|
content,
|
|
|
|
|
link,
|
|
|
|
|
spoiler,
|
2024-08-07 16:25:53 +02:00
|
|
|
...(anonMode ? { signer } : {}),
|
2024-08-06 20:12:07 +02:00
|
|
|
...options,
|
|
|
|
|
});
|
|
|
|
|
},
|
2024-08-12 21:55:52 +02:00
|
|
|
[subplebbitAddress, parentCid, author, displayName, signer, content, link, spoiler, setReplyStore, anonMode],
|
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;
|