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

125 lines
4.3 KiB
TypeScript
Raw Normal View History

2022-07-01 00:52:03 +00:00
// Copyright 2021-2022 Signal Messenger, LLC
2021-04-27 22:35:35 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2022-07-01 00:52:03 +00:00
import React from 'react';
import { useSelector } from 'react-redux';
import type { BodyRangeType } from '../../types/Util';
2022-07-01 00:52:03 +00:00
import type { ForwardMessagePropsType } from '../ducks/globalModals';
2022-06-17 00:48:57 +00:00
import type { StateType } from '../reducer';
2022-07-01 00:52:03 +00:00
import * as log from '../../logging/log';
2022-06-17 00:48:57 +00:00
import { ForwardMessageModal } from '../../components/ForwardMessageModal';
import { LinkPreviewSourceType } from '../../types/LinkPreview';
2022-07-01 00:52:03 +00:00
import { ToastMessageBodyTooLong } from '../../components/ToastMessageBodyTooLong';
2021-04-27 22:35:35 +00:00
import { getAllComposableConversations } from '../selectors/conversations';
import { getEmojiSkinTone } from '../selectors/items';
2022-06-17 00:48:57 +00:00
import { getIntl, getTheme, getRegionCode } from '../selectors/user';
import { getLinkPreview } from '../selectors/linkPreviews';
2022-07-01 00:52:03 +00:00
import { getMessageById } from '../../messages/getMessageById';
2022-06-17 00:48:57 +00:00
import { getPreferredBadgeSelector } from '../selectors/badges';
2022-07-01 00:52:03 +00:00
import { maybeForwardMessage } from '../../util/maybeForwardMessage';
import {
maybeGrabLinkPreview,
resetLinkPreview,
} from '../../services/LinkPreview';
2021-04-27 22:35:35 +00:00
import { selectRecentEmojis } from '../selectors/emojis';
2022-07-01 00:52:03 +00:00
import { showToast } from '../../util/showToast';
import { useActions as useEmojiActions } from '../ducks/emojis';
import { useActions as useItemsActions } from '../ducks/items';
import { useGlobalModalActions } from '../ducks/globalModals';
import { useLinkPreviewActions } from '../ducks/linkPreviews';
2021-04-27 22:35:35 +00:00
2022-07-01 00:52:03 +00:00
export function SmartForwardMessageModal(): JSX.Element | null {
const forwardMessageProps = useSelector<
StateType,
ForwardMessagePropsType | undefined
>(state => state.globalModals.forwardMessageProps);
const candidateConversations = useSelector(getAllComposableConversations);
const getPreferredBadge = useSelector(getPreferredBadgeSelector);
const i18n = useSelector(getIntl);
const linkPreviewForSource = useSelector(getLinkPreview);
const recentEmojis = useSelector(selectRecentEmojis);
const regionCode = useSelector(getRegionCode);
const skinTone = useSelector(getEmojiSkinTone);
const theme = useSelector(getTheme);
2021-04-27 22:35:35 +00:00
2022-07-01 00:52:03 +00:00
const { removeLinkPreview } = useLinkPreviewActions();
const { onUseEmoji: onPickEmoji } = useEmojiActions();
const { onSetSkinTone } = useItemsActions();
const { toggleForwardMessageModal } = useGlobalModalActions();
2021-04-27 22:35:35 +00:00
2022-07-01 00:52:03 +00:00
if (!forwardMessageProps) {
return null;
}
2021-04-27 22:35:35 +00:00
2022-07-01 00:52:03 +00:00
const { attachments = [] } = forwardMessageProps;
2021-04-27 22:35:35 +00:00
2022-07-01 00:52:03 +00:00
function closeModal() {
resetLinkPreview();
toggleForwardMessageModal();
}
2021-04-27 22:35:35 +00:00
2022-07-01 00:52:03 +00:00
return (
<ForwardMessageModal
attachments={attachments}
candidateConversations={candidateConversations}
doForwardMessage={async (
conversationIds,
messageBody,
includedAttachments,
linkPreview
) => {
try {
const message = await getMessageById(forwardMessageProps.id);
if (!message) {
throw new Error('No message found');
}
const didForwardSuccessfully = await maybeForwardMessage(
message.attributes,
conversationIds,
messageBody,
includedAttachments,
linkPreview
);
if (didForwardSuccessfully) {
closeModal();
}
} catch (err) {
log.warn('doForwardMessage', err && err.stack ? err.stack : err);
}
}}
getPreferredBadge={getPreferredBadge}
hasContact={Boolean(forwardMessageProps.contact)}
i18n={i18n}
isSticker={Boolean(forwardMessageProps.isSticker)}
linkPreview={linkPreviewForSource(
LinkPreviewSourceType.ForwardMessageModal
)}
messageBody={forwardMessageProps.text}
onClose={closeModal}
onEditorStateChange={(
messageText: string,
_: Array<BodyRangeType>,
caretLocation?: number
) => {
if (!attachments.length) {
maybeGrabLinkPreview(
messageText,
LinkPreviewSourceType.ForwardMessageModal,
caretLocation
);
}
}}
onPickEmoji={onPickEmoji}
onSetSkinTone={onSetSkinTone}
onTextTooLong={() => showToast(ToastMessageBodyTooLong)}
recentEmojis={recentEmojis}
regionCode={regionCode}
removeLinkPreview={removeLinkPreview}
skinTone={skinTone}
theme={theme}
/>
);
}