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

83 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
import type { RefObject } from 'react';
import React from 'react';
import { connect } from 'react-redux';
import { mapDispatchToProps } from '../actions';
import type { 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
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 = {
containerElementRef: RefObject<HTMLElement>;
conversationId: string;
messageId: string;
nextMessageId: undefined | string;
previousMessageId: undefined | string;
};
2020-09-09 02:25:05 +00:00
function renderContact(conversationId: string): JSX.Element {
return <SmartContactName conversationId={conversationId} />;
2020-09-09 02:25:05 +00:00
}
2021-06-01 20:45:43 +00:00
function renderUniversalTimerNotification(): JSX.Element {
return <SmartUniversalTimerNotification />;
}
const mapStateToProps = (state: StateType, props: ExternalProps) => {
const {
containerElementRef,
conversationId,
messageId,
nextMessageId,
previousMessageId,
} = props;
const messageSelector = getMessageSelector(state);
const item = messageSelector(messageId);
const previousItem = previousMessageId
? messageSelector(previousMessageId)
: undefined;
const nextItem = nextMessageId ? messageSelector(nextMessageId) : undefined;
2019-11-07 21:36:16 +00:00
const selectedMessage = getSelectedMessage(state);
const isSelected = Boolean(
selectedMessage && messageId === selectedMessage.id
);
2019-11-07 21:36:16 +00:00
2021-05-28 16:15:17 +00:00
const conversation = getConversationSelector(state)(conversationId);
return {
item,
previousItem,
nextItem,
id: messageId,
containerElementRef,
2019-11-07 21:36:16 +00:00
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);