// Copyright 2019-2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { MutableRefObject, useCallback, useEffect, useRef, useState, } from 'react'; import { get } from 'lodash'; import classNames from 'classnames'; import type { BodyRangeType, BodyRangesType } from '../types/Util'; import type { ErrorDialogAudioRecorderType } from '../state/ducks/audioRecorder'; import type { HandleAttachmentsProcessingArgsType } from '../util/handleAttachmentsProcessing'; import { Spinner } from './Spinner'; import { EmojiButton, Props as EmojiButtonProps } from './emoji/EmojiButton'; import { Props as StickerButtonProps, StickerButton, } from './stickers/StickerButton'; import { CompositionInput, InputApi, Props as CompositionInputProps, } from './CompositionInput'; import { MessageRequestActions, Props as MessageRequestActionsProps, } from './conversation/MessageRequestActions'; import { GroupV1DisabledActions, PropsType as GroupV1DisabledActionsPropsType, } from './conversation/GroupV1DisabledActions'; import { GroupV2PendingApprovalActions, PropsType as GroupV2PendingApprovalActionsPropsType, } from './conversation/GroupV2PendingApprovalActions'; import { AnnouncementsOnlyGroupBanner } from './AnnouncementsOnlyGroupBanner'; import { AttachmentList } from './conversation/AttachmentList'; import { AttachmentType, isImageAttachment } from '../types/Attachment'; import { AudioCapture } from './conversation/AudioCapture'; import { CompositionUpload } from './CompositionUpload'; import { ConversationType } from '../state/ducks/conversations'; import { EmojiPickDataType } from './emoji/EmojiPicker'; import { LinkPreviewWithDomain } from '../types/LinkPreview'; import { LocalizerType } from '../types/Util'; import { MandatoryProfileSharingActions } from './conversation/MandatoryProfileSharingActions'; import { MediaQualitySelector } from './MediaQualitySelector'; import { Quote, Props as QuoteProps } from './conversation/Quote'; import { StagedLinkPreview } from './conversation/StagedLinkPreview'; import { countStickers } from './stickers/lib'; export type CompositionAPIType = | { focusInput: () => void; isDirty: () => boolean; setDisabled: (disabled: boolean) => void; reset: InputApi['reset']; resetEmojiResults: InputApi['resetEmojiResults']; } | undefined; export type OwnProps = Readonly<{ acceptedMessageRequest?: boolean; addAttachment: ( conversationId: string, attachment: AttachmentType ) => unknown; addPendingAttachment: ( conversationId: string, pendingAttachment: AttachmentType ) => unknown; announcementsOnly?: boolean; areWeAdmin?: boolean; areWePending?: boolean; areWePendingApproval?: boolean; cancelRecording: () => unknown; completeRecording: ( conversationId: string, onSendAudioRecording?: (rec: AttachmentType) => unknown ) => unknown; compositionApi?: MutableRefObject; conversationId: string; draftAttachments: ReadonlyArray; errorDialogAudioRecorderType?: ErrorDialogAudioRecorderType; errorRecording: (e: ErrorDialogAudioRecorderType) => unknown; groupAdmins: Array; groupVersion?: 1 | 2; i18n: LocalizerType; isFetchingUUID?: boolean; isGroupV1AndDisabled?: boolean; isMissingMandatoryProfileSharing?: boolean; isRecording: boolean; isSMSOnly?: boolean; left?: boolean; linkPreviewLoading: boolean; linkPreviewResult?: LinkPreviewWithDomain; messageRequestsEnabled?: boolean; onClearAttachments(): unknown; onClickAttachment(att: AttachmentType): unknown; onClickQuotedMessage(): unknown; onCloseLinkPreview(): unknown; processAttachments: (options: HandleAttachmentsProcessingArgsType) => unknown; onSelectMediaQuality(isHQ: boolean): unknown; onSendMessage(options: { draftAttachments?: ReadonlyArray; mentions?: BodyRangesType; message?: string; timestamp?: number; voiceNoteAttachment?: AttachmentType; }): unknown; openConversation(conversationId: string): unknown; quotedMessageProps?: Omit< QuoteProps, 'i18n' | 'onClick' | 'onClose' | 'withContentAbove' >; removeAttachment: (conversationId: string, filePath: string) => unknown; setQuotedMessage(message: undefined): unknown; shouldSendHighQualityAttachments: boolean; startRecording: () => unknown; }>; export type Props = Pick< CompositionInputProps, | 'sortedGroupMembers' | 'onEditorStateChange' | 'onTextTooLong' | 'draftText' | 'draftBodyRanges' | 'clearQuotedMessage' | 'getQuotedMessage' > & Pick< EmojiButtonProps, 'onPickEmoji' | 'onSetSkinTone' | 'recentEmojis' | 'skinTone' > & Pick< StickerButtonProps, | 'knownPacks' | 'receivedPacks' | 'installedPack' | 'installedPacks' | 'blessedPacks' | 'recentStickers' | 'clearInstalledStickerPack' | 'onClickAddPack' | 'onPickSticker' | 'clearShowIntroduction' | 'showPickerHint' | 'clearShowPickerHint' > & MessageRequestActionsProps & Pick & Pick & OwnProps; export const CompositionArea = ({ // Base props addAttachment, addPendingAttachment, conversationId, i18n, onSendMessage, processAttachments, removeAttachment, // AttachmentList draftAttachments, onClearAttachments, onClickAttachment, // AudioCapture cancelRecording, completeRecording, errorDialogAudioRecorderType, errorRecording, isRecording, startRecording, // StagedLinkPreview linkPreviewLoading, linkPreviewResult, onCloseLinkPreview, // Quote quotedMessageProps, onClickQuotedMessage, setQuotedMessage, // MediaQualitySelector onSelectMediaQuality, shouldSendHighQualityAttachments, // CompositionInput compositionApi, onEditorStateChange, onTextTooLong, draftText, draftBodyRanges, clearQuotedMessage, getQuotedMessage, sortedGroupMembers, // EmojiButton onPickEmoji, onSetSkinTone, recentEmojis, skinTone, // StickerButton knownPacks, receivedPacks, installedPack, installedPacks, blessedPacks, recentStickers, clearInstalledStickerPack, onClickAddPack, onPickSticker, clearShowIntroduction, showPickerHint, clearShowPickerHint, // Message Requests acceptedMessageRequest, areWePending, areWePendingApproval, conversationType, groupVersion, isBlocked, isMissingMandatoryProfileSharing, left, messageRequestsEnabled, onAccept, onBlock, onBlockAndReportSpam, onDelete, onUnblock, title, // GroupV1 Disabled Actions isGroupV1AndDisabled, onStartGroupMigration, // GroupV2 announcementsOnly, areWeAdmin, groupAdmins, onCancelJoinRequest, openConversation, // SMS-only contacts isSMSOnly, isFetchingUUID, }: Props): JSX.Element => { const [disabled, setDisabled] = useState(false); const [dirty, setDirty] = useState(false); const [large, setLarge] = useState(false); const inputApiRef = useRef(); const fileInputRef = useRef(null); const handleForceSend = useCallback(() => { setLarge(false); if (inputApiRef.current) { inputApiRef.current.submit(); } }, [inputApiRef, setLarge]); const handleSubmit = useCallback( (message: string, mentions: Array, timestamp: number) => { setLarge(false); onSendMessage({ draftAttachments, mentions, message, timestamp, }); }, [draftAttachments, onSendMessage, setLarge] ); const launchAttachmentPicker = () => { const fileInput = fileInputRef.current; if (fileInput) { // Setting the value to empty so that onChange always fires in case // you add multiple photos. fileInput.value = ''; fileInput.click(); } }; const focusInput = useCallback(() => { if (inputApiRef.current) { inputApiRef.current.focus(); } }, [inputApiRef]); const withStickers = countStickers({ knownPacks, blessedPacks, installedPacks, receivedPacks, }) > 0; if (compositionApi) { // Using a React.MutableRefObject, so we need to reassign this prop. // eslint-disable-next-line no-param-reassign compositionApi.current = { isDirty: () => dirty, focusInput, setDisabled, reset: () => { if (inputApiRef.current) { inputApiRef.current.reset(); } }, resetEmojiResults: () => { if (inputApiRef.current) { inputApiRef.current.resetEmojiResults(); } }, }; } const insertEmoji = useCallback( (e: EmojiPickDataType) => { if (inputApiRef.current) { inputApiRef.current.insertEmoji(e); onPickEmoji(e); } }, [inputApiRef, onPickEmoji] ); const handleToggleLarge = useCallback(() => { setLarge(l => !l); }, [setLarge]); const shouldShowMicrophone = !large && !draftAttachments.length && !draftText; const showMediaQualitySelector = draftAttachments.some(isImageAttachment); const leftHandSideButtonsFragment = ( <>
{showMediaQualitySelector ? (
) : null} ); const micButtonFragment = shouldShowMicrophone ? ( { onSendMessage({ voiceNoteAttachment }); }} startRecording={startRecording} /> ) : null; const attButton = (
); const sendButtonFragment = (
); const stickerButtonPlacement = large ? 'top-start' : 'top-end'; const stickerButtonFragment = withStickers ? (
) : null; // Listen for cmd/ctrl-shift-x to toggle large composition mode useEffect(() => { const handler = (e: KeyboardEvent) => { const { key, shiftKey, ctrlKey, metaKey } = e; // When using the ctrl key, `key` is `'X'`. When using the cmd key, `key` is `'x'` const xKey = key === 'x' || key === 'X'; const commandKey = get(window, 'platform') === 'darwin' && metaKey; const controlKey = get(window, 'platform') !== 'darwin' && ctrlKey; const commandOrCtrl = commandKey || controlKey; // cmd/ctrl-shift-x if (xKey && shiftKey && commandOrCtrl) { e.preventDefault(); setLarge(x => !x); } }; document.addEventListener('keydown', handler); return () => { document.removeEventListener('keydown', handler); }; }, [setLarge]); if ( isBlocked || areWePending || (messageRequestsEnabled && !acceptedMessageRequest) ) { return ( ); } if (conversationType === 'direct' && isSMSOnly) { return (
{isFetchingUUID ? ( ) : ( <>

{i18n('CompositionArea--sms-only__title')}

{i18n('CompositionArea--sms-only__body')}

)}
); } // If no message request, but we haven't shared profile yet, we show profile-sharing UI if ( !left && (conversationType === 'direct' || (conversationType === 'group' && groupVersion === 1)) && isMissingMandatoryProfileSharing ) { return ( ); } // If this is a V1 group, now disabled entirely, we show UI to help them upgrade if (!left && isGroupV1AndDisabled) { return ( ); } if (areWePendingApproval) { return ( ); } if (announcementsOnly && !areWeAdmin) { return ( ); } return (
{quotedMessageProps && (
{ // This one is for redux... setQuotedMessage(undefined); // and this is for conversation_view. clearQuotedMessage(); }} />
)} {linkPreviewLoading && (
)} {draftAttachments.length ? (
{ if (attachment.path) { removeAttachment(conversationId, attachment.path); } }} />
) : null}
{!large ? leftHandSideButtonsFragment : null}
{!large ? ( <> {stickerButtonFragment} {!dirty ? micButtonFragment : null} {attButton} ) : null}
{large ? (
{leftHandSideButtonsFragment} {stickerButtonFragment} {attButton} {!dirty ? micButtonFragment : null} {dirty || !shouldShowMicrophone ? sendButtonFragment : null}
) : null}
); };