import clsx from 'clsx'; import React, { useRef } from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import { cancelReplyCompose, setGroupTimelineVisible, uploadCompose } from 'soapbox/actions/compose'; import { openModal, closeModal } from 'soapbox/actions/modals'; import { checkComposeContent } from 'soapbox/components/modal-root'; import { HStack, Modal, Text, Toggle } from 'soapbox/components/ui'; import { useAppDispatch, useAppSelector, useCompose, useDraggedFiles } from 'soapbox/hooks'; import { useGroup } from 'soapbox/hooks/api'; import ComposeForm from '../../../compose/components/compose-form'; const messages = defineMessages({ close: { id: 'lightbox.close', defaultMessage: 'Close' }, confirm: { id: 'confirmations.cancel.confirm', defaultMessage: 'Discard' }, cancelEditing: { id: 'confirmations.cancel_editing.confirm', defaultMessage: 'Cancel editing' }, }); interface IComposeModal { onClose: (type?: string) => void composeId?: string } const ComposeModal: React.FC = ({ onClose, composeId = 'compose-modal' }) => { const intl = useIntl(); const dispatch = useAppDispatch(); const node = useRef(null); const compose = useCompose(composeId); const { id: statusId, privacy, in_reply_to: inReplyTo, quote, group_id: groupId } = compose!; const { isDragging, isDraggedOver } = useDraggedFiles(node, (files) => { dispatch(uploadCompose(composeId, files, intl)); }); const onClickClose = () => { if (checkComposeContent(compose)) { dispatch(openModal('CONFIRM', { icon: require('@tabler/icons/trash.svg'), heading: statusId ? : , message: statusId ? : , confirm: intl.formatMessage(statusId ? messages.cancelEditing : messages.confirm), onConfirm: () => { dispatch(closeModal('COMPOSE')); dispatch(cancelReplyCompose()); }, })); } else { onClose('COMPOSE'); } }; const renderTitle = () => { if (statusId) { return ; } else if (privacy === 'direct') { return ; } else if (inReplyTo && groupId) { return ; } else if (groupId) { return ; } else if (inReplyTo) { return ; } else if (quote) { return ; } else { return ; } }; return ( } /> ); }; interface IComposeFormGroupToggle { composeId: string groupId: string | null } const ComposeFormGroupToggle: React.FC = ({ composeId, groupId }) => { const dispatch = useAppDispatch(); const { group } = useGroup(groupId || '', false); const groupTimelineVisible = useAppSelector((state) => !!state.compose.get(composeId)?.group_timeline_visible); const handleToggleChange = () => { dispatch(setGroupTimelineVisible(composeId, !groupTimelineVisible)); }; const labelId = `group-timeline-visible+${composeId}`; if (!group) return null; if (group.locked) return null; return ( ); }; export default ComposeModal;