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

125 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-01-26 23:05:26 +00:00
// Copyright 2019-2022 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';
2021-11-17 21:11:46 +00:00
import { getPreferredBadgeSelector } from '../selectors/badges';
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';
import {
areMessagesInSameGroup,
shouldCurrentMessageHideMetadata,
UnreadIndicatorPlacement,
} from '../../util/timelineUtil';
2020-09-09 02:25:05 +00:00
import { SmartContactName } from './ContactName';
2021-06-01 20:45:43 +00:00
import { SmartUniversalTimerNotification } from './UniversalTimerNotification';
import { isSameDay } from '../../util/timestamp';
2020-09-09 02:25:05 +00:00
type ExternalProps = {
containerElementRef: RefObject<HTMLElement>;
conversationId: string;
2022-01-26 23:05:26 +00:00
isOldestTimelineItem: boolean;
messageId: string;
nextMessageId: undefined | string;
previousMessageId: undefined | string;
unreadIndicatorPlacement: undefined | UnreadIndicatorPlacement;
};
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,
2022-01-26 23:05:26 +00:00
isOldestTimelineItem,
messageId,
nextMessageId,
previousMessageId,
unreadIndicatorPlacement,
} = 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);
const isNextItemCallingNotification = nextItem?.type === 'callHistory';
const shouldCollapseAbove = areMessagesInSameGroup(
previousItem,
unreadIndicatorPlacement === UnreadIndicatorPlacement.JustAbove,
item
);
const shouldCollapseBelow = areMessagesInSameGroup(
item,
unreadIndicatorPlacement === UnreadIndicatorPlacement.JustBelow,
nextItem
);
const shouldHideMetadata = shouldCurrentMessageHideMetadata(
shouldCollapseBelow,
item,
nextItem
);
const shouldRenderDateHeader =
isOldestTimelineItem ||
Boolean(
item &&
previousItem &&
// This comparison avoids strange header behavior for out-of-order messages.
item.timestamp > previousItem.timestamp &&
!isSameDay(previousItem.timestamp, item.timestamp)
);
return {
item,
id: messageId,
containerElementRef,
2019-11-07 21:36:16 +00:00
conversationId,
2022-05-11 20:59:58 +00:00
conversationColor: conversation.conversationColor,
customColor: conversation.customColor,
2021-11-17 21:11:46 +00:00
getPreferredBadge: getPreferredBadgeSelector(state),
isNextItemCallingNotification,
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,
shouldCollapseAbove,
shouldCollapseBelow,
shouldHideMetadata,
shouldRenderDateHeader,
i18n: getIntl(state),
interactionMode: getInteractionMode(state),
theme: getTheme(state),
};
};
const smart = connect(mapStateToProps, mapDispatchToProps);
export const SmartTimelineItem = smart(TimelineItem);