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
|
|
|
|
|
2024-03-19 17:12:32 +00:00
|
|
|
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';
|
2022-11-22 18:43:43 +00:00
|
|
|
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';
|
2024-06-12 20:40:06 +00:00
|
|
|
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';
|
2023-06-16 18:41:52 +00:00
|
|
|
import { isDownloaded } from '../../types/Attachment';
|
2023-10-04 00:12:57 +00:00
|
|
|
import { __DEPRECATED$getMessageById } from '../../messages/getMessageById';
|
|
|
|
import { strictAssert } from '../../util/assert';
|
2023-10-11 19:06:43 +00:00
|
|
|
import type {
|
|
|
|
ForwardMessageData,
|
|
|
|
MessageForwardDraft,
|
|
|
|
} from '../../types/ForwardDraft';
|
2024-03-13 20:44:13 +00:00
|
|
|
import { getForwardMessagesProps } from '../selectors/globalModals';
|
2023-06-16 18:41:52 +00:00
|
|
|
|
2023-03-20 22:23:53 +00:00
|
|
|
export function SmartForwardMessagesModal(): JSX.Element | null {
|
2024-03-13 20:44:13 +00:00
|
|
|
const forwardMessagesProps = useSelector(getForwardMessagesProps);
|
2023-06-16 18:41:52 +00:00
|
|
|
|
|
|
|
if (forwardMessagesProps == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2024-05-22 16:24:27 +00:00
|
|
|
!forwardMessagesProps.messageDrafts.every(messageDraft => {
|
|
|
|
return messageDraft.attachments?.every(isDownloaded) ?? true;
|
2023-06-16 18:41:52 +00:00
|
|
|
})
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SmartForwardMessagesModalInner
|
|
|
|
forwardMessagesProps={forwardMessagesProps}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function SmartForwardMessagesModalInner({
|
|
|
|
forwardMessagesProps,
|
|
|
|
}: {
|
|
|
|
forwardMessagesProps: ForwardMessagesPropsType;
|
|
|
|
}): JSX.Element | null {
|
2024-06-12 20:40:06 +00:00
|
|
|
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);
|
2024-06-12 20:40:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-03-19 17:12:32 +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
|
|
|
|
2024-03-19 17:12:32 +00:00
|
|
|
const closeModal = useCallback(() => {
|
2022-07-01 00:52:03 +00:00
|
|
|
resetLinkPreview();
|
2024-05-22 16:24:27 +00:00
|
|
|
toggleForwardMessagesModal(null);
|
2024-03-19 17:12:32 +00:00
|
|
|
}, [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 };
|
|
|
|
}
|
2024-03-19 17:12:32 +00:00
|
|
|
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}
|
2024-03-19 17:12:32 +00:00
|
|
|
doForwardMessages={doForwardMessages}
|
2023-03-20 22:23:53 +00:00
|
|
|
linkPreviewForSource={linkPreviewForSource}
|
2022-07-01 00:52:03 +00:00
|
|
|
getPreferredBadge={getPreferredBadge}
|
|
|
|
i18n={i18n}
|
2024-06-12 20:40:06 +00:00
|
|
|
isInFullScreenCall={isInFullScreenCall}
|
2022-07-01 00:52:03 +00:00
|
|
|
onClose={closeModal}
|
2024-03-19 17:12:32 +00:00
|
|
|
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}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|