signal-desktop/ts/components/StoryViewer.tsx

732 lines
23 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
import FocusTrap from 'focus-trap-react';
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
2022-05-03 23:50:44 +00:00
import classNames from 'classnames';
2022-03-04 21:14:52 +00:00
import { useSpring, animated, to } from '@react-spring/web';
import type { BodyRangeType, LocalizerType } from '../types/Util';
import type { ConversationType } from '../state/ducks/conversations';
import type { EmojiPickDataType } from './emoji/EmojiPicker';
import type { PreferredBadgeSelectorType } from '../state/selectors/badges';
import type { RenderEmojiPickerProps } from './conversation/ReactionPicker';
2022-07-01 00:52:03 +00:00
import type { ReplyStateType, StoryViewType } from '../types/Stories';
2022-05-04 17:43:22 +00:00
import { AnimatedEmojiGalore } from './AnimatedEmojiGalore';
2022-03-04 21:14:52 +00:00
import { Avatar, AvatarSize } from './Avatar';
import { ConfirmationDialog } from './ConfirmationDialog';
import { ContextMenuPopper } from './ContextMenu';
2022-03-04 21:14:52 +00:00
import { Intl } from './Intl';
import { MessageTimestamp } from './conversation/MessageTimestamp';
2022-07-01 00:52:03 +00:00
import { SendStatus } from '../messages/MessageSendState';
2022-03-29 01:10:08 +00:00
import { StoryImage } from './StoryImage';
2022-03-04 21:14:52 +00:00
import { StoryViewsNRepliesModal } from './StoryViewsNRepliesModal';
2022-05-06 19:02:44 +00:00
import { Theme } from '../util/theme';
2022-03-04 21:14:52 +00:00
import { getAvatarColor } from '../types/Colors';
2022-04-22 18:36:34 +00:00
import { getStoryBackground } from '../util/getStoryBackground';
2022-04-12 19:29:30 +00:00
import { getStoryDuration } from '../util/getStoryDuration';
2022-04-14 17:02:12 +00:00
import { graphemeAwareSlice } from '../util/graphemeAwareSlice';
2022-04-12 19:29:30 +00:00
import { isDownloaded, isDownloading } from '../types/Attachment';
2022-03-04 21:14:52 +00:00
import { useEscapeHandling } from '../hooks/useEscapeHandling';
import * as log from '../logging/log';
2022-03-04 21:14:52 +00:00
export type PropsType = {
2022-04-15 00:08:46 +00:00
conversationId: string;
2022-03-04 21:14:52 +00:00
getPreferredBadge: PreferredBadgeSelectorType;
2022-04-15 00:08:46 +00:00
group?: Pick<
ConversationType,
| 'acceptedMessageRequest'
| 'avatarPath'
| 'color'
| 'id'
| 'name'
| 'profileName'
| 'sharedGroupNames'
| 'title'
>;
2022-05-06 19:02:44 +00:00
hasAllStoriesMuted: boolean;
2022-03-04 21:14:52 +00:00
i18n: LocalizerType;
2022-04-15 00:08:46 +00:00
loadStoryReplies: (conversationId: string, messageId: string) => unknown;
2022-03-04 21:14:52 +00:00
markStoryRead: (mId: string) => unknown;
onClose: () => unknown;
onGoToConversation: (conversationId: string) => unknown;
onHideStory: (conversationId: string) => unknown;
2022-07-01 00:52:03 +00:00
onNextUserStories?: () => unknown;
onPrevUserStories?: () => unknown;
2022-03-04 21:14:52 +00:00
onSetSkinTone: (tone: number) => unknown;
onTextTooLong: () => unknown;
onReactToStory: (emoji: string, story: StoryViewType) => unknown;
onReplyToStory: (
message: string,
mentions: Array<BodyRangeType>,
timestamp: number,
story: StoryViewType
) => unknown;
onUseEmoji: (_: EmojiPickDataType) => unknown;
preferredReactionEmoji: Array<string>;
2022-03-29 01:10:08 +00:00
queueStoryDownload: (storyId: string) => unknown;
2022-03-04 21:14:52 +00:00
recentEmojis?: Array<string>;
renderEmojiPicker: (props: RenderEmojiPickerProps) => JSX.Element;
2022-04-15 00:08:46 +00:00
replyState?: ReplyStateType;
2022-03-04 21:14:52 +00:00
skinTone?: number;
stories: Array<StoryViewType>;
2022-05-06 19:02:44 +00:00
toggleHasAllStoriesMuted: () => unknown;
2022-03-04 21:14:52 +00:00
};
2022-04-14 17:02:12 +00:00
const CAPTION_BUFFER = 20;
const CAPTION_INITIAL_LENGTH = 200;
const CAPTION_MAX_LENGTH = 700;
2022-05-03 23:50:44 +00:00
const MOUSE_IDLE_TIME = 3000;
enum Arrow {
None,
Left,
Right,
}
2022-04-14 17:02:12 +00:00
2022-03-04 21:14:52 +00:00
export const StoryViewer = ({
2022-04-15 00:08:46 +00:00
conversationId,
2022-03-04 21:14:52 +00:00
getPreferredBadge,
group,
2022-05-06 19:02:44 +00:00
hasAllStoriesMuted,
2022-03-04 21:14:52 +00:00
i18n,
2022-04-15 00:08:46 +00:00
loadStoryReplies,
2022-03-04 21:14:52 +00:00
markStoryRead,
onClose,
onGoToConversation,
onHideStory,
2022-03-04 21:14:52 +00:00
onNextUserStories,
onPrevUserStories,
onReactToStory,
onReplyToStory,
onSetSkinTone,
onTextTooLong,
onUseEmoji,
preferredReactionEmoji,
2022-03-29 01:10:08 +00:00
queueStoryDownload,
2022-03-04 21:14:52 +00:00
recentEmojis,
renderEmojiPicker,
2022-04-15 00:08:46 +00:00
replyState,
2022-03-04 21:14:52 +00:00
skinTone,
stories,
2022-05-06 19:02:44 +00:00
toggleHasAllStoriesMuted,
2022-03-04 21:14:52 +00:00
}: PropsType): JSX.Element => {
const [currentStoryIndex, setCurrentStoryIndex] = useState(0);
2022-04-12 19:29:30 +00:00
const [storyDuration, setStoryDuration] = useState<number | undefined>();
const [isShowingContextMenu, setIsShowingContextMenu] = useState(false);
const [hasConfirmHideStory, setHasConfirmHideStory] = useState(false);
const [referenceElement, setReferenceElement] =
useState<HTMLButtonElement | null>(null);
2022-05-04 17:43:22 +00:00
const [reactionEmoji, setReactionEmoji] = useState<string | undefined>();
2022-03-04 21:14:52 +00:00
const visibleStory = stories[currentStoryIndex];
2022-07-01 00:52:03 +00:00
const { attachment, canReply, isHidden, messageId, sendState, timestamp } =
visibleStory;
2022-03-04 21:14:52 +00:00
const {
acceptedMessageRequest,
avatarPath,
color,
isMe,
id,
firstName,
2022-03-04 21:14:52 +00:00
name,
profileName,
sharedGroupNames,
title,
} = visibleStory.sender;
const [hasReplyModal, setHasReplyModal] = useState(false);
const onEscape = useCallback(() => {
if (hasReplyModal) {
setHasReplyModal(false);
} else {
onClose();
}
}, [hasReplyModal, onClose]);
useEscapeHandling(onEscape);
2022-04-14 17:02:12 +00:00
// Caption related hooks
const [hasExpandedCaption, setHasExpandedCaption] = useState<boolean>(false);
const caption = useMemo(() => {
if (!attachment?.caption) {
return;
}
return graphemeAwareSlice(
attachment.caption,
hasExpandedCaption ? CAPTION_MAX_LENGTH : CAPTION_INITIAL_LENGTH,
CAPTION_BUFFER
);
}, [attachment?.caption, hasExpandedCaption]);
// Reset expansion if messageId changes
useEffect(() => {
setHasExpandedCaption(false);
}, [messageId]);
// These exist to change currentStoryIndex to the oldest unread story a user
// has, or set to 0 whenever conversationId changes.
// We use a ref so that we only depend on conversationId changing, since
// the stories Array will change once we mark as story as viewed.
const storiesRef = useRef(stories);
2022-04-28 18:59:09 +00:00
useEffect(() => {
const unreadStoryIndex = storiesRef.current.findIndex(
story => story.isUnread
);
log.info('stories.findUnreadStory', {
unreadStoryIndex,
stories: storiesRef.current.length,
});
setCurrentStoryIndex(unreadStoryIndex < 0 ? 0 : unreadStoryIndex);
}, [conversationId]);
useEffect(() => {
storiesRef.current = stories;
}, [stories]);
2022-04-28 18:59:09 +00:00
2022-03-29 01:10:08 +00:00
// Either we show the next story in the current user's stories or we ask
// for the next user's stories.
2022-03-04 21:14:52 +00:00
const showNextStory = useCallback(() => {
if (currentStoryIndex < stories.length - 1) {
setCurrentStoryIndex(currentStoryIndex + 1);
} else {
2022-03-29 01:10:08 +00:00
setCurrentStoryIndex(0);
2022-07-01 00:52:03 +00:00
onNextUserStories?.();
2022-03-04 21:14:52 +00:00
}
}, [currentStoryIndex, onNextUserStories, stories.length]);
2022-03-29 01:10:08 +00:00
// Either we show the previous story in the current user's stories or we ask
// for the prior user's stories.
2022-03-04 21:14:52 +00:00
const showPrevStory = useCallback(() => {
if (currentStoryIndex === 0) {
2022-07-01 00:52:03 +00:00
onPrevUserStories?.();
2022-03-04 21:14:52 +00:00
} else {
setCurrentStoryIndex(currentStoryIndex - 1);
}
}, [currentStoryIndex, onPrevUserStories]);
2022-04-12 19:29:30 +00:00
useEffect(() => {
let shouldCancel = false;
(async function hydrateStoryDuration() {
if (!attachment) {
return;
}
const duration = await getStoryDuration(attachment);
if (shouldCancel) {
return;
}
log.info('stories.setStoryDuration', {
contentType: attachment.textAttachment
? 'text'
: attachment.contentType,
duration,
});
2022-04-12 19:29:30 +00:00
setStoryDuration(duration);
})();
return () => {
shouldCancel = true;
};
}, [attachment]);
const [styles, spring] = useSpring(
() => ({
2022-03-04 21:14:52 +00:00
from: { width: 0 },
to: { width: 100 },
loop: true,
2022-03-29 01:10:08 +00:00
onRest: {
width: ({ value }) => {
if (value === 100) {
showNextStory();
}
},
},
}),
[showNextStory]
);
// We need to be careful about this effect refreshing, it should only run
// every time a story changes or its duration changes.
useEffect(() => {
if (!storyDuration) {
spring.stop();
return;
}
spring.start({
config: {
duration: storyDuration,
},
from: { width: 0 },
to: { width: 100 },
2022-03-04 21:14:52 +00:00
});
2022-03-29 01:10:08 +00:00
return () => {
spring.stop();
};
}, [currentStoryIndex, spring, storyDuration]);
2022-03-04 21:14:52 +00:00
2022-05-06 19:02:44 +00:00
const [pauseStory, setPauseStory] = useState(false);
const shouldPauseViewing =
hasConfirmHideStory ||
hasExpandedCaption ||
hasReplyModal ||
2022-05-04 17:43:22 +00:00
isShowingContextMenu ||
2022-05-06 19:02:44 +00:00
pauseStory ||
2022-05-04 17:43:22 +00:00
Boolean(reactionEmoji);
2022-03-04 21:14:52 +00:00
useEffect(() => {
if (shouldPauseViewing) {
2022-03-04 21:14:52 +00:00
spring.pause();
} else {
spring.resume();
}
}, [shouldPauseViewing, spring]);
2022-03-04 21:14:52 +00:00
useEffect(() => {
2022-03-29 01:10:08 +00:00
markStoryRead(messageId);
log.info('stories.markStoryRead', { messageId });
2022-03-29 01:10:08 +00:00
}, [markStoryRead, messageId]);
// Queue all undownloaded stories once we're viewing someone's stories
const storiesToDownload = useMemo(() => {
return stories
.filter(
story =>
!isDownloaded(story.attachment) && !isDownloading(story.attachment)
)
.map(story => story.messageId);
}, [stories]);
useEffect(() => {
storiesToDownload.forEach(storyId => queueStoryDownload(storyId));
2022-03-29 01:10:08 +00:00
}, [queueStoryDownload, storiesToDownload]);
2022-03-04 21:14:52 +00:00
const navigateStories = useCallback(
(ev: KeyboardEvent) => {
if (ev.key === 'ArrowRight') {
showNextStory();
ev.preventDefault();
ev.stopPropagation();
} else if (ev.key === 'ArrowLeft') {
showPrevStory();
ev.preventDefault();
ev.stopPropagation();
}
},
[showPrevStory, showNextStory]
);
useEffect(() => {
document.addEventListener('keydown', navigateStories);
return () => {
document.removeEventListener('keydown', navigateStories);
};
}, [navigateStories]);
2022-04-15 00:08:46 +00:00
const isGroupStory = Boolean(group?.id);
useEffect(() => {
if (!isGroupStory) {
return;
}
loadStoryReplies(conversationId, messageId);
}, [conversationId, isGroupStory, loadStoryReplies, messageId]);
2022-05-03 23:50:44 +00:00
const [arrowToShow, setArrowToShow] = useState<Arrow>(Arrow.None);
useEffect(() => {
if (arrowToShow === Arrow.None) {
return;
}
let lastMouseMove: number | undefined;
function updateLastMouseMove() {
lastMouseMove = Date.now();
}
function checkMouseIdle() {
requestAnimationFrame(() => {
if (lastMouseMove && Date.now() - lastMouseMove > MOUSE_IDLE_TIME) {
setArrowToShow(Arrow.None);
} else {
checkMouseIdle();
}
});
}
checkMouseIdle();
document.addEventListener('mousemove', updateLastMouseMove);
return () => {
lastMouseMove = undefined;
document.removeEventListener('mousemove', updateLastMouseMove);
};
}, [arrowToShow]);
2022-04-15 00:08:46 +00:00
const replies =
replyState && replyState.messageId === messageId ? replyState.replies : [];
2022-07-01 00:52:03 +00:00
const views = sendState
? sendState.filter(({ status }) => status === SendStatus.Viewed)
: [];
2022-04-15 00:08:46 +00:00
const replyCount = replies.length;
2022-07-01 00:52:03 +00:00
const viewCount = views.length;
const shouldShowContextMenu = !sendState;
2022-04-15 00:08:46 +00:00
2022-03-04 21:14:52 +00:00
return (
2022-04-08 18:50:26 +00:00
<FocusTrap focusTrapOptions={{ allowOutsideClick: true }}>
<div className="StoryViewer">
2022-04-22 18:36:34 +00:00
<div
className="StoryViewer__overlay"
style={{ background: getStoryBackground(attachment) }}
/>
<div className="StoryViewer__content">
2022-07-01 00:52:03 +00:00
{onPrevUserStories && (
<button
aria-label={i18n('back')}
className={classNames(
'StoryViewer__arrow StoryViewer__arrow--left',
{
'StoryViewer__arrow--visible': arrowToShow === Arrow.Left,
}
)}
onClick={showPrevStory}
onMouseMove={() => setArrowToShow(Arrow.Left)}
type="button"
/>
)}
2022-05-03 23:50:44 +00:00
<div className="StoryViewer__protection StoryViewer__protection--top" />
<div className="StoryViewer__container">
<StoryImage
attachment={attachment}
2022-03-04 21:14:52 +00:00
i18n={i18n}
isPaused={shouldPauseViewing}
2022-05-06 19:02:44 +00:00
isMuted={hasAllStoriesMuted}
label={i18n('lightboxImageAlt')}
moduleClassName="StoryViewer__story"
queueStoryDownload={queueStoryDownload}
storyId={messageId}
2022-05-04 17:43:22 +00:00
>
{reactionEmoji && (
<div className="StoryViewer__animated-emojis">
<AnimatedEmojiGalore
emoji={reactionEmoji}
onAnimationEnd={() => {
setReactionEmoji(undefined);
}}
/>
</div>
)}
</StoryImage>
2022-04-14 17:02:12 +00:00
{hasExpandedCaption && (
2022-04-22 18:36:34 +00:00
<button
aria-label={i18n('close-popup')}
className="StoryViewer__caption__overlay"
onClick={() => setHasExpandedCaption(false)}
type="button"
/>
2022-04-14 17:02:12 +00:00
)}
2022-05-04 18:45:32 +00:00
</div>
<div className="StoryViewer__meta">
{caption && (
<div className="StoryViewer__caption">
{caption.text}
{caption.hasReadMore && !hasExpandedCaption && (
<button
className="MessageBody__read-more"
onClick={() => {
setHasExpandedCaption(true);
}}
onKeyDown={(ev: React.KeyboardEvent) => {
if (ev.key === 'Space' || ev.key === 'Enter') {
2022-04-14 17:02:12 +00:00
setHasExpandedCaption(true);
2022-05-04 18:45:32 +00:00
}
}}
type="button"
>
...
{i18n('MessageBody--read-more')}
</button>
)}
</div>
)}
2022-05-06 19:02:44 +00:00
<div className="StoryViewer__meta__playback-bar">
<div>
<Avatar
acceptedMessageRequest={acceptedMessageRequest}
avatarPath={avatarPath}
badge={undefined}
color={getAvatarColor(color)}
conversationType="direct"
i18n={i18n}
isMe={Boolean(isMe)}
name={name}
profileName={profileName}
sharedGroupNames={sharedGroupNames}
size={AvatarSize.TWENTY_EIGHT}
title={title}
/>
{group && (
<Avatar
acceptedMessageRequest={group.acceptedMessageRequest}
avatarPath={group.avatarPath}
badge={undefined}
className="StoryViewer__meta--group-avatar"
color={getAvatarColor(group.color)}
conversationType="group"
i18n={i18n}
isMe={false}
name={group.name}
profileName={group.profileName}
sharedGroupNames={group.sharedGroupNames}
size={AvatarSize.TWENTY_EIGHT}
title={group.title}
/>
)}
<div className="StoryViewer__meta--title">
{group
? i18n('Stories__from-to-group', {
name: title,
group: group.title,
})
: title}
</div>
<MessageTimestamp
i18n={i18n}
isRelativeTime
module="StoryViewer__meta--timestamp"
timestamp={timestamp}
/>
</div>
<div className="StoryViewer__meta__playback-controls">
<button
aria-label={
pauseStory
? i18n('StoryViewer__play')
: i18n('StoryViewer__pause')
}
className={
pauseStory ? 'StoryViewer__play' : 'StoryViewer__pause'
}
onClick={() => setPauseStory(!pauseStory)}
type="button"
/>
<button
aria-label={
hasAllStoriesMuted
? i18n('StoryViewer__unmute')
: i18n('StoryViewer__mute')
}
className={
hasAllStoriesMuted
? 'StoryViewer__unmute'
: 'StoryViewer__mute'
}
onClick={toggleHasAllStoriesMuted}
type="button"
/>
2022-07-01 00:52:03 +00:00
{shouldShowContextMenu && (
<button
aria-label={i18n('MyStories__more')}
className="StoryViewer__more"
onClick={() => setIsShowingContextMenu(true)}
ref={setReferenceElement}
type="button"
/>
)}
2022-05-06 19:02:44 +00:00
</div>
2022-05-04 18:45:32 +00:00
</div>
<div className="StoryViewer__progress">
{stories.map((story, index) => (
<div
className="StoryViewer__progress--container"
key={story.messageId}
>
{currentStoryIndex === index ? (
<animated.div
className="StoryViewer__progress--bar"
style={{
width: to([styles.width], width => `${width}%`),
}}
/>
) : (
<div
className="StoryViewer__progress--bar"
style={{
width: currentStoryIndex < index ? '0%' : '100%',
}}
/>
)}
</div>
))}
</div>
<div className="StoryViewer__actions">
{canReply && (
<button
className="StoryViewer__reply"
onClick={() => setHasReplyModal(true)}
tabIndex={0}
type="button"
>
<>
{viewCount > 0 || replyCount > 0 ? (
<span className="StoryViewer__reply__chevron">
{viewCount > 0 &&
(viewCount === 1 ? (
<Intl
i18n={i18n}
id="MyStories__views--singular"
components={[<strong>{viewCount}</strong>]}
/>
) : (
<Intl
i18n={i18n}
id="MyStories__views--plural"
components={[<strong>{viewCount}</strong>]}
/>
))}
{viewCount > 0 && replyCount > 0 && ' '}
{replyCount > 0 &&
(replyCount === 1 ? (
<Intl
i18n={i18n}
id="MyStories__replies--singular"
components={[<strong>{replyCount}</strong>]}
/>
) : (
<Intl
i18n={i18n}
id="MyStories__replies--plural"
components={[<strong>{replyCount}</strong>]}
/>
))}
</span>
) : null}
{!viewCount && !replyCount && (
<span className="StoryViewer__reply__arrow">
{isGroupStory
? i18n('StoryViewer__reply-group')
: i18n('StoryViewer__reply')}
</span>
)}
2022-05-04 18:45:32 +00:00
</>
</button>
)}
2022-03-04 21:14:52 +00:00
</div>
</div>
2022-07-01 00:52:03 +00:00
{onNextUserStories && (
<button
aria-label={i18n('forward')}
className={classNames(
'StoryViewer__arrow StoryViewer__arrow--right',
{
'StoryViewer__arrow--visible': arrowToShow === Arrow.Right,
}
)}
onClick={showNextStory}
onMouseMove={() => setArrowToShow(Arrow.Right)}
type="button"
/>
)}
2022-05-03 23:50:44 +00:00
<div className="StoryViewer__protection StoryViewer__protection--bottom" />
2022-04-22 18:36:34 +00:00
<button
aria-label={i18n('close')}
className="StoryViewer__close-button"
onClick={onClose}
tabIndex={0}
type="button"
/>
2022-03-04 21:14:52 +00:00
</div>
<ContextMenuPopper
isMenuShowing={isShowingContextMenu}
menuOptions={[
{
icon: 'StoryListItem__icon--hide',
label: isHidden
? i18n('StoryListItem__unhide')
: i18n('StoryListItem__hide'),
onClick: () => {
if (isHidden) {
onHideStory(id);
} else {
setHasConfirmHideStory(true);
}
},
},
{
icon: 'StoryListItem__icon--chat',
label: i18n('StoryListItem__go-to-chat'),
onClick: () => {
onGoToConversation(id);
},
},
]}
onClose={() => setIsShowingContextMenu(false)}
referenceElement={referenceElement}
2022-05-06 19:02:44 +00:00
theme={Theme.Dark}
/>
2022-04-15 00:08:46 +00:00
{hasReplyModal && canReply && (
<StoryViewsNRepliesModal
authorTitle={firstName || title}
getPreferredBadge={getPreferredBadge}
i18n={i18n}
2022-04-23 03:16:13 +00:00
isGroupStory={isGroupStory}
isMyStory={isMe}
onClose={() => setHasReplyModal(false)}
onReact={emoji => {
onReactToStory(emoji, visibleStory);
2022-05-04 17:43:22 +00:00
setHasReplyModal(false);
setReactionEmoji(emoji);
}}
onReply={(message, mentions, replyTimestamp) => {
2022-04-23 03:16:13 +00:00
if (!isGroupStory) {
setHasReplyModal(false);
}
onReplyToStory(message, mentions, replyTimestamp, visibleStory);
}}
onSetSkinTone={onSetSkinTone}
onTextTooLong={onTextTooLong}
onUseEmoji={onUseEmoji}
preferredReactionEmoji={preferredReactionEmoji}
recentEmojis={recentEmojis}
renderEmojiPicker={renderEmojiPicker}
2022-04-15 00:08:46 +00:00
replies={replies}
skinTone={skinTone}
storyPreviewAttachment={attachment}
2022-07-01 00:52:03 +00:00
views={views}
/>
)}
{hasConfirmHideStory && (
<ConfirmationDialog
actions={[
{
action: () => onHideStory(id),
style: 'affirmative',
text: i18n('StoryListItem__hide-modal--confirm'),
},
]}
i18n={i18n}
onClose={() => {
setHasConfirmHideStory(false);
}}
>
{i18n('StoryListItem__hide-modal--body', [String(firstName)])}
</ConfirmationDialog>
)}
2022-03-04 21:14:52 +00:00
</div>
</FocusTrap>
2022-03-04 21:14:52 +00:00
);
};