From fcd1ce65fb123735c616b4b348955b7372923240 Mon Sep 17 00:00:00 2001 From: "Tom (plebeius.eth)" Date: Wed, 4 Sep 2024 16:15:47 +0200 Subject: [PATCH] fix edit menu logic --- src/components/edit-menu/edit-menu.tsx | 146 ++++++++++++++---- src/components/post-desktop/post-desktop.tsx | 5 +- .../post-menu-mobile/post-menu-mobile.tsx | 8 +- src/hooks/use-author-privileges.ts | 20 ++- 4 files changed, 130 insertions(+), 49 deletions(-) diff --git a/src/components/edit-menu/edit-menu.tsx b/src/components/edit-menu/edit-menu.tsx index bfb051ae..e8715eae 100644 --- a/src/components/edit-menu/edit-menu.tsx +++ b/src/components/edit-menu/edit-menu.tsx @@ -8,16 +8,11 @@ import useChallengesStore from '../../stores/use-challenges-store'; import _ from 'lodash'; import useIsMobile from '../../hooks/use-is-mobile'; import useAnonMode from '../../hooks/use-anon-mode'; +import useAuthorPrivileges from '../../hooks/use-author-privileges'; +import useAnonModeStore from '../../stores/use-anon-mode-store'; const { addChallenge } = useChallengesStore.getState(); -type EditMenuProps = { - isAccountMod?: boolean; - isAccountCommentAuthor?: boolean; - isCommentAuthorMod?: boolean; - post: Comment; -}; - const daysToTimestampInSeconds = (days: number) => { const now = new Date(); now.setDate(now.getDate() + days); @@ -29,29 +24,55 @@ const timestampToDays = (timestamp: number) => { return Math.max(1, Math.floor((timestamp - now) / (24 * 60 * 60))); }; -const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, post }: EditMenuProps) => { +const EditMenu = ({ post }: { post: Comment }) => { const { t } = useTranslation(); const isMobile = useIsMobile(); - const { author, cid, commentAuthor, content, deleted, locked, parentCid, pinned, reason, removed, spoiler, subplebbitAddress } = post || {}; + const { author, cid, commentAuthor, content, deleted, locked, parentCid, pinned, postCid, reason, removed, spoiler, subplebbitAddress } = post || {}; const isReply = parentCid; const [isEditMenuOpen, setIsEditMenuOpen] = useState(false); const [isContentEditorOpen, setIsContentEditorOpen] = useState(false); - const { getExistingSigner } = useAnonMode(post?.postCid); - const account = useAccount(); + const { getNewSigner, getExistingSigner } = useAnonMode(post?.postCid); + const { getThreadSigner } = useAnonModeStore(); + const [signer, setSigner] = useState(account?.signer); + const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor } = useAuthorPrivileges({ + commentAuthorAddress: author?.address, + subplebbitAddress, + postCid, + }); + const checkSigner = useCallback(async () => { - if (isAccountCommentAuthor && post?.author?.address !== account?.author?.address) { - const existingSigner = getExistingSigner(post?.author?.address); - if (existingSigner) { - setSigner(existingSigner); + if (isAccountCommentAuthor) { + if (author?.address !== account?.author?.address) { + // Check for existing thread signer first + const threadSigner = getThreadSigner(postCid); + if (threadSigner && threadSigner.address === author?.address) { + setSigner(threadSigner); + return; + } + + // If no thread signer, check for existing address signer + const existingSigner = getExistingSigner(author?.address); + if (existingSigner) { + setSigner(existingSigner); + return; + } + + // If no existing signer, create a new one + const newSigner = await getNewSigner(); + if (newSigner) { + setSigner(newSigner); + } + } else { + setSigner(account?.signer); } } else { setSigner(null); } - }, [isAccountCommentAuthor, post?.author?.address, getExistingSigner, account?.author?.address]); + }, [isAccountCommentAuthor, author?.address, postCid, account?.author?.address, account?.signer, getThreadSigner, getExistingSigner, getNewSigner]); useEffect(() => { checkSigner(); @@ -59,7 +80,7 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po const defaultPublishEditOptions = useMemo(() => { return { - commentAuthor: isAccountMod && !isAccountCommentAuthor ? commentAuthor : undefined, + commentAuthor: !isCommentAuthorMod && isAccountMod && !isAccountCommentAuthor ? commentAuthor : undefined, commentCid: cid, content: isAccountCommentAuthor ? content : undefined, deleted: isAccountCommentAuthor ? deleted ?? false : undefined, @@ -69,13 +90,6 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po removed: isAccountMod ? removed ?? false : undefined, spoiler: spoiler ?? false, subplebbitAddress, - signer: signer, - author: signer - ? { - address: signer.address, - displayName: post?.author?.displayName, - } - : author, onChallenge: (...args: any) => addChallenge([...args, post]), onChallengeVerification: alertChallengeVerificationFailed, onError: (error: Error) => { @@ -83,10 +97,58 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po alert('Comment edit failed. ' + error.message); }, }; - }, [isAccountMod, isAccountCommentAuthor, commentAuthor, cid, content, deleted, locked, pinned, reason, removed, spoiler, subplebbitAddress, signer, post]); + }, [isAccountMod, isAccountCommentAuthor, commentAuthor, cid, content, deleted, locked, pinned, reason, removed, spoiler, subplebbitAddress, post, isCommentAuthorMod]); const [publishCommentEditOptions, setPublishCommentEditOptions] = useState(defaultPublishEditOptions); - const { publishCommentEdit } = usePublishCommentEdit(publishCommentEditOptions); + + const authorEditOptions = useMemo( + () => ({ + commentCid: cid, + subplebbitAddress, + signer, + author: + signer && signer.address === author?.address + ? { + address: signer.address, + displayName: post?.author?.displayName, + } + : account?.author, + content: publishCommentEditOptions.content, + deleted: publishCommentEditOptions.deleted, + reason: publishCommentEditOptions.reason, + spoiler: publishCommentEditOptions.spoiler, + onChallenge: (...args: any) => addChallenge([...args, post]), + onChallengeVerification: alertChallengeVerificationFailed, + onError: (error: Error) => { + console.warn(error); + alert('Comment edit failed. ' + error.message); + }, + }), + [publishCommentEditOptions, cid, subplebbitAddress, signer, post, account?.author, author?.address], + ); + + const modEditOptions = useMemo( + () => ({ + commentCid: cid, + subplebbitAddress, + locked: parentCid === undefined ? publishCommentEditOptions.locked : undefined, + pinned: publishCommentEditOptions.pinned, + removed: publishCommentEditOptions.removed, + reason: publishCommentEditOptions.reason, + commentAuthor: !isCommentAuthorMod ? publishCommentEditOptions.commentAuthor : undefined, + author: account?.author, + onChallenge: (...args: any) => addChallenge([...args, post]), + onChallengeVerification: alertChallengeVerificationFailed, + onError: (error: Error) => { + console.warn(error); + alert('Comment edit failed. ' + error.message); + }, + }), + [publishCommentEditOptions, cid, subplebbitAddress, isCommentAuthorMod, post, account?.author, parentCid], + ); + + const { publishCommentEdit: publishAuthorEdit } = usePublishCommentEdit(authorEditOptions); + const { publishCommentEdit: publishModEdit } = usePublishCommentEdit(modEditOptions); useEffect(() => { setPublishCommentEditOptions(defaultPublishEditOptions); @@ -117,8 +179,6 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po })); }; - const onReason = (e: React.ChangeEvent) => setPublishCommentEditOptions((state) => ({ ...state, reason: e.target.value })); - const { refs, floatingStyles, context } = useFloating({ placement: 'bottom-start', open: isEditMenuOpen, @@ -137,7 +197,14 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po const _publishCommentEdit = async () => { try { - await publishCommentEdit(); + if (isAccountCommentAuthor && isAccountMod) { + await publishAuthorEdit(); + await publishModEdit(); + } else if (isAccountCommentAuthor) { + await publishAuthorEdit(); + } else if (isAccountMod) { + await publishModEdit(); + } } catch (error) { if (error instanceof Error) { console.warn(error); @@ -176,8 +243,11 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po