2024-08-06 20:12:07 +02:00
|
|
|
import { useCallback } from 'react';
|
2024-11-05 12:02:01 +01:00
|
|
|
import { Comment, useAccount, usePublishComment } from '@plebbit/plebbit-react-hooks';
|
2024-08-07 16:25:53 +02:00
|
|
|
import useAnonMode from './use-anon-mode';
|
2024-11-05 12:02:01 +01:00
|
|
|
import usePublishReplyStore from '../stores/use-publish-reply-store';
|
2024-04-27 12:41:54 +02:00
|
|
|
|
2024-11-07 13:51:39 +01:00
|
|
|
const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; subplebbitAddress: string; postCid?: string }) => {
|
2024-04-27 12:41:54 +02:00
|
|
|
const parentCid = cid;
|
2024-09-19 21:07:45 +02:00
|
|
|
const account = useAccount();
|
2025-01-20 12:38:08 +01:00
|
|
|
const { anonMode } = useAnonMode(postCid);
|
2024-09-19 21:07:45 +02:00
|
|
|
|
2024-10-13 21:36:54 +02:00
|
|
|
const { author, signer, content, link, spoiler, publishCommentOptions } = usePublishReplyStore((state) => ({
|
2025-01-20 12:38:08 +01:00
|
|
|
author: state.author[parentCid],
|
2024-10-14 17:12:18 +02:00
|
|
|
signer: state.signer[parentCid] || undefined,
|
|
|
|
|
content: state.content[parentCid] || undefined,
|
|
|
|
|
link: state.link[parentCid] || undefined,
|
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],
|
|
|
|
|
}));
|
|
|
|
|
|
2025-01-30 17:27:58 +01:00
|
|
|
const setPublishReplyStore = usePublishReplyStore((state) => state.setPublishReplyStore);
|
|
|
|
|
const resetPublishReplyStore = usePublishReplyStore((state) => state.resetPublishReplyStore);
|
2024-04-27 12:41:54 +02:00
|
|
|
|
2025-01-20 12:38:08 +01:00
|
|
|
const createBaseOptions = useCallback(() => {
|
|
|
|
|
const baseOptions: Comment = {
|
|
|
|
|
subplebbitAddress,
|
|
|
|
|
parentCid,
|
|
|
|
|
postCid: postCid ?? parentCid,
|
|
|
|
|
content,
|
|
|
|
|
link,
|
|
|
|
|
spoiler,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (anonMode) {
|
2025-01-29 13:09:38 +01:00
|
|
|
baseOptions.author = {
|
|
|
|
|
address: signer?.address,
|
|
|
|
|
displayName: author?.displayName,
|
|
|
|
|
};
|
2025-01-20 12:38:08 +01:00
|
|
|
baseOptions.signer = signer;
|
2025-01-29 13:09:38 +01:00
|
|
|
} else {
|
|
|
|
|
baseOptions.author = {
|
|
|
|
|
...account?.author,
|
|
|
|
|
displayName: author?.displayName || account?.author?.displayName,
|
|
|
|
|
};
|
2025-01-20 12:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return baseOptions;
|
|
|
|
|
}, [anonMode, author, content, link, parentCid, postCid, signer, spoiler, subplebbitAddress, account]);
|
|
|
|
|
|
2024-08-06 20:12:07 +02:00
|
|
|
const setPublishReplyOptions = useCallback(
|
2025-01-20 12:38:08 +01:00
|
|
|
(options: Partial<Comment>) => {
|
|
|
|
|
const baseOptions = createBaseOptions();
|
|
|
|
|
const sanitizedOptions = Object.entries(options).reduce((acc, [key, value]) => {
|
|
|
|
|
acc[key] = value === '' ? undefined : value;
|
|
|
|
|
return acc;
|
|
|
|
|
}, {} as Partial<Comment>);
|
2024-08-30 12:08:53 +02:00
|
|
|
|
2025-01-20 12:38:08 +01:00
|
|
|
const newOptions = { ...baseOptions, ...sanitizedOptions };
|
2025-01-30 17:27:58 +01:00
|
|
|
setPublishReplyStore(newOptions);
|
2024-08-06 20:12:07 +02:00
|
|
|
},
|
2025-01-30 17:27:58 +01:00
|
|
|
[createBaseOptions, setPublishReplyStore],
|
2024-04-27 12:41:54 +02:00
|
|
|
);
|
|
|
|
|
|
2025-01-30 17:27:58 +01:00
|
|
|
const resetPublishReplyOptions = useCallback(() => resetPublishReplyStore(parentCid), [parentCid, resetPublishReplyStore]);
|
2024-04-27 12:41:54 +02:00
|
|
|
|
|
|
|
|
const { index, publishComment } = usePublishComment(publishCommentOptions);
|
|
|
|
|
|
2025-01-20 12:38:08 +01:00
|
|
|
return {
|
|
|
|
|
setPublishReplyOptions,
|
|
|
|
|
resetPublishReplyOptions,
|
|
|
|
|
replyIndex: index,
|
|
|
|
|
publishReply: publishComment,
|
|
|
|
|
};
|
2024-04-27 12:41:54 +02:00
|
|
|
};
|
|
|
|
|
|
2024-10-13 21:36:54 +02:00
|
|
|
export default usePublishReply;
|