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

155 lines
4.3 KiB
TypeScript
Raw Normal View History

// Copyright 2019-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import { connect } from 'react-redux';
import { get } from 'lodash';
import { mapDispatchToProps } from '../actions';
2021-10-05 16:47:06 +00:00
import {
CompositionArea,
Props as ComponentPropsType,
} from '../../components/CompositionArea';
import { StateType } from '../reducer';
import { isConversationSMSOnly } from '../../util/isConversationSMSOnly';
import { dropNull } from '../../util/dropNull';
2021-04-27 22:35:35 +00:00
import { selectRecentEmojis } from '../selectors/emojis';
2021-06-25 16:08:16 +00:00
import { getIntl, getUserConversationId } from '../selectors/user';
import { getEmojiSkinTone } from '../selectors/items';
import {
getConversationSelector,
2021-07-20 20:18:35 +00:00
getGroupAdminsSelector,
isMissingRequiredProfileSharing,
} from '../selectors/conversations';
2021-06-25 16:08:16 +00:00
import { getPropsForQuote } from '../selectors/message';
import {
getBlessedStickerPacks,
getInstalledStickerPacks,
getKnownStickerPacks,
getReceivedStickerPacks,
getRecentlyInstalledStickerPack,
getRecentStickers,
} from '../selectors/stickers';
2019-08-07 00:40:25 +00:00
type ExternalProps = {
id: string;
2021-10-05 16:47:06 +00:00
handleClickQuotedMessage: (id: string) => unknown;
2019-08-07 00:40:25 +00:00
};
2021-10-05 16:47:06 +00:00
export type CompositionAreaPropsType = ExternalProps & ComponentPropsType;
2019-08-07 00:40:25 +00:00
const mapStateToProps = (state: StateType, props: ExternalProps) => {
2021-10-05 16:47:06 +00:00
const { id, handleClickQuotedMessage } = props;
2019-08-07 00:40:25 +00:00
2021-06-25 16:08:16 +00:00
const conversationSelector = getConversationSelector(state);
const conversation = conversationSelector(id);
2019-08-07 00:40:25 +00:00
if (!conversation) {
throw new Error(`Conversation id ${id} not found!`);
}
2021-07-20 20:18:35 +00:00
const {
announcementsOnly,
areWeAdmin,
draftText,
draftBodyRanges,
} = conversation;
2019-08-07 00:40:25 +00:00
const receivedPacks = getReceivedStickerPacks(state);
const installedPacks = getInstalledStickerPacks(state);
const blessedPacks = getBlessedStickerPacks(state);
const knownPacks = getKnownStickerPacks(state);
const installedPack = getRecentlyInstalledStickerPack(state);
const recentStickers = getRecentStickers(state);
const showIntroduction = get(
state.items,
['showStickersIntroduction'],
false
);
const showPickerHint = Boolean(
get(state.items, ['showStickerPickerHint'], false) &&
receivedPacks.length > 0
);
2021-06-25 16:08:16 +00:00
const {
attachments: draftAttachments,
linkPreviewLoading,
linkPreviewResult,
quotedMessage,
shouldSendHighQualityAttachments,
} = state.composer;
const recentEmojis = selectRecentEmojis(state);
return {
// Base
2021-09-24 20:02:30 +00:00
conversationId: id,
i18n: getIntl(state),
2021-09-29 20:23:06 +00:00
// AudioCapture
errorDialogAudioRecorderType:
state.audioRecorder.errorDialogAudioRecorderType,
isRecording: state.audioRecorder.isRecording,
2021-06-25 16:08:16 +00:00
// AttachmentsList
draftAttachments,
// MediaQualitySelector
shouldSendHighQualityAttachments,
// StagedLinkPreview
linkPreviewLoading,
linkPreviewResult,
// Quote
quotedMessageProps: quotedMessage
2021-08-11 23:06:20 +00:00
? getPropsForQuote(quotedMessage, {
2021-06-25 16:08:16 +00:00
conversationSelector,
2021-08-11 23:06:20 +00:00
ourConversationId: getUserConversationId(state),
})
2021-06-25 16:08:16 +00:00
: undefined,
2021-08-30 21:32:56 +00:00
onClickQuotedMessage: () => {
const messageId = quotedMessage?.quote?.messageId;
if (messageId) {
2021-10-05 16:47:06 +00:00
handleClickQuotedMessage(messageId);
2021-08-30 21:32:56 +00:00
}
},
// Emojis
recentEmojis,
skinTone: getEmojiSkinTone(state),
// Stickers
receivedPacks,
installedPack,
blessedPacks,
knownPacks,
installedPacks,
recentStickers,
showIntroduction,
showPickerHint,
2020-05-27 21:37:06 +00:00
// Message Requests
...conversation,
2020-05-27 21:37:06 +00:00
conversationType: conversation.type,
isSMSOnly: Boolean(isConversationSMSOnly(conversation)),
isFetchingUUID: conversation.isFetchingUUID,
isMissingMandatoryProfileSharing: isMissingRequiredProfileSharing(
conversation
),
2021-07-20 20:18:35 +00:00
// Groups
announcementsOnly,
areWeAdmin,
groupAdmins: getGroupAdminsSelector(state)(conversation.id),
draftText: dropNull(draftText),
draftBodyRanges,
};
};
const dispatchPropsMap = {
...mapDispatchToProps,
onSetSkinTone: (tone: number) => mapDispatchToProps.putItem('skinTone', tone),
clearShowIntroduction: () =>
mapDispatchToProps.removeItem('showStickersIntroduction'),
clearShowPickerHint: () =>
mapDispatchToProps.removeItem('showStickerPickerHint'),
onPickEmoji: mapDispatchToProps.onUseEmoji,
};
const smart = connect(mapStateToProps, dispatchPropsMap);
export const SmartCompositionArea = smart(CompositionArea);