// Copyright 2022 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { useCallback, useLayoutEffect, useMemo, useRef, useState, } from 'react'; import classNames from 'classnames'; import { noop } from 'lodash'; import type { DraftBodyRanges } from '../types/BodyRange'; import type { LocalizerType } from '../types/Util'; import type { ConversationType } from '../state/ducks/conversations'; import type { EmojiPickDataType } from './emoji/EmojiPicker'; import type { InputApi } from './CompositionInput'; import type { PreferredBadgeSelectorType } from '../state/selectors/badges'; import type { RenderEmojiPickerProps } from './conversation/ReactionPicker'; import type { ReplyType, StorySendStateType } from '../types/Stories'; import { StoryViewTargetType } from '../types/Stories'; import { Avatar, AvatarSize } from './Avatar'; import { CompositionInput } from './CompositionInput'; import { ContactName } from './conversation/ContactName'; import { EmojiButton } from './emoji/EmojiButton'; import { Emojify } from './conversation/Emojify'; import { Message, TextDirection } from './conversation/Message'; import { MessageTimestamp } from './conversation/MessageTimestamp'; import { Modal } from './Modal'; import { ReactionPicker } from './conversation/ReactionPicker'; import { Tabs } from './Tabs'; import { Theme } from '../util/theme'; import { ThemeType } from '../types/Util'; import { WidthBreakpoint } from './_util'; import { getAvatarColor } from '../types/Colors'; import { shouldNeverBeCalled } from '../util/shouldNeverBeCalled'; import { ContextMenu } from './ContextMenu'; import { ConfirmationDialog } from './ConfirmationDialog'; // Menu is disabled so these actions are inaccessible. We also don't support // link previews, tap to view messages, attachments, or gifts. Just regular // text messages and reactions. const MESSAGE_DEFAULT_PROPS = { canDeleteForEveryone: false, checkForAccount: shouldNeverBeCalled, clearTargetedMessage: shouldNeverBeCalled, containerWidthBreakpoint: WidthBreakpoint.Medium, doubleCheckMissingQuoteReference: shouldNeverBeCalled, isBlocked: false, isMessageRequestAccepted: true, isSelected: false, isSelectMode: false, onToggleSelect: shouldNeverBeCalled, onReplyToMessage: shouldNeverBeCalled, kickOffAttachmentDownload: shouldNeverBeCalled, markAttachmentAsCorrupted: shouldNeverBeCalled, messageExpanded: shouldNeverBeCalled, openGiftBadge: shouldNeverBeCalled, openLink: shouldNeverBeCalled, previews: [], retryMessageSend: shouldNeverBeCalled, pushPanelForConversation: shouldNeverBeCalled, renderAudioAttachment: () =>
, saveAttachment: shouldNeverBeCalled, scrollToQuotedMessage: shouldNeverBeCalled, showConversation: noop, showExpiredIncomingTapToViewToast: shouldNeverBeCalled, showExpiredOutgoingTapToViewToast: shouldNeverBeCalled, showLightbox: shouldNeverBeCalled, showLightboxForViewOnceMedia: shouldNeverBeCalled, startConversation: shouldNeverBeCalled, theme: ThemeType.dark, viewStory: shouldNeverBeCalled, }; export enum StoryViewsNRepliesTab { Replies = 'Replies', Views = 'Views', } export type PropsType = { authorTitle: string; canReply: boolean; deleteGroupStoryReply: (id: string) => void; deleteGroupStoryReplyForEveryone: (id: string) => void; getPreferredBadge: PreferredBadgeSelectorType; group: Pick | undefined; hasViewReceiptSetting: boolean; hasViewsCapability: boolean; i18n: LocalizerType; platform: string; isFormattingEnabled: boolean; isFormattingSpoilersEnabled: boolean; isInternalUser?: boolean; onChangeViewTarget: (target: StoryViewTargetType) => unknown; onClose: () => unknown; onReact: (emoji: string) => unknown; onReply: ( message: string, bodyRanges: DraftBodyRanges, timestamp: number ) => unknown; onSetSkinTone: (tone: number) => unknown; onTextTooLong: () => unknown; onUseEmoji: (_: EmojiPickDataType) => unknown; preferredReactionEmoji: ReadonlyArray; recentEmojis?: ReadonlyArray; renderEmojiPicker: (props: RenderEmojiPickerProps) => JSX.Element; replies: ReadonlyArray; showContactModal: (contactId: string, conversationId?: string) => void; skinTone?: number; sortedGroupMembers?: ReadonlyArray; views: ReadonlyArray; viewTarget: StoryViewTargetType; }; export function StoryViewsNRepliesModal({ authorTitle, canReply, deleteGroupStoryReply, deleteGroupStoryReplyForEveryone, getPreferredBadge, group, hasViewReceiptSetting, hasViewsCapability, i18n, platform, isFormattingEnabled, isFormattingSpoilersEnabled, isInternalUser, onChangeViewTarget, onClose, onReact, onReply, onSetSkinTone, onTextTooLong, onUseEmoji, preferredReactionEmoji, recentEmojis, renderEmojiPicker, replies, showContactModal, skinTone, sortedGroupMembers, viewTarget, views, }: PropsType): JSX.Element | null { const [deleteReplyId, setDeleteReplyId] = useState( undefined ); const [deleteForEveryoneReplyId, setDeleteForEveryoneReplyId] = useState< string | undefined >(undefined); // These states aren't in redux; they are meant to last only as long as this dialog. const [revealedSpoilersById, setRevealedSpoilersById] = useState< Record >({}); const [displayLimitById, setDisplayLimitById] = useState< Record >({}); const containerElementRef = useRef(null); const inputApiRef = useRef(); const shouldScrollToBottomRef = useRef(true); const bottomRef = useRef(null); const [messageBodyText, setMessageBodyText] = useState(''); const currentTab = useMemo(() => { return viewTarget === StoryViewTargetType.Replies ? StoryViewsNRepliesTab.Replies : StoryViewsNRepliesTab.Views; }, [viewTarget]); const onTabChange = (tab: string) => { onChangeViewTarget( tab === StoryViewsNRepliesTab.Replies ? StoryViewTargetType.Replies : StoryViewTargetType.Views ); }; const focusComposer = useCallback(() => { if (inputApiRef.current) { inputApiRef.current.focus(); } }, [inputApiRef]); const insertEmoji = useCallback( (e: EmojiPickDataType) => { if (inputApiRef.current) { inputApiRef.current.insertEmoji(e); onUseEmoji(e); } }, [inputApiRef, onUseEmoji] ); let composerElement: JSX.Element | undefined; useLayoutEffect(() => { if ( currentTab === StoryViewsNRepliesTab.Replies && replies.length && shouldScrollToBottomRef.current ) { bottomRef.current?.scrollIntoView({ behavior: 'smooth' }); shouldScrollToBottomRef.current = false; } }, [currentTab, replies.length]); if (group && group.left) { composerElement = (
{i18n('icu:StoryViewsNRepliesModal__not-a-member')}
); } else if (canReply) { composerElement = ( <> { if (!group) { onClose(); } onReact(emoji); }} onSetSkinTone={onSetSkinTone} preferredReactionEmoji={preferredReactionEmoji} renderEmojiPicker={renderEmojiPicker} />
{ setMessageBodyText(messageText); }} onPickEmoji={onUseEmoji} onSubmit={(...args) => { inputApiRef.current?.reset(); shouldScrollToBottomRef.current = true; onReply(...args); }} onTextTooLong={onTextTooLong} placeholder={ group ? i18n('icu:StoryViewer__reply-group') : i18n('icu:StoryViewer__reply-placeholder', { firstName: authorTitle, }) } sendCounter={0} sortedGroupMembers={sortedGroupMembers} theme={ThemeType.dark} >
); } let repliesElement: JSX.Element | undefined; function shouldCollapse(reply: ReplyType, otherReply?: ReplyType) { // deleted reactions get rendered the same as deleted replies return ( reply.conversationId === otherReply?.conversationId && (!otherReply?.reactionEmoji || Boolean(otherReply.deletedForEveryone)) ); } if (replies.length) { repliesElement = (
{replies.map((reply, index) => { return ( setDeleteReplyId(reply.id)} deleteGroupStoryReplyForEveryone={() => setDeleteForEveryoneReplyId(reply.id) } displayLimit={displayLimitById[reply.id]} getPreferredBadge={getPreferredBadge} i18n={i18n} platform={platform} id={reply.id} isInternalUser={isInternalUser} isSpoilerExpanded={revealedSpoilersById[reply.id] || false} messageExpanded={(messageId, displayLimit) => { const update = { ...displayLimitById, [messageId]: displayLimit, }; setDisplayLimitById(update); }} reply={reply} shouldCollapseAbove={shouldCollapse(reply, replies[index - 1])} shouldCollapseBelow={shouldCollapse(reply, replies[index + 1])} showContactModal={showContactModal} showSpoiler={messageId => { const update = { ...revealedSpoilersById, [messageId]: true, }; setRevealedSpoilersById(update); }} /> ); })}
); } else if (group) { repliesElement = (
{i18n('icu:StoryViewsNRepliesModal__no-replies')}
); } let viewsElement: JSX.Element | undefined; if (hasViewsCapability && !hasViewReceiptSetting) { viewsElement = (
{i18n('icu:StoryViewsNRepliesModal__read-receipts-off')}
); } else if (views.length) { viewsElement = (
{views.map(view => (
{view.updatedAt && ( )}
))}
); } else if (hasViewsCapability) { viewsElement = (
{i18n('icu:StoryViewsNRepliesModal__no-views')}
); } const tabsElement = viewsElement && repliesElement ? ( {({ selectedTab }) => ( <> {selectedTab === StoryViewsNRepliesTab.Views && viewsElement} {selectedTab === StoryViewsNRepliesTab.Replies && ( <> {repliesElement} {composerElement} )} )} ) : undefined; if (!tabsElement && !viewsElement && !repliesElement && !composerElement) { return null; } return ( <>
{tabsElement || ( <> {viewsElement || repliesElement} {composerElement} )}
{deleteReplyId && ( deleteGroupStoryReply(deleteReplyId), style: 'negative', }, ]} title={i18n('icu:deleteWarning')} onClose={() => setDeleteReplyId(undefined)} onCancel={() => setDeleteReplyId(undefined)} /> )} {deleteForEveryoneReplyId && ( deleteGroupStoryReplyForEveryone(deleteForEveryoneReplyId), style: 'negative', }, ]} title={i18n('icu:deleteWarning')} onClose={() => setDeleteForEveryoneReplyId(undefined)} onCancel={() => setDeleteForEveryoneReplyId(undefined)} > {i18n('icu:deleteForEveryoneWarning')} )} ); } type ReplyOrReactionMessageProps = { containerElementRef: React.RefObject; deleteGroupStoryReply: (replyId: string) => void; deleteGroupStoryReplyForEveryone: (replyId: string) => void; displayLimit: number | undefined; getPreferredBadge: PreferredBadgeSelectorType; i18n: LocalizerType; platform: string; id: string; isInternalUser?: boolean; isSpoilerExpanded: boolean; onContextMenu?: (ev: React.MouseEvent) => void; reply: ReplyType; shouldCollapseAbove: boolean; shouldCollapseBelow: boolean; showContactModal: (contactId: string, conversationId?: string) => void; messageExpanded: (messageId: string, displayLimit: number) => void; showSpoiler: (messageId: string) => void; }; function ReplyOrReactionMessage({ containerElementRef, deleteGroupStoryReply, deleteGroupStoryReplyForEveryone, displayLimit, getPreferredBadge, i18n, id, isInternalUser, isSpoilerExpanded, messageExpanded, platform, reply, shouldCollapseAbove, shouldCollapseBelow, showContactModal, showSpoiler, }: ReplyOrReactionMessageProps) { const renderContent = (onContextMenu?: (ev: React.MouseEvent) => void) => { if (reply.reactionEmoji && !reply.deletedForEveryone) { return (
{i18n('icu:StoryViewsNRepliesModal__reacted')}
); } return (
); }; const menuOptions = [ { icon: 'module-message__context--icon module-message__context__delete-message', label: i18n('icu:StoryViewsNRepliesModal__delete-reply'), onClick: () => deleteGroupStoryReply(reply.id), }, { icon: 'module-message__context--icon module-message__context__delete-message-for-everyone', label: i18n('icu:StoryViewsNRepliesModal__delete-reply-for-everyone'), onClick: () => deleteGroupStoryReplyForEveryone(reply.id), }, ]; if (isInternalUser) { menuOptions.push({ icon: 'module-message__context--icon module-message__context__copy-timestamp', label: i18n('icu:StoryViewsNRepliesModal__copy-reply-timestamp'), onClick: () => { void window.navigator.clipboard.writeText(String(reply.timestamp)); }, }); } return reply.author.isMe && !reply.deletedForEveryone ? ( {({ openMenu, menuNode }) => ( <> {renderContent(openMenu)} {menuNode} )} ) : ( renderContent() ); }