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

175 lines
5.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
2023-03-20 22:23:53 +00:00
import React, { useState } from 'react';
2022-07-01 00:52:03 +00:00
import { useSelector } from 'react-redux';
import type {
ForwardMessagePropsType,
ForwardMessagesPropsType,
} 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';
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';
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';
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 { hydrateRanges } from '../../types/BodyRange';
import { isDownloaded } from '../../types/Attachment';
import { __DEPRECATED$getMessageById } from '../../messages/getMessageById';
import { strictAssert } from '../../util/assert';
import type {
ForwardMessageData,
MessageForwardDraft,
} from '../../types/ForwardDraft';
function toMessageForwardDraft(
props: ForwardMessagePropsType,
getConversation: GetConversationByIdType
): MessageForwardDraft {
return {
attachments: props.attachments ?? [],
bodyRanges: hydrateRanges(props.bodyRanges, getConversation),
hasContact: Boolean(props.contact),
isSticker: Boolean(props.isSticker),
messageBody: props.text,
originalMessageId: props.id,
previews: props.previews ?? [],
};
}
2021-04-27 22:35:35 +00:00
2023-03-20 22:23:53 +00:00
export function SmartForwardMessagesModal(): JSX.Element | null {
const forwardMessagesProps = useSelector<
2022-07-01 00:52:03 +00:00
StateType,
2023-03-20 22:23:53 +00:00
ForwardMessagesPropsType | undefined
>(state => state.globalModals.forwardMessagesProps);
if (forwardMessagesProps == null) {
return null;
}
if (
!forwardMessagesProps.messages.every(message => {
return message.attachments?.every(isDownloaded) ?? true;
})
) {
return null;
}
return (
<SmartForwardMessagesModalInner
forwardMessagesProps={forwardMessagesProps}
/>
);
}
function SmartForwardMessagesModalInner({
forwardMessagesProps,
}: {
forwardMessagesProps: ForwardMessagesPropsType;
}): JSX.Element | null {
2022-07-01 00:52:03 +00:00
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();
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>>(
() => {
return forwardMessagesProps.messages.map((props): MessageForwardDraft => {
return toMessageForwardDraft(props, getConversation);
});
2023-03-20 22:23:53 +00:00
}
);
if (!drafts.length) {
2022-07-01 00:52:03 +00:00
return null;
}
2021-04-27 22:35:35 +00:00
2022-07-01 00:52:03 +00:00
function closeModal() {
resetLinkPreview();
2023-03-20 22:23:53 +00:00
toggleForwardMessagesModal();
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}
2023-03-20 22:23:53 +00:00
doForwardMessages={async (conversationIds, finalDrafts) => {
2022-07-01 00:52:03 +00:00
try {
2023-03-20 22:23:53 +00:00
const messages = await Promise.all(
finalDrafts.map(async (draft): Promise<ForwardMessageData> => {
const message = await __DEPRECATED$getMessageById(
draft.originalMessageId
);
strictAssert(message, 'no message found');
2023-03-20 22:23:53 +00:00
return {
draft,
originalMessage: message.attributes,
};
})
);
2022-07-01 00:52:03 +00:00
2023-03-20 22:23:53 +00:00
const didForwardSuccessfully = await maybeForwardMessages(
messages,
conversationIds
2022-07-01 00:52:03 +00:00
);
if (didForwardSuccessfully) {
closeModal();
2023-03-20 22:23:53 +00:00
forwardMessagesProps?.onForward?.();
2022-07-01 00:52:03 +00:00
}
} catch (err) {
log.warn('doForwardMessage', Errors.toLogFormat(err));
2022-07-01 00:52:03 +00:00
}
}}
2023-03-20 22:23:53 +00:00
linkPreviewForSource={linkPreviewForSource}
2022-07-01 00:52:03 +00:00
getPreferredBadge={getPreferredBadge}
i18n={i18n}
onClose={closeModal}
2023-03-20 22:23:53 +00:00
onChange={(updatedDrafts, caretLocation) => {
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) {
2022-07-01 00:52:03 +00:00
maybeGrabLinkPreview(
2023-03-20 22:23:53 +00:00
lonelyDraft.messageBody ?? '',
2022-07-01 00:52:03 +00:00
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}
2023-03-20 22:23:53 +00:00
showToast={showToast}
2022-07-01 00:52:03 +00:00
theme={theme}
/>
);
}