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

144 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 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';
2022-11-10 04:59:36 +00:00
import type { DraftBodyRangesType } 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';
import * as Errors from '../../types/errors';
import type { GetConversationByIdType } from '../selectors/conversations';
import {
getAllComposableConversations,
getConversationSelector,
} from '../selectors/conversations';
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';
import { useGlobalModalActions } from '../ducks/globalModals';
import { useLinkPreviewActions } from '../ducks/linkPreviews';
import { processBodyRanges } from '../selectors/message';
import { getTextWithMentions } from '../../util/getTextWithMentions';
2022-10-04 23:17:15 +00:00
import { SmartCompositionTextArea } from './CompositionTextArea';
function renderMentions(
message: ForwardMessagePropsType,
conversationSelector: GetConversationByIdType
): string | undefined {
const { text } = message;
if (!text) {
return text;
}
const bodyRanges = processBodyRanges(message, {
conversationSelector,
});
if (bodyRanges && bodyRanges.length) {
return getTextWithMentions(bodyRanges, text);
}
return text;
}
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 getConversation = useSelector(getConversationSelector);
2022-07-01 00:52:03 +00:00
const i18n = useSelector(getIntl);
const linkPreviewForSource = useSelector(getLinkPreview);
const regionCode = useSelector(getRegionCode);
const theme = useSelector(getTheme);
2021-04-27 22:35:35 +00:00
2022-07-01 00:52:03 +00:00
const { removeLinkPreview } = useLinkPreviewActions();
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
const cleanedBody = renderMentions(forwardMessageProps, getConversation);
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', Errors.toLogFormat(err));
2022-07-01 00:52:03 +00:00
}
}}
getPreferredBadge={getPreferredBadge}
hasContact={Boolean(forwardMessageProps.contact)}
i18n={i18n}
isSticker={Boolean(forwardMessageProps.isSticker)}
linkPreview={linkPreviewForSource(
LinkPreviewSourceType.ForwardMessageModal
)}
messageBody={cleanedBody}
2022-07-01 00:52:03 +00:00
onClose={closeModal}
onEditorStateChange={(
_conversationId: string | undefined,
2022-07-01 00:52:03 +00:00
messageText: string,
2022-11-10 04:59:36 +00:00
_: DraftBodyRangesType,
2022-07-01 00:52:03 +00:00
caretLocation?: number
) => {
if (!attachments.length) {
maybeGrabLinkPreview(
messageText,
LinkPreviewSourceType.ForwardMessageModal,
{ caretLocation }
2022-07-01 00:52:03 +00:00
);
}
}}
regionCode={regionCode}
2022-10-04 23:17:15 +00:00
RenderCompositionTextArea={SmartCompositionTextArea}
2022-07-01 00:52:03 +00:00
removeLinkPreview={removeLinkPreview}
theme={theme}
/>
);
}