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

171 lines
5.4 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
import React, { useCallback, useState } from 'react';
2022-07-01 00:52:03 +00:00
import { useSelector } from 'react-redux';
2024-05-22 16:24:27 +00:00
import type { ForwardMessagesPropsType } from '../ducks/globalModals';
2022-07-01 00:52:03 +00:00
import * as log from '../../logging/log';
2023-03-20 22:23:53 +00:00
import { ForwardMessagesModal } from '../../components/ForwardMessagesModal';
2022-06-17 00:48:57 +00:00
import { LinkPreviewSourceType } from '../../types/LinkPreview';
import * as Errors from '../../types/errors';
2024-05-22 16:24:27 +00:00
import { getAllComposableConversations } from '../selectors/conversations';
2022-06-17 00:48:57 +00:00
import { getIntl, getTheme, getRegionCode } from '../selectors/user';
import { getLinkPreview } from '../selectors/linkPreviews';
import { isInFullScreenCall as getIsInFullScreenCall } from '../selectors/calling';
2022-06-17 00:48:57 +00:00
import { getPreferredBadgeSelector } from '../selectors/badges';
2023-03-20 22:23:53 +00:00
import { maybeForwardMessages } from '../../util/maybeForwardMessages';
2022-07-01 00:52:03 +00:00
import {
maybeGrabLinkPreview,
resetLinkPreview,
} from '../../services/LinkPreview';
import { useGlobalModalActions } from '../ducks/globalModals';
import { useLinkPreviewActions } from '../ducks/linkPreviews';
2022-10-04 23:17:15 +00:00
import { SmartCompositionTextArea } from './CompositionTextArea';
2023-03-20 22:23:53 +00:00
import { useToastActions } from '../ducks/toast';
import { isDownloaded } from '../../types/Attachment';
import { __DEPRECATED$getMessageById } from '../../messages/getMessageById';
import { strictAssert } from '../../util/assert';
import type {
ForwardMessageData,
MessageForwardDraft,
} from '../../types/ForwardDraft';
import { getForwardMessagesProps } from '../selectors/globalModals';
2023-03-20 22:23:53 +00:00
export function SmartForwardMessagesModal(): JSX.Element | null {
const forwardMessagesProps = useSelector(getForwardMessagesProps);
if (forwardMessagesProps == null) {
return null;
}
if (
2024-05-22 16:24:27 +00:00
!forwardMessagesProps.messageDrafts.every(messageDraft => {
return messageDraft.attachments?.every(isDownloaded) ?? true;
})
) {
return null;
}
return (
<SmartForwardMessagesModalInner
forwardMessagesProps={forwardMessagesProps}
/>
);
}
function SmartForwardMessagesModalInner({
forwardMessagesProps,
}: {
forwardMessagesProps: ForwardMessagesPropsType;
}): JSX.Element | null {
const { type } = forwardMessagesProps;
2022-07-01 00:52:03 +00:00
const candidateConversations = useSelector(getAllComposableConversations);
const getPreferredBadge = useSelector(getPreferredBadgeSelector);
const i18n = useSelector(getIntl);
const linkPreviewForSource = useSelector(getLinkPreview);
const regionCode = useSelector(getRegionCode);
const theme = useSelector(getTheme);
const isInFullScreenCall = useSelector(getIsInFullScreenCall);
2021-04-27 22:35:35 +00:00
2022-07-01 00:52:03 +00:00
const { removeLinkPreview } = useLinkPreviewActions();
2023-03-20 22:23:53 +00:00
const { toggleForwardMessagesModal } = useGlobalModalActions();
const { showToast } = useToastActions();
2021-04-27 22:35:35 +00:00
2023-03-20 22:23:53 +00:00
const [drafts, setDrafts] = useState<ReadonlyArray<MessageForwardDraft>>(
() => {
2024-05-22 16:24:27 +00:00
return forwardMessagesProps.messageDrafts;
2023-03-20 22:23:53 +00:00
}
);
const handleChange = useCallback(
(
updatedDrafts: ReadonlyArray<MessageForwardDraft>,
caretLocation?: number
) => {
setDrafts(updatedDrafts);
const isLonelyDraft = updatedDrafts.length === 1;
const lonelyDraft = isLonelyDraft ? updatedDrafts[0] : null;
if (lonelyDraft == null) {
return;
}
const attachmentsLength = lonelyDraft.attachments?.length ?? 0;
if (attachmentsLength === 0) {
maybeGrabLinkPreview(
lonelyDraft.messageBody ?? '',
LinkPreviewSourceType.ForwardMessageModal,
{ caretLocation }
);
}
},
[]
);
2021-04-27 22:35:35 +00:00
const closeModal = useCallback(() => {
2022-07-01 00:52:03 +00:00
resetLinkPreview();
2024-05-22 16:24:27 +00:00
toggleForwardMessagesModal(null);
}, [toggleForwardMessagesModal]);
const doForwardMessages = useCallback(
async (
conversationIds: ReadonlyArray<string>,
finalDrafts: ReadonlyArray<MessageForwardDraft>
) => {
try {
const messages = await Promise.all(
finalDrafts.map(async (draft): Promise<ForwardMessageData> => {
2024-05-22 16:24:27 +00:00
if (draft.originalMessageId == null) {
return { draft, originalMessage: null };
}
const message = await __DEPRECATED$getMessageById(
draft.originalMessageId
);
strictAssert(message, 'no message found');
return {
draft,
originalMessage: message.attributes,
};
})
);
const didForwardSuccessfully = await maybeForwardMessages(
messages,
conversationIds
);
if (didForwardSuccessfully) {
closeModal();
forwardMessagesProps?.onForward?.();
}
} catch (err) {
log.warn('doForwardMessage', Errors.toLogFormat(err));
}
},
[forwardMessagesProps, closeModal]
);
if (!drafts.length) {
return null;
2022-07-01 00:52:03 +00:00
}
2021-04-27 22:35:35 +00:00
2022-07-01 00:52:03 +00:00
return (
2023-03-20 22:23:53 +00:00
<ForwardMessagesModal
drafts={drafts}
2022-07-01 00:52:03 +00:00
candidateConversations={candidateConversations}
doForwardMessages={doForwardMessages}
2023-03-20 22:23:53 +00:00
linkPreviewForSource={linkPreviewForSource}
2022-07-01 00:52:03 +00:00
getPreferredBadge={getPreferredBadge}
i18n={i18n}
isInFullScreenCall={isInFullScreenCall}
2022-07-01 00:52:03 +00:00
onClose={closeModal}
onChange={handleChange}
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}
2024-05-22 16:24:27 +00:00
type={type}
2023-03-20 22:23:53 +00:00
showToast={showToast}
2022-07-01 00:52:03 +00:00
theme={theme}
/>
);
}