import { List as ImmutableList } from 'immutable'; import React from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import { joinGroup, leaveGroup } from 'soapbox/actions/groups'; import { openModal } from 'soapbox/actions/modals'; import StillImage from 'soapbox/components/still-image'; import { Avatar, Button, HStack, Icon, Stack, Text } from 'soapbox/components/ui'; import { useAppDispatch } from 'soapbox/hooks'; import { normalizeAttachment } from 'soapbox/normalizers'; import { isDefaultHeader } from 'soapbox/utils/accounts'; import type { Group } from 'soapbox/types/entities'; const messages = defineMessages({ header: { id: 'group.header.alt', defaultMessage: 'Group header' }, 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?' }, confirmationConfirm: { id: 'confirmations.leave_group.confirm', defaultMessage: 'Leave' }, }); interface IGroupHeader { group?: Group | false | null, } const GroupHeader: React.FC = ({ group }) => { const intl = useIntl(); const dispatch = useAppDispatch(); if (!group) { return (
); } const onJoinGroup = () => dispatch(joinGroup(group.id)); const onLeaveGroup = () => dispatch(openModal('CONFIRM', { heading: intl.formatMessage(messages.confirmationHeading), message: intl.formatMessage(messages.confirmationMessage), confirm: intl.formatMessage(messages.confirmationConfirm), onConfirm: () => dispatch(leaveGroup(group.id)), })); const onAvatarClick = () => { const avatar = normalizeAttachment({ type: 'image', url: group.avatar, }); dispatch(openModal('MEDIA', { media: ImmutableList.of(avatar), index: 0 })); }; const handleAvatarClick: React.MouseEventHandler = (e) => { if (e.button === 0 && !(e.ctrlKey || e.metaKey)) { e.preventDefault(); onAvatarClick(); } }; const onHeaderClick = () => { const header = normalizeAttachment({ type: 'image', url: group.header, }); dispatch(openModal('MEDIA', { media: ImmutableList.of(header), index: 0 })); }; const handleHeaderClick: React.MouseEventHandler = (e) => { if (e.button === 0 && !(e.ctrlKey || e.metaKey)) { e.preventDefault(); onHeaderClick(); } }; const renderHeader = () => { let header: React.ReactNode; if (group.header) { header = ( ); if (!isDefaultHeader(group.header)) { header = ( {header} ); } } return header; }; const makeActionButton = () => { if (!group.relationship || !group.relationship.member) { return ( ); } if (group.relationship.requested) { return ( ); } if (group.relationship?.role === 'admin') { return ( ); } return ( ); }; const actionButton = makeActionButton(); return (
{renderHeader()}
{group.relationship?.role === 'admin' ? ( ) : group.relationship?.role === 'moderator' && ( )} {group.locked ? ( ) : ( )} {actionButton}
); }; export default GroupHeader;