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

78 lines
2.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
2020-09-09 02:25:05 +00:00
import React from 'react';
import { connect } from 'react-redux';
import { mapDispatchToProps } from '../actions';
import { StateType } from '../reducer';
import { TimelineItem } from '../../components/conversation/TimelineItem';
import { getIntl, getInteractionMode, getTheme } from '../selectors/user';
2019-11-07 21:36:16 +00:00
import {
2021-05-28 16:15:17 +00:00
getContactNameColorSelector,
getConversationSelector,
2019-11-07 21:36:16 +00:00
getMessageSelector,
getSelectedMessage,
} from '../selectors/conversations';
2020-09-09 02:25:05 +00:00
import { SmartContactName } from './ContactName';
2021-06-01 20:45:43 +00:00
import { SmartUniversalTimerNotification } from './UniversalTimerNotification';
2020-09-09 02:25:05 +00:00
type ExternalProps = {
id: string;
2019-11-07 21:36:16 +00:00
conversationId: string;
};
2020-09-09 02:25:05 +00:00
// Workaround: A react component's required properties are filtering up through connect()
// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/31363
/* eslint-disable @typescript-eslint/no-explicit-any */
2020-09-09 02:25:05 +00:00
const FilteredSmartContactName = SmartContactName as any;
/* eslint-enable @typescript-eslint/no-explicit-any */
2020-09-09 02:25:05 +00:00
function renderContact(conversationId: string): JSX.Element {
return <FilteredSmartContactName conversationId={conversationId} />;
}
2021-06-01 20:45:43 +00:00
function renderUniversalTimerNotification(): JSX.Element {
return <SmartUniversalTimerNotification />;
}
const mapStateToProps = (state: StateType, props: ExternalProps) => {
2019-11-07 21:36:16 +00:00
const { id, conversationId } = props;
const messageSelector = getMessageSelector(state);
const item = messageSelector(id);
2021-05-28 16:15:17 +00:00
if (item?.type === 'message' && item.data.conversationType === 'group') {
const { author } = item.data;
item.data.contactNameColor = getContactNameColorSelector(state)(
conversationId,
author.id
);
}
2019-11-07 21:36:16 +00:00
const selectedMessage = getSelectedMessage(state);
const isSelected = Boolean(selectedMessage && id === selectedMessage.id);
2021-05-28 16:15:17 +00:00
const conversation = getConversationSelector(state)(conversationId);
return {
item,
2019-11-07 21:36:16 +00:00
id,
conversationId,
2021-05-28 16:15:17 +00:00
conversationColor: conversation?.conversationColor,
customColor: conversation?.customColor,
2019-11-07 21:36:16 +00:00
isSelected,
2020-09-09 02:25:05 +00:00
renderContact,
2021-06-01 20:45:43 +00:00
renderUniversalTimerNotification,
i18n: getIntl(state),
interactionMode: getInteractionMode(state),
theme: getTheme(state),
};
};
const smart = connect(mapStateToProps, mapDispatchToProps);
export const SmartTimelineItem = smart(TimelineItem);