refactor: migrate 5chan to the community hooks API (#1073)

* refactor(community-api): migrate 5chan to community hooks

* fix(review): address PR feedback

* fix(review): preserve legacy board context fallbacks

* fix(review): address latest bot feedback

* fix(review): use communityAddress in edit menu privileges
This commit is contained in:
Tommaso Casaburi
2026-03-13 13:25:10 +08:00
committed by GitHub
parent 9dc4d96d27
commit d7703953fb
105 changed files with 2659 additions and 1491 deletions
@@ -20,6 +20,7 @@ const testState = vi.hoisted(() => ({
} as Record<string, any>,
addChallengeMock: vi.fn(),
authorOptions: undefined as Record<string, any> | undefined,
authorPrivilegesOptions: undefined as Record<string, any> | undefined,
isMobile: false,
modOptions: undefined as Record<string, any> | undefined,
privileges: {
@@ -84,7 +85,10 @@ vi.mock('@bitsocialnet/bitsocial-react-hooks', () => ({
}));
vi.mock('../../../hooks/use-author-privileges', () => ({
default: () => testState.privileges,
default: (options: Record<string, any>) => {
testState.authorPrivilegesOptions = options;
return testState.privileges;
},
}));
vi.mock('../../../hooks/use-is-mobile', () => ({
@@ -122,7 +126,7 @@ const basePost = {
reason: '',
removed: false,
spoiler: false,
subplebbitAddress: 'music-posting.eth',
communityAddress: 'music-posting.eth',
} as Record<string, any>;
const renderMenu = async (post = basePost) => {
@@ -177,6 +181,7 @@ describe('EditMenu', () => {
},
};
testState.authorOptions = undefined;
testState.authorPrivilegesOptions = undefined;
testState.isMobile = false;
testState.modOptions = undefined;
testState.privileges = {
@@ -248,8 +253,14 @@ describe('EditMenu', () => {
deleted: true,
reason: 'cleanup',
spoiler: false,
subplebbitAddress: 'music-posting.eth',
communityAddress: 'music-posting.eth',
});
expect(testState.authorPrivilegesOptions).toMatchObject({
commentAuthorAddress: '0xauthor',
communityAddress: 'music-posting.eth',
postCid: 'post-1',
});
expect(testState.authorPrivilegesOptions).not.toHaveProperty('subplebbitAddress');
});
it('lets moderators change moderation flags, ban duration, and save them', async () => {
@@ -287,7 +298,7 @@ describe('EditMenu', () => {
shortAddress: '0xmod',
},
commentCid: 'comment-1',
subplebbitAddress: 'music-posting.eth',
communityAddress: 'music-posting.eth',
});
expect(testState.modOptions?.commentModeration).toMatchObject({
reason: 'rule violation',
+17 -14
View File
@@ -15,6 +15,7 @@ import useChallengesStore from '../../stores/use-challenges-store';
import capitalize from 'lodash/capitalize';
import useIsMobile from '../../hooks/use-is-mobile';
import useAuthorPrivileges from '../../hooks/use-author-privileges';
import { getCommentCommunityAddress, withResolvedCommentCommunityAddress } from '../../lib/utils/comment-utils';
const { addChallenge } = useChallengesStore.getState();
@@ -32,30 +33,32 @@ const timestampToDays = (timestamp: number) => {
const EditMenu = ({ post }: { post: Comment }) => {
const { t } = useTranslation();
const isMobile = useIsMobile();
const { author, cid, content, deleted, locked, parentCid, pinned, postCid, reason, removed, spoiler, subplebbitAddress } = post || {};
const authorDisplayName = post?.author?.displayName;
const modBanExpiresAt = post?.commentModeration?.author?.banExpiresAt;
const purged = post?.commentModeration?.purged ?? false;
const resolvedPost = withResolvedCommentCommunityAddress(post);
const { author, cid, content, deleted, locked, parentCid, pinned, postCid, reason, removed, spoiler } = resolvedPost || {};
const communityAddress = getCommentCommunityAddress(resolvedPost);
const authorDisplayName = resolvedPost?.author?.displayName;
const modBanExpiresAt = resolvedPost?.commentModeration?.author?.banExpiresAt;
const purged = resolvedPost?.commentModeration?.purged ?? false;
const [isEditMenuOpen, setIsEditMenuOpen] = useState(false);
const [isContentEditorOpen, setIsContentEditorOpen] = useState(false);
const account = useAccount();
const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor } = useAuthorPrivileges({
commentAuthorAddress: author?.address,
subplebbitAddress,
communityAddress: communityAddress || '',
postCid,
});
const signer = isAccountCommentAuthor ? account?.signer : null;
const latestPostRef = useRef(post);
const latestPostRef = useRef(resolvedPost);
useEffect(() => {
latestPostRef.current = post;
}, [post]);
latestPostRef.current = resolvedPost;
}, [resolvedPost]);
const onChallenge = useCallback((...args: any) => addChallenge([...args, latestPostRef.current]), []);
const defaultPublishEditOptions = useMemo(() => {
return {
commentCid: cid,
subplebbitAddress,
communityAddress,
// Author edit properties
content: isAccountCommentAuthor ? content : undefined,
deleted: isAccountCommentAuthor ? (deleted ?? false) : undefined,
@@ -79,14 +82,14 @@ const EditMenu = ({ post }: { post: Comment }) => {
alert('Comment edit failed. ' + error.message);
},
};
}, [isAccountMod, isAccountCommentAuthor, cid, content, deleted, locked, pinned, reason, removed, purged, spoiler, subplebbitAddress, modBanExpiresAt, onChallenge]);
}, [isAccountMod, isAccountCommentAuthor, cid, content, deleted, locked, pinned, reason, removed, purged, spoiler, communityAddress, modBanExpiresAt, onChallenge]);
const [publishCommentEditOptions, setPublishCommentEditOptions] = useState<PublishCommentEditOptions>(defaultPublishEditOptions);
const authorEditOptions = useMemo<PublishCommentEditOptions>(
() => ({
commentCid: cid,
subplebbitAddress,
communityAddress,
signer,
author: signer?.address === author?.address ? { address: signer?.address, displayName: authorDisplayName } : account?.author,
content: publishCommentEditOptions.content,
@@ -100,13 +103,13 @@ const EditMenu = ({ post }: { post: Comment }) => {
alert('Comment edit failed. ' + error.message);
},
}),
[publishCommentEditOptions, cid, subplebbitAddress, signer, account?.author, author?.address, authorDisplayName, onChallenge],
[publishCommentEditOptions, cid, communityAddress, signer, account?.author, author?.address, authorDisplayName, onChallenge],
);
const modEditOptions = useMemo<PublishCommentModerationOptions>(
() => ({
commentCid: cid,
subplebbitAddress,
communityAddress,
commentModeration: {
locked: parentCid === undefined ? publishCommentEditOptions.commentModeration?.locked : undefined,
pinned: publishCommentEditOptions.commentModeration?.pinned,
@@ -124,7 +127,7 @@ const EditMenu = ({ post }: { post: Comment }) => {
alert('Comment moderation failed. ' + error.message);
},
}),
[publishCommentEditOptions, cid, subplebbitAddress, account?.author, parentCid, onChallenge],
[publishCommentEditOptions, cid, communityAddress, account?.author, parentCid, onChallenge],
);
const { publishCommentEdit: publishAuthorEdit } = usePublishCommentEdit(authorEditOptions);