signal-desktop/ts/components/StoryViewsNRepliesModal.tsx

459 lines
15 KiB
TypeScript
Raw Normal View History

2022-03-04 21:14:52 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2022-04-23 03:16:13 +00:00
import React, { useCallback, useEffect, useRef, useState } from 'react';
2022-03-04 21:14:52 +00:00
import classNames from 'classnames';
import { usePopper } from 'react-popper';
import type { AttachmentType } from '../types/Attachment';
import type { BodyRangeType, LocalizerType } from '../types/Util';
2022-08-23 18:02:51 +00:00
import type { ConversationType } from '../state/ducks/conversations';
2022-03-04 21:14:52 +00:00
import type { EmojiPickDataType } from './emoji/EmojiPicker';
import type { InputApi } from './CompositionInput';
import type { PreferredBadgeSelectorType } from '../state/selectors/badges';
import type { RenderEmojiPickerProps } from './conversation/ReactionPicker';
2022-07-01 00:52:03 +00:00
import type { ReplyType, StorySendStateType } from '../types/Stories';
2022-03-04 21:14:52 +00:00
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';
2022-03-04 21:14:52 +00:00
import { MessageTimestamp } from './conversation/MessageTimestamp';
import { Modal } from './Modal';
import { Quote } from './conversation/Quote';
import { ReactionPicker } from './conversation/ReactionPicker';
import { Tabs } from './Tabs';
2022-04-15 00:08:46 +00:00
import { Theme } from '../util/theme';
2022-03-04 21:14:52 +00:00
import { ThemeType } from '../types/Util';
import { WidthBreakpoint } from './_util';
2022-03-04 21:14:52 +00:00
import { getAvatarColor } from '../types/Colors';
import { getStoryReplyText } from '../util/getStoryReplyText';
import { shouldNeverBeCalled } from '../util/shouldNeverBeCalled';
// 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,
canDownload: false,
canReact: false,
canReply: false,
canRetry: false,
canRetryDeleteForEveryone: false,
checkForAccount: shouldNeverBeCalled,
clearSelectedMessage: shouldNeverBeCalled,
containerWidthBreakpoint: WidthBreakpoint.Medium,
deleteMessage: shouldNeverBeCalled,
deleteMessageForEveryone: shouldNeverBeCalled,
displayTapToViewMessage: shouldNeverBeCalled,
doubleCheckMissingQuoteReference: shouldNeverBeCalled,
downloadAttachment: shouldNeverBeCalled,
isBlocked: false,
isMessageRequestAccepted: true,
kickOffAttachmentDownload: shouldNeverBeCalled,
markAttachmentAsCorrupted: shouldNeverBeCalled,
markViewed: shouldNeverBeCalled,
messageExpanded: shouldNeverBeCalled,
openConversation: shouldNeverBeCalled,
openGiftBadge: shouldNeverBeCalled,
openLink: shouldNeverBeCalled,
previews: [],
reactToMessage: shouldNeverBeCalled,
renderAudioAttachment: () => <div />,
renderEmojiPicker: () => <div />,
renderReactionPicker: () => <div />,
replyToMessage: shouldNeverBeCalled,
retryDeleteForEveryone: shouldNeverBeCalled,
retrySend: shouldNeverBeCalled,
scrollToQuotedMessage: shouldNeverBeCalled,
showContactDetail: shouldNeverBeCalled,
showContactModal: shouldNeverBeCalled,
showExpiredIncomingTapToViewToast: shouldNeverBeCalled,
showExpiredOutgoingTapToViewToast: shouldNeverBeCalled,
showForwardMessageModal: shouldNeverBeCalled,
showMessageDetail: shouldNeverBeCalled,
showVisualAttachment: shouldNeverBeCalled,
startConversation: shouldNeverBeCalled,
theme: ThemeType.dark,
viewStory: shouldNeverBeCalled,
};
2022-03-04 21:14:52 +00:00
enum Tab {
Replies = 'Replies',
Views = 'Views',
}
export type PropsType = {
authorTitle: string;
2022-07-25 18:55:44 +00:00
canReply: boolean;
2022-03-04 21:14:52 +00:00
getPreferredBadge: PreferredBadgeSelectorType;
hasReadReceiptSetting: boolean;
2022-03-04 21:14:52 +00:00
i18n: LocalizerType;
2022-04-23 03:16:13 +00:00
isGroupStory?: boolean;
2022-03-04 21:14:52 +00:00
onClose: () => unknown;
onReact: (emoji: string) => unknown;
onReply: (
message: string,
mentions: Array<BodyRangeType>,
timestamp: number
) => unknown;
onSetSkinTone: (tone: number) => unknown;
onTextTooLong: () => unknown;
onUseEmoji: (_: EmojiPickDataType) => unknown;
preferredReactionEmoji: Array<string>;
recentEmojis?: Array<string>;
renderEmojiPicker: (props: RenderEmojiPickerProps) => JSX.Element;
replies: Array<ReplyType>;
skinTone?: number;
2022-08-23 18:02:51 +00:00
sortedGroupMembers?: Array<ConversationType>;
2022-03-04 21:14:52 +00:00
storyPreviewAttachment?: AttachmentType;
2022-07-01 00:52:03 +00:00
views: Array<StorySendStateType>;
2022-03-04 21:14:52 +00:00
};
export const StoryViewsNRepliesModal = ({
authorTitle,
2022-07-25 18:55:44 +00:00
canReply,
2022-03-04 21:14:52 +00:00
getPreferredBadge,
hasReadReceiptSetting,
2022-03-04 21:14:52 +00:00
i18n,
2022-04-23 03:16:13 +00:00
isGroupStory,
2022-03-04 21:14:52 +00:00
onClose,
onReact,
onReply,
onSetSkinTone,
onTextTooLong,
onUseEmoji,
preferredReactionEmoji,
recentEmojis,
renderEmojiPicker,
replies,
skinTone,
2022-08-23 18:02:51 +00:00
sortedGroupMembers,
2022-03-04 21:14:52 +00:00
storyPreviewAttachment,
views,
2022-07-25 18:55:44 +00:00
}: PropsType): JSX.Element | null => {
const containerElementRef = useRef<HTMLDivElement | null>(null);
2022-04-23 03:16:13 +00:00
const inputApiRef = useRef<InputApi | undefined>();
const [bottom, setBottom] = useState<HTMLDivElement | null>(null);
2022-03-04 21:14:52 +00:00
const [messageBodyText, setMessageBodyText] = useState('');
const [showReactionPicker, setShowReactionPicker] = useState(false);
const focusComposer = useCallback(() => {
if (inputApiRef.current) {
inputApiRef.current.focus();
}
}, [inputApiRef]);
const insertEmoji = useCallback(
(e: EmojiPickDataType) => {
onUseEmoji(e);
2022-03-04 21:14:52 +00:00
},
[onUseEmoji]
2022-03-04 21:14:52 +00:00
);
const [referenceElement, setReferenceElement] =
useState<HTMLButtonElement | null>(null);
const [popperElement, setPopperElement] = useState<HTMLDivElement | null>(
null
);
const { styles, attributes } = usePopper(referenceElement, popperElement, {
placement: 'top-start',
strategy: 'fixed',
});
2022-04-23 03:16:13 +00:00
useEffect(() => {
if (replies.length) {
bottom?.scrollIntoView({ behavior: 'smooth' });
}
}, [bottom, replies.length]);
2022-03-04 21:14:52 +00:00
let composerElement: JSX.Element | undefined;
2022-08-25 16:10:56 +00:00
if (canReply) {
2022-03-04 21:14:52 +00:00
composerElement = (
<>
{!isGroupStory && (
<Quote
authorTitle={authorTitle}
conversationColor="ultramarine"
2022-03-04 21:14:52 +00:00
i18n={i18n}
isFromMe={false}
2022-05-11 20:59:58 +00:00
isGiftBadge={false}
isStoryReply
isViewOnce={false}
moduleClassName="StoryViewsNRepliesModal__quote"
rawAttachment={storyPreviewAttachment}
referencedMessageNotFound={false}
text={getStoryReplyText(i18n, storyPreviewAttachment)}
/>
)}
<div className="StoryViewsNRepliesModal__compose-container">
<div className="StoryViewsNRepliesModal__composer">
<CompositionInput
draftText={messageBodyText}
getPreferredBadge={getPreferredBadge}
2022-03-04 21:14:52 +00:00
i18n={i18n}
inputApi={inputApiRef}
moduleClassName="StoryViewsNRepliesModal__input"
onEditorStateChange={messageText => {
setMessageBodyText(messageText);
2022-03-04 21:14:52 +00:00
}}
onPickEmoji={insertEmoji}
onSubmit={(...args) => {
inputApiRef.current?.reset();
onReply(...args);
2022-03-04 21:14:52 +00:00
}}
onTextTooLong={onTextTooLong}
placeholder={
isGroupStory
? i18n('StoryViewer__reply-group')
: i18n('StoryViewer__reply')
}
2022-08-23 18:02:51 +00:00
sortedGroupMembers={sortedGroupMembers}
theme={ThemeType.dark}
>
<EmojiButton
className="StoryViewsNRepliesModal__emoji-button"
i18n={i18n}
onPickEmoji={insertEmoji}
onClose={focusComposer}
recentEmojis={recentEmojis}
skinTone={skinTone}
onSetSkinTone={onSetSkinTone}
/>
</CompositionInput>
2022-03-04 21:14:52 +00:00
</div>
<button
aria-label={i18n('StoryViewsNRepliesModal__react')}
className="StoryViewsNRepliesModal__react"
onClick={() => {
setShowReactionPicker(!showReactionPicker);
}}
ref={setReferenceElement}
type="button"
/>
{showReactionPicker && (
<div
ref={setPopperElement}
style={styles.popper}
{...attributes.popper}
>
<ReactionPicker
i18n={i18n}
onClose={() => {
setShowReactionPicker(false);
}}
onPick={emoji => {
setShowReactionPicker(false);
onReact(emoji);
}}
onSetSkinTone={onSetSkinTone}
preferredReactionEmoji={preferredReactionEmoji}
renderEmojiPicker={renderEmojiPicker}
/>
</div>
)}
</div>
</>
2022-03-04 21:14:52 +00:00
);
}
2022-04-23 03:16:13 +00:00
let repliesElement: JSX.Element | undefined;
if (replies.length) {
repliesElement = (
<div
className="StoryViewsNRepliesModal__replies"
ref={containerElementRef}
>
{replies.map((reply, index) =>
2022-04-23 03:16:13 +00:00
reply.reactionEmoji ? (
<div className="StoryViewsNRepliesModal__reaction" key={reply.id}>
<div className="StoryViewsNRepliesModal__reaction--container">
<Avatar
acceptedMessageRequest={reply.author.acceptedMessageRequest}
avatarPath={reply.author.avatarPath}
badge={getPreferredBadge(reply.author.badges)}
color={getAvatarColor(reply.author.color)}
2022-04-23 03:16:13 +00:00
conversationType="direct"
i18n={i18n}
isMe={Boolean(reply.author.isMe)}
name={reply.author.name}
profileName={reply.author.profileName}
sharedGroupNames={reply.author.sharedGroupNames || []}
2022-04-23 03:16:13 +00:00
size={AvatarSize.TWENTY_EIGHT}
theme={ThemeType.dark}
title={reply.author.title}
2022-04-23 03:16:13 +00:00
/>
<div className="StoryViewsNRepliesModal__reaction--body">
<div className="StoryViewsNRepliesModal__reply--title">
<ContactName
contactNameColor={reply.contactNameColor}
2022-08-25 16:10:56 +00:00
title={
reply.author.isMe ? i18n('you') : reply.author.title
}
2022-04-23 03:16:13 +00:00
/>
</div>
{i18n('StoryViewsNRepliesModal__reacted')}
<MessageTimestamp
i18n={i18n}
isRelativeTime
2022-04-23 03:16:13 +00:00
module="StoryViewsNRepliesModal__reply--timestamp"
timestamp={reply.timestamp}
/>
</div>
</div>
<Emojify text={reply.reactionEmoji} />
</div>
) : (
<div key={reply.id}>
<Message
{...MESSAGE_DEFAULT_PROPS}
author={reply.author}
2022-08-25 16:10:56 +00:00
contactNameColor={reply.contactNameColor}
containerElementRef={containerElementRef}
conversationColor="ultramarine"
conversationId={reply.conversationId}
conversationTitle={reply.author.title}
conversationType="group"
direction="incoming"
disableMenu
getPreferredBadge={getPreferredBadge}
2022-03-04 21:14:52 +00:00
i18n={i18n}
id={reply.id}
interactionMode="mouse"
readStatus={reply.readStatus}
renderingContext="StoryViewsNRepliesModal"
shouldCollapseAbove={
2022-08-25 16:10:56 +00:00
reply.conversationId === replies[index - 1]?.conversationId &&
!replies[index - 1]?.reactionEmoji
}
shouldCollapseBelow={
2022-08-25 16:10:56 +00:00
reply.conversationId === replies[index + 1]?.conversationId &&
!replies[index + 1]?.reactionEmoji
}
shouldHideMetadata={false}
text={reply.body}
textDirection={TextDirection.Default}
timestamp={reply.timestamp}
2022-03-04 21:14:52 +00:00
/>
</div>
2022-04-23 03:16:13 +00:00
)
)}
<div ref={setBottom} />
</div>
);
} else if (isGroupStory) {
repliesElement = (
<div className="StoryViewsNRepliesModal__replies--none">
{i18n('StoryViewsNRepliesModal__no-replies')}
</div>
);
}
2022-03-04 21:14:52 +00:00
let viewsElement: JSX.Element | undefined;
if (!hasReadReceiptSetting) {
viewsElement = (
<div className="StoryViewsNRepliesModal__read-receipts-off">
{i18n('StoryViewsNRepliesModal__read-receipts-off')}
</div>
);
} else if (views.length) {
viewsElement = (
<div className="StoryViewsNRepliesModal__views">
{views.map(view => (
<div
className="StoryViewsNRepliesModal__view"
key={view.recipient.id}
>
<div>
<Avatar
acceptedMessageRequest={view.recipient.acceptedMessageRequest}
avatarPath={view.recipient.avatarPath}
badge={undefined}
color={getAvatarColor(view.recipient.color)}
conversationType="direct"
i18n={i18n}
isMe={Boolean(view.recipient.isMe)}
name={view.recipient.name}
profileName={view.recipient.profileName}
sharedGroupNames={view.recipient.sharedGroupNames || []}
size={AvatarSize.TWENTY_EIGHT}
title={view.recipient.title}
/>
<span className="StoryViewsNRepliesModal__view--name">
<ContactName title={view.recipient.title} />
</span>
</div>
{view.updatedAt && (
<MessageTimestamp
i18n={i18n}
module="StoryViewsNRepliesModal__view--timestamp"
timestamp={view.updatedAt}
/>
)}
2022-03-04 21:14:52 +00:00
</div>
))}
</div>
);
}
2022-03-04 21:14:52 +00:00
const tabsElement =
views.length && replies.length ? (
<Tabs
initialSelectedTab={Tab.Views}
moduleClassName="StoryViewsNRepliesModal__tabs"
tabs={[
{
id: Tab.Views,
label: i18n('StoryViewsNRepliesModal__tab--views'),
},
{
id: Tab.Replies,
label: i18n('StoryViewsNRepliesModal__tab--replies'),
},
]}
>
{({ selectedTab }) => (
<>
{selectedTab === Tab.Views && viewsElement}
{selectedTab === Tab.Replies && (
<>
{repliesElement}
{composerElement}
</>
)}
</>
)}
</Tabs>
) : undefined;
2022-07-25 18:55:44 +00:00
if (!tabsElement && !viewsElement && !repliesElement && !composerElement) {
return null;
}
2022-03-04 21:14:52 +00:00
return (
<Modal
i18n={i18n}
2022-04-23 03:16:13 +00:00
moduleClassName="StoryViewsNRepliesModal"
2022-03-04 21:14:52 +00:00
onClose={onClose}
2022-04-23 03:16:13 +00:00
useFocusTrap={Boolean(composerElement)}
2022-04-15 00:08:46 +00:00
theme={Theme.Dark}
2022-03-04 21:14:52 +00:00
>
2022-04-23 03:16:13 +00:00
<div
className={classNames({
'StoryViewsNRepliesModal--group': Boolean(isGroupStory),
})}
>
{tabsElement || (
<>
{viewsElement || repliesElement}
{composerElement}
</>
)}
</div>
2022-03-04 21:14:52 +00:00
</Modal>
);
};