bigbuffet-rw/app/soapbox/features/admin/components/report.tsx

141 lines
5.1 KiB
TypeScript
Raw Normal View History

import React, { useCallback, useState } from 'react';
2022-04-28 11:35:52 -07:00
import { useIntl, FormattedMessage, defineMessages } from 'react-intl';
import { Link } from 'react-router-dom';
import { closeReports } from 'soapbox/actions/admin';
import { deactivateUserModal, deleteUserModal } from 'soapbox/actions/moderation';
import snackbar from 'soapbox/actions/snackbar';
import Avatar from 'soapbox/components/avatar';
2022-04-28 19:55:02 -07:00
import HoverRefWrapper from 'soapbox/components/hover_ref_wrapper';
2022-04-28 19:36:28 -07:00
import { Button, HStack } from 'soapbox/components/ui';
2022-11-15 08:13:54 -08:00
import DropdownMenu from 'soapbox/containers/dropdown-menu-container';
2022-04-28 11:35:52 -07:00
import Accordion from 'soapbox/features/ui/components/accordion';
import { useAppDispatch, useAppSelector } from 'soapbox/hooks';
import { makeGetReport } from 'soapbox/selectors';
2022-04-28 11:35:52 -07:00
import ReportStatus from './report_status';
import type { List as ImmutableList } from 'immutable';
import type { Account, AdminReport, Status } from 'soapbox/types/entities';
2022-04-28 11:35:52 -07:00
const messages = defineMessages({
reportClosed: { id: 'admin.reports.report_closed_message', defaultMessage: 'Report on @{name} was closed' },
deactivateUser: { id: 'admin.users.actions.deactivate_user', defaultMessage: 'Deactivate @{name}' },
deleteUser: { id: 'admin.users.actions.delete_user', defaultMessage: 'Delete @{name}' },
});
interface IReport {
id: string;
2022-04-28 11:35:52 -07:00
}
const Report: React.FC<IReport> = ({ id }) => {
2022-04-28 11:35:52 -07:00
const intl = useIntl();
const dispatch = useAppDispatch();
const getReport = useCallback(makeGetReport(), []);
const report = useAppSelector((state) => getReport(state, id) as AdminReport | undefined);
2022-04-28 11:35:52 -07:00
const [accordionExpanded, setAccordionExpanded] = useState(false);
if (!report) return null;
const account = report.account as Account;
const targetAccount = report.target_account as Account;
2022-04-28 11:35:52 -07:00
const makeMenu = () => {
return [{
text: intl.formatMessage(messages.deactivateUser, { name: targetAccount.username as string }),
2022-04-28 11:35:52 -07:00
action: handleDeactivateUser,
icon: require('@tabler/icons/user-off.svg'),
2022-04-28 11:35:52 -07:00
}, {
text: intl.formatMessage(messages.deleteUser, { name: targetAccount.username as string }),
2022-04-28 11:35:52 -07:00
action: handleDeleteUser,
icon: require('@tabler/icons/user-minus.svg'),
2022-04-28 11:35:52 -07:00
}];
};
const handleCloseReport = () => {
dispatch(closeReports([report.id])).then(() => {
const message = intl.formatMessage(messages.reportClosed, { name: targetAccount.username as string });
2022-04-28 11:35:52 -07:00
dispatch(snackbar.success(message));
}).catch(() => {});
};
const handleDeactivateUser = () => {
const accountId = targetAccount.id;
2022-04-28 11:35:52 -07:00
dispatch(deactivateUserModal(intl, accountId, () => handleCloseReport()));
};
const handleDeleteUser = () => {
const accountId = targetAccount.id as string;
2022-04-28 11:35:52 -07:00
dispatch(deleteUserModal(intl, accountId, () => handleCloseReport()));
};
const handleAccordionToggle = (setting: boolean) => {
setAccordionExpanded(setting);
};
const menu = makeMenu();
const statuses = report.statuses as ImmutableList<Status>;
2022-04-28 11:35:52 -07:00
const statusCount = statuses.count();
const acct = targetAccount.acct as string;
const reporterAcct = account.acct as string;
2022-04-28 11:35:52 -07:00
return (
<div className='admin-report' key={report.id}>
2022-04-28 11:35:52 -07:00
<div className='admin-report__avatar'>
<HoverRefWrapper accountId={targetAccount.id as string} inline>
2022-04-28 19:55:02 -07:00
<Link to={`/@${acct}`} title={acct}>
<Avatar account={targetAccount} size={32} />
2022-04-28 19:55:02 -07:00
</Link>
</HoverRefWrapper>
2022-04-28 11:35:52 -07:00
</div>
<div className='admin-report__content'>
<h4 className='admin-report__title'>
<FormattedMessage
id='admin.reports.report_title'
defaultMessage='Report on {acct}'
2022-04-28 19:55:02 -07:00
values={{ acct: (
<HoverRefWrapper accountId={account.id as string} inline>
2022-04-28 19:55:02 -07:00
<Link to={`/@${acct}`} title={acct}>@{acct}</Link>
</HoverRefWrapper>
) }}
2022-04-28 11:35:52 -07:00
/>
</h4>
<div className='admin-report__statuses'>
{statusCount > 0 && (
<Accordion
headline={`Reported posts (${statusCount})`}
expanded={accordionExpanded}
onToggle={handleAccordionToggle}
>
{statuses.map(status => <ReportStatus report={report} status={status} key={status.id} />)}
</Accordion>
)}
</div>
<div className='admin-report__quote'>
{(report.comment || '').length > 0 && (
<blockquote className='md' dangerouslySetInnerHTML={{ __html: report.comment }} />
2022-04-28 19:36:28 -07:00
)}
2022-04-28 19:55:02 -07:00
<span className='byline'>
&mdash;
<HoverRefWrapper accountId={account.id as string} inline>
2022-04-28 19:55:02 -07:00
<Link to={`/@${reporterAcct}`} title={reporterAcct}>@{reporterAcct}</Link>
</HoverRefWrapper>
</span>
2022-04-28 11:35:52 -07:00
</div>
</div>
2022-04-28 19:36:28 -07:00
<HStack space={2} alignItems='start'>
2022-04-28 11:35:52 -07:00
<Button onClick={handleCloseReport}>
<FormattedMessage id='admin.reports.actions.close' defaultMessage='Close' />
</Button>
<DropdownMenu items={menu} src={require('@tabler/icons/dots-vertical.svg')} />
2022-04-28 19:36:28 -07:00
</HStack>
2022-04-28 11:35:52 -07:00
</div>
);
};
export default Report;