Add collection report modal (#37961)

This commit is contained in:
diondiondion
2026-02-24 15:29:46 +01:00
committed by GitHub
parent 72406a1cd1
commit 919b1e69b8
8 changed files with 476 additions and 148 deletions

View File

@@ -2,11 +2,13 @@ import { useCallback, useMemo } from 'react';
import { defineMessages, useIntl } from 'react-intl'; import { defineMessages, useIntl } from 'react-intl';
import { useAccount } from '@/mastodon/hooks/useAccount';
import MoreVertIcon from '@/material-icons/400-24px/more_vert.svg?react'; import MoreVertIcon from '@/material-icons/400-24px/more_vert.svg?react';
import { openModal } from 'mastodon/actions/modal'; import { openModal } from 'mastodon/actions/modal';
import type { ApiCollectionJSON } from 'mastodon/api_types/collections'; import type { ApiCollectionJSON } from 'mastodon/api_types/collections';
import { Dropdown } from 'mastodon/components/dropdown_menu'; import { Dropdown } from 'mastodon/components/dropdown_menu';
import { IconButton } from 'mastodon/components/icon_button'; import { IconButton } from 'mastodon/components/icon_button';
import { me } from 'mastodon/initial_state';
import { useAppDispatch } from 'mastodon/store'; import { useAppDispatch } from 'mastodon/store';
import { messages as editorMessages } from '../editor'; import { messages as editorMessages } from '../editor';
@@ -16,10 +18,18 @@ const messages = defineMessages({
id: 'collections.view_collection', id: 'collections.view_collection',
defaultMessage: 'View collection', defaultMessage: 'View collection',
}, },
viewOtherCollections: {
id: 'collections.view_other_collections_by_user',
defaultMessage: 'View other collections by this user',
},
delete: { delete: {
id: 'collections.delete_collection', id: 'collections.delete_collection',
defaultMessage: 'Delete collection', defaultMessage: 'Delete collection',
}, },
report: {
id: 'collections.report_collection',
defaultMessage: 'Report this collection',
},
more: { id: 'status.more', defaultMessage: 'More' }, more: { id: 'status.more', defaultMessage: 'More' },
}); });
@@ -31,9 +41,11 @@ export const CollectionMenu: React.FC<{
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const intl = useIntl(); const intl = useIntl();
const { id, name } = collection; const { id, name, account_id } = collection;
const isOwnCollection = account_id === me;
const ownerAccount = useAccount(account_id);
const handleDeleteClick = useCallback(() => { const openDeleteConfirmation = useCallback(() => {
dispatch( dispatch(
openModal({ openModal({
modalType: 'CONFIRM_DELETE_COLLECTION', modalType: 'CONFIRM_DELETE_COLLECTION',
@@ -45,34 +57,69 @@ export const CollectionMenu: React.FC<{
); );
}, [dispatch, id, name]); }, [dispatch, id, name]);
const menu = useMemo(() => { const openReportModal = useCallback(() => {
const commonItems = [ dispatch(
{ openModal({
text: intl.formatMessage(editorMessages.manageAccounts), modalType: 'REPORT_COLLECTION',
to: `/collections/${id}/edit`, modalProps: {
}, collection,
{ },
text: intl.formatMessage(editorMessages.editDetails), }),
to: `/collections/${id}/edit/details`, );
}, }, [collection, dispatch]);
null,
{
text: intl.formatMessage(messages.delete),
action: handleDeleteClick,
dangerous: true,
},
];
if (context === 'list') { const menu = useMemo(() => {
return [ if (isOwnCollection) {
{ text: intl.formatMessage(messages.view), to: `/collections/${id}` }, const commonItems = [
{
text: intl.formatMessage(editorMessages.manageAccounts),
to: `/collections/${id}/edit`,
},
{
text: intl.formatMessage(editorMessages.editDetails),
to: `/collections/${id}/edit/details`,
},
null, null,
...commonItems, {
text: intl.formatMessage(messages.delete),
action: openDeleteConfirmation,
dangerous: true,
},
];
if (context === 'list') {
return [
{ text: intl.formatMessage(messages.view), to: `/collections/${id}` },
null,
...commonItems,
];
} else {
return commonItems;
}
} else if (ownerAccount) {
return [
{
text: intl.formatMessage(messages.viewOtherCollections),
to: `/@${ownerAccount.acct}/featured`,
},
null,
{
text: intl.formatMessage(messages.report),
action: openReportModal,
},
]; ];
} else { } else {
return commonItems; return [];
} }
}, [intl, id, handleDeleteClick, context]); }, [
isOwnCollection,
intl,
id,
openDeleteConfirmation,
context,
ownerAccount,
openReportModal,
]);
return ( return (
<Dropdown scrollKey='collections' items={menu}> <Dropdown scrollKey='collections' items={menu}>

View File

@@ -79,7 +79,7 @@ const CollectionHeader: React.FC<{ collection: ApiCollectionJSON }> = ({
collection, collection,
}) => { }) => {
const intl = useIntl(); const intl = useIntl();
const { name, description, tag } = collection; const { name, description, tag, account_id } = collection;
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const handleShare = useCallback(() => { const handleShare = useCallback(() => {
@@ -114,7 +114,7 @@ const CollectionHeader: React.FC<{ collection: ApiCollectionJSON }> = ({
{description && <p className={classes.description}>{description}</p>} {description && <p className={classes.description}>{description}</p>}
<AuthorNote id={collection.account_id} /> <AuthorNote id={collection.account_id} />
<CollectionMetaData <CollectionMetaData
extended extended={account_id === me}
collection={collection} collection={collection}
className={classes.metaData} className={classes.metaData}
/> />

View File

@@ -1,121 +0,0 @@
import PropTypes from 'prop-types';
import { useCallback, useEffect, useRef } from 'react';
import { useIntl, defineMessages, FormattedMessage } from 'react-intl';
import { createSelector } from '@reduxjs/toolkit';
import { OrderedSet, List as ImmutableList } from 'immutable';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { shallowEqual } from 'react-redux';
import Toggle from 'react-toggle';
import { fetchAccount } from 'mastodon/actions/accounts';
import { Button } from 'mastodon/components/button';
import { useAppDispatch, useAppSelector } from 'mastodon/store';
const messages = defineMessages({
placeholder: { id: 'report.placeholder', defaultMessage: 'Type or paste additional comments' },
});
const selectRepliedToAccountIds = createSelector(
[
(state) => state.get('statuses'),
(_, statusIds) => statusIds,
],
(statusesMap, statusIds) => statusIds.map((statusId) => statusesMap.getIn([statusId, 'in_reply_to_account_id'])),
{
resultEqualityCheck: shallowEqual,
}
);
const Comment = ({ comment, domain, statusIds, isRemote, isSubmitting, selectedDomains, onSubmit, onChangeComment, onToggleDomain }) => {
const intl = useIntl();
const dispatch = useAppDispatch();
const loadedRef = useRef(false);
const handleClick = useCallback(() => onSubmit(), [onSubmit]);
const handleChange = useCallback((e) => onChangeComment(e.target.value), [onChangeComment]);
const handleToggleDomain = useCallback(e => onToggleDomain(e.target.value, e.target.checked), [onToggleDomain]);
const handleKeyDown = useCallback((e) => {
if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
handleClick();
}
}, [handleClick]);
// Memoize accountIds since we don't want it to trigger `useEffect` on each render
const accountIds = useAppSelector((state) => domain ? selectRepliedToAccountIds(state, statusIds) : ImmutableList());
// While we could memoize `availableDomains`, it is pretty inexpensive to recompute
const accountsMap = useAppSelector((state) => state.get('accounts'));
const availableDomains = domain ? OrderedSet([domain]).union(accountIds.map((accountId) => accountsMap.getIn([accountId, 'acct'], '').split('@')[1]).filter(domain => !!domain)) : OrderedSet();
useEffect(() => {
if (loadedRef.current) {
return;
}
loadedRef.current = true;
// First, pre-select known domains
availableDomains.forEach((domain) => {
onToggleDomain(domain, true);
});
// Then, fetch missing replied-to accounts
const unknownAccounts = OrderedSet(accountIds.filter(accountId => accountId && !accountsMap.has(accountId)));
unknownAccounts.forEach((accountId) => {
dispatch(fetchAccount(accountId));
});
});
return (
<>
<h3 className='report-dialog-modal__title'><FormattedMessage id='report.comment.title' defaultMessage='Is there anything else you think we should know?' /></h3>
<textarea
className='report-dialog-modal__textarea'
placeholder={intl.formatMessage(messages.placeholder)}
value={comment}
onChange={handleChange}
onKeyDown={handleKeyDown}
disabled={isSubmitting}
/>
{isRemote && (
<>
<p className='report-dialog-modal__lead'><FormattedMessage id='report.forward_hint' defaultMessage='The account is from another server. Send an anonymized copy of the report there as well?' /></p>
{ availableDomains.map((domain) => (
<label className='report-dialog-modal__toggle' key={`toggle-${domain}`}>
<Toggle checked={selectedDomains.includes(domain)} disabled={isSubmitting} onChange={handleToggleDomain} value={domain} />
<FormattedMessage id='report.forward' defaultMessage='Forward to {target}' values={{ target: domain }} />
</label>
))}
</>
)}
<div className='flex-spacer' />
<div className='report-dialog-modal__actions'>
<Button onClick={handleClick} disabled={isSubmitting}><FormattedMessage id='report.submit' defaultMessage='Submit report' /></Button>
</div>
</>
);
};
Comment.propTypes = {
comment: PropTypes.string.isRequired,
domain: PropTypes.string,
statusIds: ImmutablePropTypes.list.isRequired,
isRemote: PropTypes.bool,
isSubmitting: PropTypes.bool,
selectedDomains: ImmutablePropTypes.set.isRequired,
onSubmit: PropTypes.func.isRequired,
onChangeComment: PropTypes.func.isRequired,
onToggleDomain: PropTypes.func.isRequired,
};
export default Comment;

View File

@@ -0,0 +1,217 @@
import { useCallback, useEffect, useId, useRef } from 'react';
import { useIntl, defineMessages, FormattedMessage } from 'react-intl';
import type { Map } from 'immutable';
import { OrderedSet } from 'immutable';
import { shallowEqual } from 'react-redux';
import Toggle from 'react-toggle';
import { fetchAccount } from 'mastodon/actions/accounts';
import { Button } from 'mastodon/components/button';
import type { Status } from 'mastodon/models/status';
import type { RootState } from 'mastodon/store';
import {
createAppSelector,
useAppDispatch,
useAppSelector,
} from 'mastodon/store';
const messages = defineMessages({
placeholder: {
id: 'report.placeholder',
defaultMessage: 'Type or paste additional comments',
},
});
const selectRepliedToAccountIds = createAppSelector(
[
(state: RootState) => state.statuses,
(_: unknown, statusIds: string[]) => statusIds,
],
(statusesMap: Map<string, Status>, statusIds: string[]) =>
statusIds.map(
(statusId) =>
statusesMap.getIn([statusId, 'in_reply_to_account_id']) as string,
),
{
memoizeOptions: {
resultEqualityCheck: shallowEqual,
},
},
);
interface Props {
modalTitle?: React.ReactNode;
comment: string;
domain?: string;
statusIds: string[];
isRemote?: boolean;
isSubmitting?: boolean;
selectedDomains: string[];
submitError?: React.ReactNode;
onSubmit: () => void;
onChangeComment: (newComment: string) => void;
onToggleDomain: (toggledDomain: string, checked: boolean) => void;
}
const Comment: React.FC<Props> = ({
modalTitle,
comment,
domain,
statusIds,
isRemote,
isSubmitting,
selectedDomains,
submitError,
onSubmit,
onChangeComment,
onToggleDomain,
}) => {
const intl = useIntl();
const dispatch = useAppDispatch();
const loadedRef = useRef(false);
const handleSubmit = useCallback(() => {
onSubmit();
}, [onSubmit]);
const handleChange = useCallback(
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
onChangeComment(e.target.value);
},
[onChangeComment],
);
const handleToggleDomain = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
onToggleDomain(e.target.value, e.target.checked);
},
[onToggleDomain],
);
const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
handleSubmit();
}
},
[handleSubmit],
);
// Memoize accountIds since we don't want it to trigger `useEffect` on each render
const accountIds = useAppSelector((state) =>
domain ? selectRepliedToAccountIds(state, statusIds) : [],
);
// While we could memoize `availableDomains`, it is pretty inexpensive to recompute
const accountsMap = useAppSelector((state) => state.accounts);
const availableDomains = domain
? OrderedSet([domain]).union(
accountIds
.map(
(accountId) =>
(accountsMap.getIn([accountId, 'acct'], '') as string).split(
'@',
)[1],
)
.filter((domain): domain is string => !!domain),
)
: OrderedSet<string>();
useEffect(() => {
if (loadedRef.current) {
return;
}
loadedRef.current = true;
// First, pre-select known domains
availableDomains.forEach((domain) => {
onToggleDomain(domain, true);
});
// Then, fetch missing replied-to accounts
const unknownAccounts = OrderedSet(
accountIds.filter(
(accountId) => accountId && !accountsMap.has(accountId),
),
);
unknownAccounts.forEach((accountId) => {
dispatch(fetchAccount(accountId));
});
});
const titleId = useId();
return (
<>
<h3 className='report-dialog-modal__title' id={titleId}>
{modalTitle ?? (
<FormattedMessage
id='report.comment.title'
defaultMessage='Is there anything else you think we should know?'
/>
)}
</h3>
<textarea
className='report-dialog-modal__textarea'
placeholder={intl.formatMessage(messages.placeholder)}
aria-labelledby={titleId}
value={comment}
onChange={handleChange}
onKeyDown={handleKeyDown}
disabled={isSubmitting}
/>
{isRemote && (
<>
<p className='report-dialog-modal__lead'>
<FormattedMessage
id='report.forward_hint'
defaultMessage='The account is from another server. Send an anonymized copy of the report there as well?'
/>
</p>
{availableDomains.map((domain) => (
<label
className='report-dialog-modal__toggle'
key={`toggle-${domain}`}
htmlFor={`input-${domain}`}
>
<Toggle
checked={selectedDomains.includes(domain)}
disabled={isSubmitting}
onChange={handleToggleDomain}
value={domain}
id={`input-${domain}`}
/>
<FormattedMessage
id='report.forward'
defaultMessage='Forward to {target}'
values={{ target: domain }}
/>
</label>
))}
</>
)}
{submitError}
<div className='flex-spacer' />
<div className='report-dialog-modal__actions'>
<Button onClick={handleSubmit} disabled={isSubmitting}>
<FormattedMessage id='report.submit' defaultMessage='Submit report' />
</Button>
</div>
</>
);
};
// eslint-disable-next-line import/no-default-export
export default Comment;

View File

@@ -10,6 +10,7 @@ import {
BlockModal, BlockModal,
DomainBlockModal, DomainBlockModal,
ReportModal, ReportModal,
ReportCollectionModal,
EmbedModal, EmbedModal,
ListAdder, ListAdder,
CompareHistoryModal, CompareHistoryModal,
@@ -77,6 +78,7 @@ export const MODAL_COMPONENTS = {
'BLOCK': BlockModal, 'BLOCK': BlockModal,
'DOMAIN_BLOCK': DomainBlockModal, 'DOMAIN_BLOCK': DomainBlockModal,
'REPORT': ReportModal, 'REPORT': ReportModal,
'REPORT_COLLECTION': ReportCollectionModal,
'ACTIONS': () => Promise.resolve({ default: ActionsModal }), 'ACTIONS': () => Promise.resolve({ default: ActionsModal }),
'EMBED': EmbedModal, 'EMBED': EmbedModal,
'FOCAL_POINT': () => Promise.resolve({ default: AltTextModal }), 'FOCAL_POINT': () => Promise.resolve({ default: AltTextModal }),

View File

@@ -0,0 +1,173 @@
import { useCallback, useEffect, useState } from 'react';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import { Callout } from '@/mastodon/components/callout';
import CloseIcon from '@/material-icons/400-24px/close.svg?react';
import { submitReport } from 'mastodon/actions/reports';
import { fetchServer } from 'mastodon/actions/server';
import type { ApiCollectionJSON } from 'mastodon/api_types/collections';
import { Button } from 'mastodon/components/button';
import { IconButton } from 'mastodon/components/icon_button';
import { useAccount } from 'mastodon/hooks/useAccount';
import { useAppDispatch } from 'mastodon/store';
import Comment from '../../report/comment';
const messages = defineMessages({
close: { id: 'lightbox.close', defaultMessage: 'Close' },
});
const CollectionThanks: React.FC<{
onClose: () => void;
}> = ({ onClose }) => {
return (
<>
<h3 className='report-dialog-modal__title'>
<FormattedMessage
id='report.thanks.title_actionable'
defaultMessage="Thanks for reporting, we'll look into this."
/>
</h3>
<div className='flex-spacer' />
<div className='report-dialog-modal__actions'>
<Button onClick={onClose}>
<FormattedMessage id='report.close' defaultMessage='Done' />
</Button>
</div>
</>
);
};
export const ReportCollectionModal: React.FC<{
collection: ApiCollectionJSON;
onClose: () => void;
}> = ({ collection, onClose }) => {
const intl = useIntl();
const dispatch = useAppDispatch();
const { id: collectionId, name, account_id } = collection;
const account = useAccount(account_id);
useEffect(() => {
dispatch(fetchServer());
}, [dispatch]);
const [submitState, setSubmitState] = useState<
'idle' | 'submitting' | 'submitted' | 'error'
>('idle');
const [step, setStep] = useState<'comment' | 'thanks'>('comment');
const [comment, setComment] = useState('');
const [selectedDomains, setSelectedDomains] = useState<string[]>([]);
const handleDomainToggle = useCallback((domain: string, checked: boolean) => {
if (checked) {
setSelectedDomains((domains) => [...domains, domain]);
} else {
setSelectedDomains((domains) => domains.filter((d) => d !== domain));
}
}, []);
const handleSubmit = useCallback(() => {
setSubmitState('submitting');
dispatch(
submitReport(
{
account_id,
status_ids: [],
collection_ids: [collectionId],
forward_to_domains: selectedDomains,
comment,
forward: selectedDomains.length > 0,
category: 'spam',
},
() => {
setSubmitState('submitted');
setStep('thanks');
},
() => {
setSubmitState('error');
},
),
);
}, [account_id, comment, dispatch, collectionId, selectedDomains]);
if (!account) {
return null;
}
const domain = account.get('acct').split('@')[1];
const isRemote = !!domain;
let stepComponent;
switch (step) {
case 'comment':
stepComponent = (
<Comment
modalTitle={
<FormattedMessage
id='report.collection_comment'
defaultMessage='Why do you want to report this collection?'
/>
}
submitError={
submitState === 'error' && (
<Callout
variant='error'
title={
<FormattedMessage
id='report.submission_error'
defaultMessage='Report could not be submitted'
/>
}
>
<FormattedMessage
id='report.submission_error_details'
defaultMessage='Please check your network connection and try again later.'
/>
</Callout>
)
}
onSubmit={handleSubmit}
isSubmitting={submitState === 'submitting'}
isRemote={isRemote}
comment={comment}
domain={domain}
onChangeComment={setComment}
statusIds={[]}
selectedDomains={selectedDomains}
onToggleDomain={handleDomainToggle}
/>
);
break;
case 'thanks':
stepComponent = <CollectionThanks onClose={onClose} />;
}
return (
<div className='modal-root__modal report-dialog-modal'>
<div className='report-modal__target'>
<IconButton
className='report-modal__close'
title={intl.formatMessage(messages.close)}
icon='times'
iconComponent={CloseIcon}
onClick={onClose}
/>
<FormattedMessage
id='report.target'
defaultMessage='Report {target}'
values={{ target: <strong>{name}</strong> }}
/>
</div>
<div className='report-dialog-modal__container'>{stepComponent}</div>
</div>
);
};

View File

@@ -172,6 +172,11 @@ export function ReportModal () {
return import('../components/report_modal'); return import('../components/report_modal');
} }
export function ReportCollectionModal () {
return import('../components/report_collection_modal')
.then((module) => ({ default: module.ReportCollectionModal }));;
}
export function IgnoreNotificationsModal () { export function IgnoreNotificationsModal () {
return import('../components/ignore_notifications_modal'); return import('../components/ignore_notifications_modal');
} }

View File

@@ -306,11 +306,13 @@
"collections.no_collections_yet": "No collections yet.", "collections.no_collections_yet": "No collections yet.",
"collections.old_last_post_note": "Last posted over a week ago", "collections.old_last_post_note": "Last posted over a week ago",
"collections.remove_account": "Remove this account", "collections.remove_account": "Remove this account",
"collections.report_collection": "Report this collection",
"collections.search_accounts_label": "Search for accounts to add…", "collections.search_accounts_label": "Search for accounts to add…",
"collections.search_accounts_max_reached": "You have added the maximum number of accounts", "collections.search_accounts_max_reached": "You have added the maximum number of accounts",
"collections.sensitive": "Sensitive", "collections.sensitive": "Sensitive",
"collections.topic_hint": "Add a hashtag that helps others understand the main topic of this collection.", "collections.topic_hint": "Add a hashtag that helps others understand the main topic of this collection.",
"collections.view_collection": "View collection", "collections.view_collection": "View collection",
"collections.view_other_collections_by_user": "View other collections by this user",
"collections.visibility_public": "Public", "collections.visibility_public": "Public",
"collections.visibility_public_hint": "Discoverable in search results and other areas where recommendations appear.", "collections.visibility_public_hint": "Discoverable in search results and other areas where recommendations appear.",
"collections.visibility_title": "Visibility", "collections.visibility_title": "Visibility",
@@ -976,6 +978,7 @@
"report.category.title_account": "profile", "report.category.title_account": "profile",
"report.category.title_status": "post", "report.category.title_status": "post",
"report.close": "Done", "report.close": "Done",
"report.collection_comment": "Why do you want to report this collection?",
"report.comment.title": "Is there anything else you think we should know?", "report.comment.title": "Is there anything else you think we should know?",
"report.forward": "Forward to {target}", "report.forward": "Forward to {target}",
"report.forward_hint": "The account is from another server. Send an anonymized copy of the report there as well?", "report.forward_hint": "The account is from another server. Send an anonymized copy of the report there as well?",
@@ -997,6 +1000,8 @@
"report.rules.title": "Which rules are being violated?", "report.rules.title": "Which rules are being violated?",
"report.statuses.subtitle": "Select all that apply", "report.statuses.subtitle": "Select all that apply",
"report.statuses.title": "Are there any posts that back up this report?", "report.statuses.title": "Are there any posts that back up this report?",
"report.submission_error": "Report could not be submitted",
"report.submission_error_details": "Please check your network connection and try again later.",
"report.submit": "Submit", "report.submit": "Submit",
"report.target": "Reporting {target}", "report.target": "Reporting {target}",
"report.thanks.take_action": "Here are your options for controlling what you see on Mastodon:", "report.thanks.take_action": "Here are your options for controlling what you see on Mastodon:",