signal-desktop/ts/state/smart/StoryViewer.tsx

176 lines
6.2 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 React, { memo, useCallback } from 'react';
2022-03-04 21:14:52 +00:00
import { useSelector } from 'react-redux';
import { StoryViewer } from '../../components/StoryViewer';
import { ToastType } from '../../types/Toast';
import { useToastActions } from '../ducks/toast';
2022-07-06 19:06:20 +00:00
import { getConversationSelector } from '../selectors/conversations';
2022-03-04 21:14:52 +00:00
import {
getEmojiSkinTone,
2022-10-25 22:18:42 +00:00
getHasStoryViewReceiptSetting,
2022-03-04 21:14:52 +00:00
getPreferredReactionEmoji,
2023-04-18 01:16:41 +00:00
getTextFormattingEnabled,
2022-11-22 22:33:15 +00:00
isInternalUser,
2022-03-04 21:14:52 +00:00
} from '../selectors/items';
2023-04-03 20:16:27 +00:00
import { getIntl, getPlatform } from '../selectors/user';
2022-03-04 21:14:52 +00:00
import { getPreferredBadgeSelector } from '../selectors/badges';
2022-07-06 19:06:20 +00:00
import {
getSelectedStoryData,
getStoryReplies,
getStoryByIdSelector,
getHasAllStoriesUnmuted,
2022-07-06 19:06:20 +00:00
} from '../selectors/stories';
import { isInFullScreenCall } from '../selectors/calling';
import { isSignalConversation as getIsSignalConversation } from '../../util/isSignalConversation';
2022-03-04 21:14:52 +00:00
import { renderEmojiPicker } from './renderEmojiPicker';
2022-07-06 19:06:20 +00:00
import { strictAssert } from '../../util/assert';
2022-11-22 22:33:15 +00:00
import { asyncShouldNeverBeCalled } from '../../util/shouldNeverBeCalled';
2024-03-12 16:29:31 +00:00
import { useEmojisActions } from '../ducks/emojis';
import { useConversationsActions } from '../ducks/conversations';
2022-03-04 21:14:52 +00:00
import { useRecentEmojis } from '../selectors/emojis';
2023-08-09 00:53:06 +00:00
import { useItemsActions } from '../ducks/items';
2023-02-24 23:18:57 +00:00
import { useAudioPlayerActions } from '../ducks/audioPlayer';
2023-03-07 22:59:44 +00:00
import { useGlobalModalActions } from '../ducks/globalModals';
2022-03-04 21:14:52 +00:00
import { useStoriesActions } from '../ducks/stories';
import { useIsWindowActive } from '../../hooks/useIsWindowActive';
2022-03-04 21:14:52 +00:00
export const SmartStoryViewer = memo(function SmartStoryViewer() {
const {
reactToStory,
replyToStory,
deleteGroupStoryReply,
deleteGroupStoryReplyForEveryone,
deleteStoryForEveryone,
loadStoryReplies,
markStoryRead,
queueStoryDownload,
setHasAllStoriesUnmuted,
viewStory,
} = useStoriesActions();
2022-03-04 21:14:52 +00:00
const { onUseEmoji } = useEmojisActions();
const {
retryMessageSend,
saveAttachment,
showConversation,
toggleHideStories,
} = useConversationsActions();
const { onSetSkinTone } = useItemsActions();
const { showToast } = useToastActions();
2023-03-07 22:59:44 +00:00
const { showContactModal } = useGlobalModalActions();
2022-03-04 21:14:52 +00:00
const isWindowActive = useIsWindowActive();
const i18n = useSelector(getIntl);
2023-04-03 20:16:27 +00:00
const platform = useSelector(getPlatform);
2022-03-04 21:14:52 +00:00
const getPreferredBadge = useSelector(getPreferredBadgeSelector);
const preferredReactionEmoji = useSelector(getPreferredReactionEmoji);
const selectedStoryData = useSelector(getSelectedStoryData);
const internalUser = useSelector(isInternalUser);
2022-11-22 22:33:15 +00:00
2022-07-06 19:06:20 +00:00
strictAssert(selectedStoryData, 'StoryViewer: !selectedStoryData');
const conversationSelector = useSelector(getConversationSelector);
2022-07-06 19:06:20 +00:00
const getStoryById = useSelector(getStoryByIdSelector);
2022-03-04 21:14:52 +00:00
const recentEmojis = useRecentEmojis();
const skinTone = useSelector(getEmojiSkinTone);
2022-04-15 00:08:46 +00:00
const replyState = useSelector(getStoryReplies);
const hasAllStoriesUnmuted = useSelector(getHasAllStoriesUnmuted);
const hasActiveCall = useSelector(isInFullScreenCall);
const hasViewReceiptSetting = useSelector(getHasStoryViewReceiptSetting);
const isFormattingEnabled = useSelector(getTextFormattingEnabled);
2023-02-24 23:18:57 +00:00
const { pauseVoiceNotePlayer } = useAudioPlayerActions();
const storyInfo = getStoryById(
conversationSelector,
selectedStoryData.messageId
);
const handleGoToConversation = useCallback(
(senderId: string) => {
showConversation({ conversationId: senderId });
},
[showConversation]
);
const handleReactToStory = useCallback(
async (emoji, story) => {
const { messageId } = story;
reactToStory(emoji, messageId);
},
[reactToStory]
);
const handleReplyToStory = useCallback(
(message, mentions, timestamp, story) => {
const conversationId = storyInfo?.conversationStory?.conversationId;
strictAssert(conversationId != null, 'conversationId is required');
replyToStory(conversationId, message, mentions, timestamp, story);
},
[storyInfo, replyToStory]
);
const handleTextTooLong = useCallback(() => {
showToast({ toastType: ToastType.MessageBodyTooLong });
}, [showToast]);
if (!storyInfo) {
return null;
}
const { conversationStory, distributionList, storyView } = storyInfo;
const { group, conversationId } = conversationStory;
const isSignalConversation = getIsSignalConversation({
id: conversationId,
});
2022-03-04 21:14:52 +00:00
return (
<StoryViewer
2022-07-06 19:06:20 +00:00
currentIndex={selectedStoryData.currentIndex}
deleteGroupStoryReply={deleteGroupStoryReply}
deleteGroupStoryReplyForEveryone={deleteGroupStoryReplyForEveryone}
deleteStoryForEveryone={deleteStoryForEveryone}
distributionList={distributionList}
2022-03-04 21:14:52 +00:00
getPreferredBadge={getPreferredBadge}
group={group}
hasActiveCall={hasActiveCall}
hasAllStoriesUnmuted={hasAllStoriesUnmuted}
2022-10-25 22:18:42 +00:00
hasViewReceiptSetting={hasViewReceiptSetting}
2022-03-04 21:14:52 +00:00
i18n={i18n}
isFormattingEnabled={isFormattingEnabled}
isInternalUser={internalUser}
isSignalConversation={isSignalConversation}
2022-11-16 22:10:11 +00:00
isWindowActive={isWindowActive}
loadStoryReplies={loadStoryReplies}
markStoryRead={markStoryRead}
2022-07-06 19:06:20 +00:00
numStories={selectedStoryData.numStories}
onGoToConversation={handleGoToConversation}
onHideStory={toggleHideStories}
onMediaPlaybackStart={pauseVoiceNotePlayer}
onReactToStory={handleReactToStory}
onReplyToStory={handleReplyToStory}
2022-03-04 21:14:52 +00:00
onSetSkinTone={onSetSkinTone}
onTextTooLong={handleTextTooLong}
2022-03-04 21:14:52 +00:00
onUseEmoji={onUseEmoji}
platform={platform}
2022-03-04 21:14:52 +00:00
preferredReactionEmoji={preferredReactionEmoji}
queueStoryDownload={queueStoryDownload}
2022-03-04 21:14:52 +00:00
recentEmojis={recentEmojis}
renderEmojiPicker={renderEmojiPicker}
2022-04-15 00:08:46 +00:00
replyState={replyState}
retryMessageSend={retryMessageSend}
saveAttachment={internalUser ? saveAttachment : asyncShouldNeverBeCalled}
setHasAllStoriesUnmuted={setHasAllStoriesUnmuted}
2023-03-07 22:59:44 +00:00
showContactModal={showContactModal}
showToast={showToast}
2022-03-04 21:14:52 +00:00
skinTone={skinTone}
2022-07-06 19:06:20 +00:00
story={storyView}
2022-08-22 17:44:23 +00:00
storyViewMode={selectedStoryData.storyViewMode}
viewStory={viewStory}
2022-11-16 22:10:11 +00:00
viewTarget={selectedStoryData.viewTarget}
2022-03-04 21:14:52 +00:00
/>
);
});