mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix: emptying fields could fail when publishing reply
This commit is contained in:
@@ -6,11 +6,10 @@ import usePublishReplyStore from '../stores/use-publish-reply-store';
|
|||||||
const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; subplebbitAddress: string; postCid?: string }) => {
|
const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; subplebbitAddress: string; postCid?: string }) => {
|
||||||
const parentCid = cid;
|
const parentCid = cid;
|
||||||
const account = useAccount();
|
const account = useAccount();
|
||||||
const { anonMode } = useAnonMode();
|
const { anonMode } = useAnonMode(postCid);
|
||||||
|
|
||||||
const { author, signer, content, link, spoiler, publishCommentOptions } = usePublishReplyStore((state) => ({
|
const { author, signer, content, link, spoiler, publishCommentOptions } = usePublishReplyStore((state) => ({
|
||||||
author: state.author[parentCid] || (account?.author ? { displayName: account.author.displayName || undefined } : undefined),
|
author: state.author[parentCid],
|
||||||
displayName: state.displayName[parentCid] || account?.author?.displayName,
|
|
||||||
signer: state.signer[parentCid] || undefined,
|
signer: state.signer[parentCid] || undefined,
|
||||||
content: state.content[parentCid] || undefined,
|
content: state.content[parentCid] || undefined,
|
||||||
link: state.link[parentCid] || undefined,
|
link: state.link[parentCid] || undefined,
|
||||||
@@ -21,47 +20,54 @@ const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; sub
|
|||||||
const setReplyStore = usePublishReplyStore((state) => state.setReplyStore);
|
const setReplyStore = usePublishReplyStore((state) => state.setReplyStore);
|
||||||
const resetReplyStore = usePublishReplyStore((state) => state.resetReplyStore);
|
const resetReplyStore = usePublishReplyStore((state) => state.resetReplyStore);
|
||||||
|
|
||||||
|
const createBaseOptions = useCallback(() => {
|
||||||
|
const baseOptions: Comment = {
|
||||||
|
subplebbitAddress,
|
||||||
|
parentCid,
|
||||||
|
postCid: postCid ?? parentCid,
|
||||||
|
content,
|
||||||
|
link,
|
||||||
|
spoiler,
|
||||||
|
};
|
||||||
|
|
||||||
|
const authorOptions = {
|
||||||
|
displayName: author?.displayName,
|
||||||
|
address: anonMode ? signer?.address : account?.author?.address,
|
||||||
|
};
|
||||||
|
|
||||||
|
baseOptions.author = authorOptions;
|
||||||
|
|
||||||
|
if (anonMode) {
|
||||||
|
baseOptions.signer = signer;
|
||||||
|
}
|
||||||
|
|
||||||
|
return baseOptions;
|
||||||
|
}, [anonMode, author, content, link, parentCid, postCid, signer, spoiler, subplebbitAddress, account]);
|
||||||
|
|
||||||
const setPublishReplyOptions = useCallback(
|
const setPublishReplyOptions = useCallback(
|
||||||
(options: Comment) => {
|
(options: Partial<Comment>) => {
|
||||||
const newOptions: Comment = {
|
const baseOptions = createBaseOptions();
|
||||||
subplebbitAddress,
|
const sanitizedOptions = Object.entries(options).reduce((acc, [key, value]) => {
|
||||||
parentCid,
|
acc[key] = value === '' ? undefined : value;
|
||||||
postCid: postCid ?? parentCid,
|
return acc;
|
||||||
content: options.content === '' ? undefined : options.content ?? content,
|
}, {} as Partial<Comment>);
|
||||||
link: options.link === '' ? undefined : options.link ?? link,
|
|
||||||
spoiler: options.spoiler ?? spoiler,
|
|
||||||
displayName: options.displayName === '' ? undefined : options.displayName ?? author?.displayName ?? account?.author?.displayName,
|
|
||||||
};
|
|
||||||
|
|
||||||
if ('displayName' in options) {
|
|
||||||
newOptions.displayName = options.displayName === '' ? undefined : options.displayName;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (anonMode) {
|
|
||||||
newOptions.signer = signer || options.signer;
|
|
||||||
newOptions.author = {
|
|
||||||
...(author || {}),
|
|
||||||
address: newOptions.signer?.address,
|
|
||||||
...('displayName' in options && { displayName: options.displayName }),
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
newOptions.signer = undefined;
|
|
||||||
newOptions.author = {
|
|
||||||
address: account?.author?.address,
|
|
||||||
...('displayName' in options && { displayName: options.displayName }),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const newOptions = { ...baseOptions, ...sanitizedOptions };
|
||||||
setReplyStore(newOptions);
|
setReplyStore(newOptions);
|
||||||
},
|
},
|
||||||
[subplebbitAddress, parentCid, author, signer, content, link, spoiler, setReplyStore, anonMode, account, postCid],
|
[createBaseOptions, setReplyStore],
|
||||||
);
|
);
|
||||||
|
|
||||||
const resetPublishReplyOptions = useCallback(() => resetReplyStore(parentCid), [parentCid, resetReplyStore]);
|
const resetPublishReplyOptions = useCallback(() => resetReplyStore(parentCid), [parentCid, resetReplyStore]);
|
||||||
|
|
||||||
const { index, publishComment } = usePublishComment(publishCommentOptions);
|
const { index, publishComment } = usePublishComment(publishCommentOptions);
|
||||||
|
|
||||||
return { setPublishReplyOptions, resetPublishReplyOptions, replyIndex: index, publishReply: publishComment, setReplyStore };
|
return {
|
||||||
|
setPublishReplyOptions,
|
||||||
|
resetPublishReplyOptions,
|
||||||
|
replyIndex: index,
|
||||||
|
publishReply: publishComment,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export default usePublishReply;
|
export default usePublishReply;
|
||||||
|
|||||||
@@ -28,8 +28,14 @@ const usePublishReplyStore = create<ReplyState>((set) => ({
|
|||||||
|
|
||||||
setReplyStore: (comment: Comment) =>
|
setReplyStore: (comment: Comment) =>
|
||||||
set((state) => {
|
set((state) => {
|
||||||
const { subplebbitAddress, parentCid, author, displayName, content, link, signer, spoiler } = comment;
|
const { subplebbitAddress, parentCid, author, content, link, signer, spoiler } = comment;
|
||||||
const updatedAuthor = displayName ? { ...author, displayName } : author;
|
|
||||||
|
const displayName = 'displayName' in comment ? comment.displayName || undefined : author?.displayName;
|
||||||
|
|
||||||
|
const baseAuthor = author ? { ...author } : {};
|
||||||
|
delete baseAuthor.displayName;
|
||||||
|
|
||||||
|
const updatedAuthor = displayName ? { ...baseAuthor, displayName } : baseAuthor;
|
||||||
|
|
||||||
const publishCommentOptions: PublishCommentOptions = {
|
const publishCommentOptions: PublishCommentOptions = {
|
||||||
subplebbitAddress,
|
subplebbitAddress,
|
||||||
@@ -48,7 +54,7 @@ const usePublishReplyStore = create<ReplyState>((set) => ({
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if (updatedAuthor) {
|
if (Object.keys(updatedAuthor).length > 0) {
|
||||||
publishCommentOptions.author = updatedAuthor;
|
publishCommentOptions.author = updatedAuthor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user