mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix edit menu logic
This commit is contained in:
@@ -8,16 +8,11 @@ import useChallengesStore from '../../stores/use-challenges-store';
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import useIsMobile from '../../hooks/use-is-mobile';
|
import useIsMobile from '../../hooks/use-is-mobile';
|
||||||
import useAnonMode from '../../hooks/use-anon-mode';
|
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();
|
const { addChallenge } = useChallengesStore.getState();
|
||||||
|
|
||||||
type EditMenuProps = {
|
|
||||||
isAccountMod?: boolean;
|
|
||||||
isAccountCommentAuthor?: boolean;
|
|
||||||
isCommentAuthorMod?: boolean;
|
|
||||||
post: Comment;
|
|
||||||
};
|
|
||||||
|
|
||||||
const daysToTimestampInSeconds = (days: number) => {
|
const daysToTimestampInSeconds = (days: number) => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
now.setDate(now.getDate() + days);
|
now.setDate(now.getDate() + days);
|
||||||
@@ -29,29 +24,55 @@ const timestampToDays = (timestamp: number) => {
|
|||||||
return Math.max(1, Math.floor((timestamp - now) / (24 * 60 * 60)));
|
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 { t } = useTranslation();
|
||||||
const isMobile = useIsMobile();
|
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 isReply = parentCid;
|
||||||
const [isEditMenuOpen, setIsEditMenuOpen] = useState(false);
|
const [isEditMenuOpen, setIsEditMenuOpen] = useState(false);
|
||||||
const [isContentEditorOpen, setIsContentEditorOpen] = useState(false);
|
const [isContentEditorOpen, setIsContentEditorOpen] = useState(false);
|
||||||
|
|
||||||
const { getExistingSigner } = useAnonMode(post?.postCid);
|
|
||||||
|
|
||||||
const account = useAccount();
|
const account = useAccount();
|
||||||
|
const { getNewSigner, getExistingSigner } = useAnonMode(post?.postCid);
|
||||||
|
const { getThreadSigner } = useAnonModeStore();
|
||||||
|
|
||||||
const [signer, setSigner] = useState<any>(account?.signer);
|
const [signer, setSigner] = useState<any>(account?.signer);
|
||||||
|
|
||||||
|
const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor } = useAuthorPrivileges({
|
||||||
|
commentAuthorAddress: author?.address,
|
||||||
|
subplebbitAddress,
|
||||||
|
postCid,
|
||||||
|
});
|
||||||
|
|
||||||
const checkSigner = useCallback(async () => {
|
const checkSigner = useCallback(async () => {
|
||||||
if (isAccountCommentAuthor && post?.author?.address !== account?.author?.address) {
|
if (isAccountCommentAuthor) {
|
||||||
const existingSigner = getExistingSigner(post?.author?.address);
|
if (author?.address !== account?.author?.address) {
|
||||||
if (existingSigner) {
|
// Check for existing thread signer first
|
||||||
setSigner(existingSigner);
|
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 {
|
} else {
|
||||||
setSigner(null);
|
setSigner(null);
|
||||||
}
|
}
|
||||||
}, [isAccountCommentAuthor, post?.author?.address, getExistingSigner, account?.author?.address]);
|
}, [isAccountCommentAuthor, author?.address, postCid, account?.author?.address, account?.signer, getThreadSigner, getExistingSigner, getNewSigner]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
checkSigner();
|
checkSigner();
|
||||||
@@ -59,7 +80,7 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po
|
|||||||
|
|
||||||
const defaultPublishEditOptions = useMemo(() => {
|
const defaultPublishEditOptions = useMemo(() => {
|
||||||
return {
|
return {
|
||||||
commentAuthor: isAccountMod && !isAccountCommentAuthor ? commentAuthor : undefined,
|
commentAuthor: !isCommentAuthorMod && isAccountMod && !isAccountCommentAuthor ? commentAuthor : undefined,
|
||||||
commentCid: cid,
|
commentCid: cid,
|
||||||
content: isAccountCommentAuthor ? content : undefined,
|
content: isAccountCommentAuthor ? content : undefined,
|
||||||
deleted: isAccountCommentAuthor ? deleted ?? false : undefined,
|
deleted: isAccountCommentAuthor ? deleted ?? false : undefined,
|
||||||
@@ -69,13 +90,6 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po
|
|||||||
removed: isAccountMod ? removed ?? false : undefined,
|
removed: isAccountMod ? removed ?? false : undefined,
|
||||||
spoiler: spoiler ?? false,
|
spoiler: spoiler ?? false,
|
||||||
subplebbitAddress,
|
subplebbitAddress,
|
||||||
signer: signer,
|
|
||||||
author: signer
|
|
||||||
? {
|
|
||||||
address: signer.address,
|
|
||||||
displayName: post?.author?.displayName,
|
|
||||||
}
|
|
||||||
: author,
|
|
||||||
onChallenge: (...args: any) => addChallenge([...args, post]),
|
onChallenge: (...args: any) => addChallenge([...args, post]),
|
||||||
onChallengeVerification: alertChallengeVerificationFailed,
|
onChallengeVerification: alertChallengeVerificationFailed,
|
||||||
onError: (error: Error) => {
|
onError: (error: Error) => {
|
||||||
@@ -83,10 +97,58 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po
|
|||||||
alert('Comment edit failed. ' + error.message);
|
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<PublishCommentEditOptions>(defaultPublishEditOptions);
|
const [publishCommentEditOptions, setPublishCommentEditOptions] = useState<PublishCommentEditOptions>(defaultPublishEditOptions);
|
||||||
const { publishCommentEdit } = usePublishCommentEdit(publishCommentEditOptions);
|
|
||||||
|
const authorEditOptions = useMemo<PublishCommentEditOptions>(
|
||||||
|
() => ({
|
||||||
|
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<PublishCommentEditOptions>(
|
||||||
|
() => ({
|
||||||
|
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(() => {
|
useEffect(() => {
|
||||||
setPublishCommentEditOptions(defaultPublishEditOptions);
|
setPublishCommentEditOptions(defaultPublishEditOptions);
|
||||||
@@ -117,8 +179,6 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po
|
|||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
const onReason = (e: React.ChangeEvent<HTMLInputElement>) => setPublishCommentEditOptions((state) => ({ ...state, reason: e.target.value }));
|
|
||||||
|
|
||||||
const { refs, floatingStyles, context } = useFloating({
|
const { refs, floatingStyles, context } = useFloating({
|
||||||
placement: 'bottom-start',
|
placement: 'bottom-start',
|
||||||
open: isEditMenuOpen,
|
open: isEditMenuOpen,
|
||||||
@@ -137,7 +197,14 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po
|
|||||||
|
|
||||||
const _publishCommentEdit = async () => {
|
const _publishCommentEdit = async () => {
|
||||||
try {
|
try {
|
||||||
await publishCommentEdit();
|
if (isAccountCommentAuthor && isAccountMod) {
|
||||||
|
await publishAuthorEdit();
|
||||||
|
await publishModEdit();
|
||||||
|
} else if (isAccountCommentAuthor) {
|
||||||
|
await publishAuthorEdit();
|
||||||
|
} else if (isAccountMod) {
|
||||||
|
await publishModEdit();
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
console.warn(error);
|
console.warn(error);
|
||||||
@@ -176,8 +243,11 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po
|
|||||||
<div>
|
<div>
|
||||||
<textarea
|
<textarea
|
||||||
className={styles.editTextarea}
|
className={styles.editTextarea}
|
||||||
defaultValue={publishCommentEditOptions.content ?? ''}
|
value={publishCommentEditOptions.content || ''}
|
||||||
onChange={(e) => setPublishCommentEditOptions((state) => ({ ...state, content: e.target.value }))}
|
onChange={(e) => {
|
||||||
|
const newContent = e.target.value;
|
||||||
|
setPublishCommentEditOptions((state) => ({ ...state, content: newContent }));
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -218,7 +288,7 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po
|
|||||||
</label>
|
</label>
|
||||||
]
|
]
|
||||||
</div>
|
</div>
|
||||||
{!isCommentAuthorMod && (
|
{!isCommentAuthorMod && isAccountMod && !isAccountCommentAuthor && (
|
||||||
<div className={styles.menuItem}>
|
<div className={styles.menuItem}>
|
||||||
[
|
[
|
||||||
<label>
|
<label>
|
||||||
@@ -227,7 +297,7 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po
|
|||||||
i18nKey='ban_user_for'
|
i18nKey='ban_user_for'
|
||||||
shouldUnescape={true}
|
shouldUnescape={true}
|
||||||
components={{
|
components={{
|
||||||
1: <input className={styles.banInput} onChange={onBanDurationChange} type='number' min={1} max={100} value={banDuration} />,
|
1: <input className={styles.banInput} onChange={onBanDurationChange} type='number' min={1} max={100} value={banDuration || ''} />,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
?
|
?
|
||||||
@@ -239,7 +309,15 @@ const EditMenu = ({ isAccountMod, isAccountCommentAuthor, isCommentAuthorMod, po
|
|||||||
)}
|
)}
|
||||||
<div className={`${styles.menuItem} ${styles.menuReason}`}>
|
<div className={`${styles.menuItem} ${styles.menuReason}`}>
|
||||||
{_.capitalize(t('reason'))}? ({t('optional')})
|
{_.capitalize(t('reason'))}? ({t('optional')})
|
||||||
<input type='text' onChange={onReason} value={publishCommentEditOptions.reason} size={14} />
|
<input
|
||||||
|
type='text'
|
||||||
|
value={publishCommentEditOptions.reason || ''}
|
||||||
|
onChange={(e) => {
|
||||||
|
const newReason = e.target.value;
|
||||||
|
setPublishCommentEditOptions((state) => ({ ...state, reason: newReason }));
|
||||||
|
}}
|
||||||
|
size={14}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className={styles.bottom}>
|
<div className={styles.bottom}>
|
||||||
<button className={isMobile ? 'button' : ''} onClick={_publishCommentEdit}>
|
<button className={isMobile ? 'button' : ''} onClick={_publishCommentEdit}>
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import useAnonModeStore from '../../stores/use-anon-mode-store';
|
|||||||
import useAvatarVisibilityStore from '../../stores/use-avatar-visibility-store';
|
import useAvatarVisibilityStore from '../../stores/use-avatar-visibility-store';
|
||||||
import useAnonMode from '../../hooks/use-anon-mode';
|
import useAnonMode from '../../hooks/use-anon-mode';
|
||||||
import useAuthorAddressClick from '../../hooks/use-author-address-click';
|
import useAuthorAddressClick from '../../hooks/use-author-address-click';
|
||||||
import useEditCommentPrivileges from '../../hooks/use-author-privileges';
|
|
||||||
import useCountLinksInReplies from '../../hooks/use-count-links-in-replies';
|
import useCountLinksInReplies from '../../hooks/use-count-links-in-replies';
|
||||||
import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame';
|
import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame';
|
||||||
import useHide from '../../hooks/use-hide';
|
import useHide from '../../hooks/use-hide';
|
||||||
@@ -75,8 +74,6 @@ const PostInfo = ({ openReplyModal, post, postReplyCount = 0, roles, isHidden }:
|
|||||||
const account = useAccount();
|
const account = useAccount();
|
||||||
const pendingShortAddress = anonMode ? currentAnonSignerAddress && Plebbit.getShortAddress(currentAnonSignerAddress) : account?.author?.shortAddress;
|
const pendingShortAddress = anonMode ? currentAnonSignerAddress && Plebbit.getShortAddress(currentAnonSignerAddress) : account?.author?.shortAddress;
|
||||||
|
|
||||||
const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor } = useEditCommentPrivileges({ commentAuthorAddress: address, subplebbitAddress });
|
|
||||||
|
|
||||||
const handleUserAddressClick = useAuthorAddressClick();
|
const handleUserAddressClick = useAuthorAddressClick();
|
||||||
const numberOfPostsByAuthor = document.querySelectorAll(`[data-author-address="${shortAddress}"][data-post-cid="${postCid}"]`).length;
|
const numberOfPostsByAuthor = document.querySelectorAll(`[data-author-address="${shortAddress}"][data-post-cid="${postCid}"]`).length;
|
||||||
|
|
||||||
@@ -86,7 +83,7 @@ const PostInfo = ({ openReplyModal, post, postReplyCount = 0, roles, isHidden }:
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.postInfo}>
|
<div className={styles.postInfo}>
|
||||||
{!isHidden && <EditMenu isAccountCommentAuthor={isAccountCommentAuthor} isAccountMod={isAccountMod} isCommentAuthorMod={isCommentAuthorMod} post={post} />}
|
{!isHidden && <EditMenu post={post} />}
|
||||||
{title &&
|
{title &&
|
||||||
(title.length <= 75 ? (
|
(title.length <= 75 ? (
|
||||||
<span className={styles.subject}>{title} </span>
|
<span className={styles.subject}>{title} </span>
|
||||||
|
|||||||
@@ -130,8 +130,8 @@ const BlockBoardButton = ({ address }: { address: string }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const PostMenuMobile = ({ post }: { post: Comment }) => {
|
const PostMenuMobile = ({ post }: { post: Comment }) => {
|
||||||
const { author, cid, isDescription, isRules, link, parentCid, subplebbitAddress } = post || {};
|
const { author, cid, isDescription, isRules, link, parentCid, postCid, subplebbitAddress } = post || {};
|
||||||
const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor } = useEditCommentPrivileges({ commentAuthorAddress: author?.address, subplebbitAddress });
|
const { isAccountMod, isAccountCommentAuthor } = useEditCommentPrivileges({ commentAuthorAddress: author?.address, subplebbitAddress });
|
||||||
const commentMediaInfo = getCommentMediaInfo(post);
|
const commentMediaInfo = getCommentMediaInfo(post);
|
||||||
const { thumbnail, type, url } = commentMediaInfo || {};
|
const { thumbnail, type, url } = commentMediaInfo || {};
|
||||||
const [isMenuOpen, setIsMenuOpen] = useState(false);
|
const [isMenuOpen, setIsMenuOpen] = useState(false);
|
||||||
@@ -169,7 +169,7 @@ const PostMenuMobile = ({ post }: { post: Comment }) => {
|
|||||||
<FloatingFocusManager context={context} modal={false}>
|
<FloatingFocusManager context={context} modal={false}>
|
||||||
<div className={styles.postMenu} ref={refs.setFloating} style={floatingStyles} aria-labelledby={headingId} {...getFloatingProps()}>
|
<div className={styles.postMenu} ref={refs.setFloating} style={floatingStyles} aria-labelledby={headingId} {...getFloatingProps()}>
|
||||||
{cid && subplebbitAddress && <CopyLinkButton cid={cid} subplebbitAddress={subplebbitAddress} onClose={handleClose} />}
|
{cid && subplebbitAddress && <CopyLinkButton cid={cid} subplebbitAddress={subplebbitAddress} onClose={handleClose} />}
|
||||||
{cid && subplebbitAddress && <HidePostButton cid={cid} isReply={parentCid} postCid={post.postCid} onClose={handleClose} />}
|
{cid && subplebbitAddress && <HidePostButton cid={cid} isReply={parentCid} postCid={postCid} onClose={handleClose} />}
|
||||||
{cid && subplebbitAddress && !isDescription && !isRules && <BlockUserButton address={author?.address} />}
|
{cid && subplebbitAddress && !isDescription && !isRules && <BlockUserButton address={author?.address} />}
|
||||||
{cid && subplebbitAddress && !isInBoardView && !isDescription && !isRules && <BlockBoardButton address={subplebbitAddress} />}
|
{cid && subplebbitAddress && !isInBoardView && !isDescription && !isRules && <BlockBoardButton address={subplebbitAddress} />}
|
||||||
{link && isValidURL(link) && (type === 'image' || type === 'gif' || thumbnail) && url && <ImageSearchButtons url={url} onClose={handleClose} />}
|
{link && isValidURL(link) && (type === 'image' || type === 'gif' || thumbnail) && url && <ImageSearchButtons url={url} onClose={handleClose} />}
|
||||||
@@ -180,7 +180,7 @@ const PostMenuMobile = ({ post }: { post: Comment }) => {
|
|||||||
)}
|
)}
|
||||||
{(isAccountMod || isAccountCommentAuthor) && cid && (
|
{(isAccountMod || isAccountCommentAuthor) && cid && (
|
||||||
<span className={styles.checkbox}>
|
<span className={styles.checkbox}>
|
||||||
<EditMenu isAccountCommentAuthor={isAccountCommentAuthor} isAccountMod={isAccountMod} isCommentAuthorMod={isCommentAuthorMod} post={post} />
|
<EditMenu post={post} />
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -1,16 +1,20 @@
|
|||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { useAccount, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
import { useAccount, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
||||||
import useAnonModeStore from '../stores/use-anon-mode-store';
|
import useAnonModeStore from '../stores/use-anon-mode-store';
|
||||||
|
import useAnonMode from './use-anon-mode';
|
||||||
|
|
||||||
interface AuthorPrivilegesProps {
|
interface AuthorPrivilegesProps {
|
||||||
commentAuthorAddress: string;
|
commentAuthorAddress: string;
|
||||||
subplebbitAddress: string;
|
subplebbitAddress: string;
|
||||||
|
postCid?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const useAuthorPrivileges = ({ commentAuthorAddress, subplebbitAddress }: AuthorPrivilegesProps) => {
|
const useAuthorPrivileges = ({ commentAuthorAddress, subplebbitAddress, postCid }: AuthorPrivilegesProps) => {
|
||||||
const accountAuthorAddress = useAccount()?.author?.address;
|
const account = useAccount();
|
||||||
|
const accountAuthorAddress = account?.author?.address;
|
||||||
const { roles } = useSubplebbit({ subplebbitAddress }) || {};
|
const { roles } = useSubplebbit({ subplebbitAddress }) || {};
|
||||||
const { getAddressSigner } = useAnonModeStore();
|
const { getAddressSigner, getThreadSigner } = useAnonModeStore();
|
||||||
|
const { anonMode } = useAnonMode(postCid);
|
||||||
|
|
||||||
const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole } = useMemo(() => {
|
const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole } = useMemo(() => {
|
||||||
const commentAuthorRole = roles?.[commentAuthorAddress]?.role;
|
const commentAuthorRole = roles?.[commentAuthorAddress]?.role;
|
||||||
@@ -20,13 +24,15 @@ const useAuthorPrivileges = ({ commentAuthorAddress, subplebbitAddress }: Author
|
|||||||
|
|
||||||
let isAccountCommentAuthor = accountAuthorAddress === commentAuthorAddress;
|
let isAccountCommentAuthor = accountAuthorAddress === commentAuthorAddress;
|
||||||
|
|
||||||
if (!isAccountCommentAuthor) {
|
if (!isAccountCommentAuthor && anonMode) {
|
||||||
const existingSigner = getAddressSigner(commentAuthorAddress);
|
const addressSigner = getAddressSigner(commentAuthorAddress);
|
||||||
isAccountCommentAuthor = !!existingSigner;
|
const threadSigner = postCid ? getThreadSigner(postCid) : null;
|
||||||
|
|
||||||
|
isAccountCommentAuthor = (addressSigner && addressSigner.address === commentAuthorAddress) || (threadSigner && threadSigner.address === commentAuthorAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole };
|
return { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole };
|
||||||
}, [roles, commentAuthorAddress, accountAuthorAddress, getAddressSigner]);
|
}, [roles, commentAuthorAddress, accountAuthorAddress, anonMode, getAddressSigner, getThreadSigner, postCid]);
|
||||||
|
|
||||||
return { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole };
|
return { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole };
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user