2022-03-04 21:14:52 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-04-07 21:11:33 +00:00
|
|
|
import FocusTrap from 'focus-trap-react';
|
2022-03-29 01:10:08 +00:00
|
|
|
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
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';
|
|
|
|
import type { StoryViewType } from './StoryListItem';
|
|
|
|
import { Avatar, AvatarSize } from './Avatar';
|
|
|
|
import { Intl } from './Intl';
|
|
|
|
import { MessageTimestamp } from './conversation/MessageTimestamp';
|
2022-03-29 01:10:08 +00:00
|
|
|
import { StoryImage } from './StoryImage';
|
2022-03-04 21:14:52 +00:00
|
|
|
import { StoryViewsNRepliesModal } from './StoryViewsNRepliesModal';
|
|
|
|
import { getAvatarColor } from '../types/Colors';
|
2022-04-12 19:29:30 +00:00
|
|
|
import { getStoryDuration } from '../util/getStoryDuration';
|
|
|
|
import { isDownloaded, isDownloading } from '../types/Attachment';
|
2022-03-04 21:14:52 +00:00
|
|
|
import { useEscapeHandling } from '../hooks/useEscapeHandling';
|
|
|
|
|
|
|
|
export type PropsType = {
|
|
|
|
getPreferredBadge: PreferredBadgeSelectorType;
|
|
|
|
group?: ConversationType;
|
|
|
|
i18n: LocalizerType;
|
|
|
|
markStoryRead: (mId: string) => unknown;
|
|
|
|
onClose: () => unknown;
|
|
|
|
onNextUserStories: () => unknown;
|
|
|
|
onPrevUserStories: () => unknown;
|
|
|
|
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>;
|
|
|
|
replies?: number;
|
|
|
|
renderEmojiPicker: (props: RenderEmojiPickerProps) => JSX.Element;
|
|
|
|
skinTone?: number;
|
|
|
|
stories: Array<StoryViewType>;
|
|
|
|
views?: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const StoryViewer = ({
|
|
|
|
getPreferredBadge,
|
|
|
|
group,
|
|
|
|
i18n,
|
|
|
|
markStoryRead,
|
|
|
|
onClose,
|
|
|
|
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,
|
|
|
|
replies,
|
|
|
|
skinTone,
|
|
|
|
stories,
|
|
|
|
views,
|
|
|
|
}: PropsType): JSX.Element => {
|
|
|
|
const [currentStoryIndex, setCurrentStoryIndex] = useState(0);
|
2022-04-12 19:29:30 +00:00
|
|
|
const [storyDuration, setStoryDuration] = useState<number | undefined>();
|
2022-03-04 21:14:52 +00:00
|
|
|
|
|
|
|
const visibleStory = stories[currentStoryIndex];
|
|
|
|
|
2022-03-29 01:10:08 +00:00
|
|
|
const { attachment, messageId, timestamp } = visibleStory;
|
2022-03-04 21:14:52 +00:00
|
|
|
const {
|
|
|
|
acceptedMessageRequest,
|
|
|
|
avatarPath,
|
|
|
|
color,
|
|
|
|
isMe,
|
|
|
|
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-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-03-04 21:14:52 +00:00
|
|
|
onNextUserStories();
|
|
|
|
}
|
|
|
|
}, [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) {
|
|
|
|
onPrevUserStories();
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
setStoryDuration(duration);
|
|
|
|
})();
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
shouldCancel = true;
|
|
|
|
};
|
|
|
|
}, [attachment]);
|
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
const [styles, spring] = useSpring(() => ({
|
|
|
|
from: { width: 0 },
|
|
|
|
to: { width: 100 },
|
|
|
|
loop: true,
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Adding "currentStoryIndex" to the dependency list here to explcitly signal
|
|
|
|
// that this useEffect should run whenever the story changes.
|
|
|
|
useEffect(() => {
|
|
|
|
spring.start({
|
2022-04-12 19:29:30 +00:00
|
|
|
config: {
|
|
|
|
duration: storyDuration,
|
|
|
|
},
|
2022-03-04 21:14:52 +00:00
|
|
|
from: { width: 0 },
|
|
|
|
to: { width: 100 },
|
2022-03-29 01:10:08 +00:00
|
|
|
onRest: {
|
|
|
|
width: ({ value }) => {
|
|
|
|
if (value === 100) {
|
|
|
|
showNextStory();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2022-03-04 21:14:52 +00:00
|
|
|
});
|
2022-03-29 01:10:08 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
spring.stop();
|
|
|
|
};
|
2022-04-12 19:29:30 +00:00
|
|
|
}, [currentStoryIndex, showNextStory, spring, storyDuration]);
|
2022-03-04 21:14:52 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (hasReplyModal) {
|
|
|
|
spring.pause();
|
|
|
|
} else {
|
|
|
|
spring.resume();
|
|
|
|
}
|
|
|
|
}, [hasReplyModal, spring]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-03-29 01:10:08 +00:00
|
|
|
markStoryRead(messageId);
|
|
|
|
}, [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(id => queueStoryDownload(id));
|
|
|
|
}, [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]);
|
|
|
|
|
|
|
|
return (
|
2022-04-08 18:50:26 +00:00
|
|
|
<FocusTrap focusTrapOptions={{ allowOutsideClick: true }}>
|
2022-04-07 21:11:33 +00:00
|
|
|
<div className="StoryViewer">
|
|
|
|
<div className="StoryViewer__overlay" />
|
|
|
|
<div className="StoryViewer__content">
|
|
|
|
<button
|
|
|
|
aria-label={i18n('MyStories__more')}
|
|
|
|
className="StoryViewer__more"
|
|
|
|
tabIndex={0}
|
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
aria-label={i18n('close')}
|
|
|
|
className="StoryViewer__close-button"
|
|
|
|
onClick={onClose}
|
|
|
|
tabIndex={0}
|
|
|
|
type="button"
|
2022-03-29 01:10:08 +00:00
|
|
|
/>
|
2022-04-07 21:11:33 +00:00
|
|
|
<div className="StoryViewer__container">
|
|
|
|
<StoryImage
|
|
|
|
attachment={attachment}
|
2022-03-04 21:14:52 +00:00
|
|
|
i18n={i18n}
|
2022-04-07 21:11:33 +00:00
|
|
|
label={i18n('lightboxImageAlt')}
|
|
|
|
moduleClassName="StoryViewer__story"
|
|
|
|
queueStoryDownload={queueStoryDownload}
|
|
|
|
storyId={messageId}
|
2022-03-04 21:14:52 +00:00
|
|
|
/>
|
2022-04-07 21:11:33 +00:00
|
|
|
<div className="StoryViewer__meta">
|
2022-03-04 21:14:52 +00:00
|
|
|
<Avatar
|
2022-04-07 21:11:33 +00:00
|
|
|
acceptedMessageRequest={acceptedMessageRequest}
|
|
|
|
avatarPath={avatarPath}
|
2022-03-04 21:14:52 +00:00
|
|
|
badge={undefined}
|
2022-04-07 21:11:33 +00:00
|
|
|
color={getAvatarColor(color)}
|
|
|
|
conversationType="direct"
|
2022-03-04 21:14:52 +00:00
|
|
|
i18n={i18n}
|
2022-04-07 21:11:33 +00:00
|
|
|
isMe={Boolean(isMe)}
|
|
|
|
name={name}
|
|
|
|
profileName={profileName}
|
|
|
|
sharedGroupNames={sharedGroupNames}
|
2022-03-04 21:14:52 +00:00
|
|
|
size={AvatarSize.TWENTY_EIGHT}
|
2022-04-07 21:11:33 +00:00
|
|
|
title={title}
|
2022-03-04 21:14:52 +00:00
|
|
|
/>
|
2022-04-07 21:11:33 +00:00
|
|
|
{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}
|
|
|
|
module="StoryViewer__meta--timestamp"
|
|
|
|
timestamp={timestamp}
|
|
|
|
/>
|
|
|
|
<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>
|
2022-03-04 21:14:52 +00:00
|
|
|
</div>
|
2022-04-07 21:11:33 +00:00
|
|
|
</div>
|
|
|
|
<div className="StoryViewer__actions">
|
|
|
|
{isMe ? (
|
|
|
|
<>
|
|
|
|
{views &&
|
|
|
|
(views === 1 ? (
|
|
|
|
<Intl
|
|
|
|
i18n={i18n}
|
|
|
|
id="MyStories__views--singular"
|
|
|
|
components={[<strong>{views}</strong>]}
|
2022-03-04 21:14:52 +00:00
|
|
|
/>
|
|
|
|
) : (
|
2022-04-07 21:11:33 +00:00
|
|
|
<Intl
|
|
|
|
i18n={i18n}
|
|
|
|
id="MyStories__views--plural"
|
|
|
|
components={[<strong>{views}</strong>]}
|
2022-03-04 21:14:52 +00:00
|
|
|
/>
|
2022-04-07 21:11:33 +00:00
|
|
|
))}
|
|
|
|
{views && replies && ' '}
|
|
|
|
{replies &&
|
|
|
|
(replies === 1 ? (
|
|
|
|
<Intl
|
|
|
|
i18n={i18n}
|
|
|
|
id="MyStories__replies--singular"
|
|
|
|
components={[<strong>{replies}</strong>]}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Intl
|
|
|
|
i18n={i18n}
|
|
|
|
id="MyStories__replies--plural"
|
|
|
|
components={[<strong>{replies}</strong>]}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
className="StoryViewer__reply"
|
|
|
|
onClick={() => setHasReplyModal(true)}
|
|
|
|
tabIndex={0}
|
|
|
|
type="button"
|
|
|
|
>
|
|
|
|
{i18n('StoryViewer__reply')}
|
|
|
|
</button>
|
|
|
|
)}
|
2022-03-04 21:14:52 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-04-07 21:11:33 +00:00
|
|
|
{hasReplyModal && (
|
|
|
|
<StoryViewsNRepliesModal
|
|
|
|
authorTitle={title}
|
|
|
|
getPreferredBadge={getPreferredBadge}
|
|
|
|
i18n={i18n}
|
|
|
|
isMyStory={isMe}
|
|
|
|
onClose={() => setHasReplyModal(false)}
|
|
|
|
onReact={emoji => {
|
|
|
|
onReactToStory(emoji, visibleStory);
|
|
|
|
}}
|
|
|
|
onReply={(message, mentions, replyTimestamp) => {
|
|
|
|
setHasReplyModal(false);
|
|
|
|
onReplyToStory(message, mentions, replyTimestamp, visibleStory);
|
|
|
|
}}
|
|
|
|
onSetSkinTone={onSetSkinTone}
|
|
|
|
onTextTooLong={onTextTooLong}
|
|
|
|
onUseEmoji={onUseEmoji}
|
|
|
|
preferredReactionEmoji={preferredReactionEmoji}
|
|
|
|
recentEmojis={recentEmojis}
|
|
|
|
renderEmojiPicker={renderEmojiPicker}
|
|
|
|
replies={[]}
|
|
|
|
skinTone={skinTone}
|
|
|
|
storyPreviewAttachment={attachment}
|
|
|
|
views={[]}
|
|
|
|
/>
|
|
|
|
)}
|
2022-03-04 21:14:52 +00:00
|
|
|
</div>
|
2022-04-07 21:11:33 +00:00
|
|
|
</FocusTrap>
|
2022-03-04 21:14:52 +00:00
|
|
|
);
|
|
|
|
};
|