2023-03-22 10:56:32 -07:00
|
|
|
import React, { useMemo } from 'react';
|
2023-03-30 09:57:42 -07:00
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
2023-03-22 10:56:32 -07:00
|
|
|
|
2023-04-04 03:29:09 -07:00
|
|
|
import { openModal } from 'soapbox/actions/modals';
|
2023-03-22 10:56:32 -07:00
|
|
|
import { initReport, ReportableEntities } from 'soapbox/actions/reports';
|
|
|
|
import DropdownMenu, { Menu } from 'soapbox/components/dropdown-menu';
|
|
|
|
import { IconButton } from 'soapbox/components/ui';
|
|
|
|
import { useAppDispatch, useOwnAccount } from 'soapbox/hooks';
|
2023-04-04 03:29:09 -07:00
|
|
|
import { useLeaveGroup } from 'soapbox/hooks/api';
|
2023-03-22 10:56:32 -07:00
|
|
|
import { GroupRoles } from 'soapbox/schemas/group-member';
|
2023-04-04 03:29:09 -07:00
|
|
|
import toast from 'soapbox/toast';
|
2023-03-22 10:56:32 -07:00
|
|
|
|
|
|
|
import type { Account, Group } from 'soapbox/types/entities';
|
|
|
|
|
2023-03-30 09:57:42 -07:00
|
|
|
const messages = defineMessages({
|
2023-04-04 03:29:09 -07:00
|
|
|
confirmationConfirm: { id: 'confirmations.leave_group.confirm', defaultMessage: 'Leave' },
|
|
|
|
confirmationHeading: { id: 'confirmations.leave_group.heading', defaultMessage: 'Leave group' },
|
|
|
|
confirmationMessage: { id: 'confirmations.leave_group.message', defaultMessage: 'You are about to leave the group. Do you want to continue?' },
|
|
|
|
leave: { id: 'group.leave.label', defaultMessage: 'Leave' },
|
|
|
|
leaveSuccess: { id: 'group.leave.success', defaultMessage: 'Left the group' },
|
2023-03-30 09:57:42 -07:00
|
|
|
report: { id: 'group.report.label', defaultMessage: 'Report' },
|
|
|
|
});
|
|
|
|
|
2023-03-22 10:56:32 -07:00
|
|
|
interface IGroupActionButton {
|
|
|
|
group: Group
|
|
|
|
}
|
|
|
|
|
|
|
|
const GroupOptionsButton = ({ group }: IGroupActionButton) => {
|
|
|
|
const account = useOwnAccount();
|
2023-03-30 09:57:42 -07:00
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
const intl = useIntl();
|
2023-04-04 03:29:09 -07:00
|
|
|
const leaveGroup = useLeaveGroup(group);
|
2023-03-22 10:56:32 -07:00
|
|
|
|
|
|
|
const isMember = group.relationship?.role === GroupRoles.USER;
|
2023-04-04 03:29:09 -07:00
|
|
|
const isAdmin = group.relationship?.role === GroupRoles.ADMIN;
|
2023-03-22 10:56:32 -07:00
|
|
|
const isBlocked = group.relationship?.blocked_by;
|
|
|
|
|
2023-04-04 03:29:09 -07:00
|
|
|
const onLeaveGroup = () =>
|
|
|
|
dispatch(openModal('CONFIRM', {
|
|
|
|
heading: intl.formatMessage(messages.confirmationHeading),
|
|
|
|
message: intl.formatMessage(messages.confirmationMessage),
|
|
|
|
confirm: intl.formatMessage(messages.confirmationConfirm),
|
|
|
|
onConfirm: () => leaveGroup.mutate(group.relationship?.id as string, {
|
|
|
|
onSuccess() {
|
|
|
|
leaveGroup.invalidate();
|
|
|
|
toast.success(intl.formatMessage(messages.leaveSuccess));
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}));
|
2023-03-22 10:56:32 -07:00
|
|
|
|
2023-04-04 03:29:09 -07:00
|
|
|
const menu: Menu = useMemo(() => {
|
|
|
|
const items = [];
|
|
|
|
|
|
|
|
if (isMember) {
|
|
|
|
items.push({
|
|
|
|
text: intl.formatMessage(messages.report),
|
|
|
|
icon: require('@tabler/icons/flag.svg'),
|
|
|
|
action: () => dispatch(initReport(ReportableEntities.GROUP, account as Account, { group })),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isAdmin) {
|
|
|
|
items.push({
|
|
|
|
text: intl.formatMessage(messages.leave),
|
|
|
|
icon: require('@tabler/icons/logout.svg'),
|
|
|
|
action: onLeaveGroup,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}, [isMember, isAdmin]);
|
|
|
|
|
|
|
|
if (isBlocked || menu.length === 0) {
|
2023-03-22 10:56:32 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DropdownMenu items={menu} placement='bottom'>
|
|
|
|
<IconButton
|
|
|
|
src={require('@tabler/icons/dots.svg')}
|
|
|
|
theme='secondary'
|
|
|
|
iconClassName='h-5 w-5'
|
|
|
|
className='self-stretch px-2.5'
|
2023-03-30 09:57:42 -07:00
|
|
|
data-testid='dropdown-menu-button'
|
2023-03-22 10:56:32 -07:00
|
|
|
/>
|
|
|
|
</DropdownMenu>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default GroupOptionsButton;
|