signal-desktop/ts/components/conversation/TimelineItem.tsx

360 lines
11 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
2022-01-26 23:05:26 +00:00
import type { ReactChild, RefObject } from 'react';
import React from 'react';
2021-08-11 16:23:21 +00:00
import { omit } from 'lodash';
2022-01-26 23:05:26 +00:00
import moment from 'moment';
2021-08-11 16:23:21 +00:00
import type { LocalizerType, ThemeType } from '../../types/Util';
import type { InteractionModeType } from '../../state/ducks/conversations';
2022-01-26 23:05:26 +00:00
import { TimelineDateHeader } from './TimelineDateHeader';
import type {
Props as AllMessageProps,
PropsActions as MessageActionsType,
PropsData as MessageProps,
} from './Message';
import { Message } from './Message';
import type { PropsActionsType as CallingNotificationActionsType } from './CallingNotification';
import { CallingNotification } from './CallingNotification';
import type { PropsActionsType as PropsChatSessionRefreshedActionsType } from './ChatSessionRefreshedNotification';
import { ChatSessionRefreshedNotification } from './ChatSessionRefreshedNotification';
import type {
PropsActionsType as DeliveryIssueActionProps,
2021-05-28 19:11:19 +00:00
PropsDataType as DeliveryIssueProps,
} from './DeliveryIssueNotification';
import { DeliveryIssueNotification } from './DeliveryIssueNotification';
import { LinkNotification } from './LinkNotification';
import type { PropsData as ChangeNumberNotificationProps } from './ChangeNumberNotification';
import { ChangeNumberNotification } from './ChangeNumberNotification';
import type { CallingNotificationType } from '../../util/callingNotification';
2019-11-07 21:36:16 +00:00
import { InlineNotificationWrapper } from './InlineNotificationWrapper';
import type {
PropsActions as UnsupportedMessageActionsType,
PropsData as UnsupportedMessageProps,
} from './UnsupportedMessage';
import { UnsupportedMessage } from './UnsupportedMessage';
import type { PropsData as TimerNotificationProps } from './TimerNotification';
import { TimerNotification } from './TimerNotification';
import type {
PropsActions as SafetyNumberActionsType,
PropsData as SafetyNumberNotificationProps,
} from './SafetyNumberNotification';
import { SafetyNumberNotification } from './SafetyNumberNotification';
import type { PropsData as VerificationNotificationProps } from './VerificationNotification';
import { VerificationNotification } from './VerificationNotification';
import type { PropsData as GroupNotificationProps } from './GroupNotification';
import { GroupNotification } from './GroupNotification';
import type { PropsDataType as GroupV2ChangeProps } from './GroupV2Change';
import { GroupV2Change } from './GroupV2Change';
import type { PropsDataType as GroupV1MigrationProps } from './GroupV1Migration';
import { GroupV1Migration } from './GroupV1Migration';
import type { SmartContactRendererType } from '../../groupChange';
import { ResetSessionNotification } from './ResetSessionNotification';
import type { PropsType as ProfileChangeNotificationPropsType } from './ProfileChangeNotification';
import { ProfileChangeNotification } from './ProfileChangeNotification';
import * as log from '../../logging/log';
import type { FullJSXType } from '../Intl';
2020-06-04 18:16:19 +00:00
type CallHistoryType = {
type: 'callHistory';
data: CallingNotificationType;
2020-06-04 18:16:19 +00:00
};
2021-02-18 16:40:26 +00:00
type ChatSessionRefreshedType = {
type: 'chatSessionRefreshed';
data: null;
};
2021-05-28 19:11:19 +00:00
type DeliveryIssueType = {
type: 'deliveryIssue';
data: DeliveryIssueProps;
};
type LinkNotificationType = {
type: 'linkNotification';
data: null;
};
type MessageType = {
type: 'message';
data: Omit<MessageProps, 'renderingContext'>;
};
type UnsupportedMessageType = {
type: 'unsupportedMessage';
data: UnsupportedMessageProps;
};
type TimerNotificationType = {
type: 'timerNotification';
data: TimerNotificationProps;
};
2021-06-01 20:45:43 +00:00
type UniversalTimerNotificationType = {
type: 'universalTimerNotification';
data: null;
};
2021-08-05 23:34:49 +00:00
type ChangeNumberNotificationType = {
type: 'changeNumberNotification';
data: ChangeNumberNotificationProps;
};
type SafetyNumberNotificationType = {
type: 'safetyNumberNotification';
data: SafetyNumberNotificationProps;
};
type VerificationNotificationType = {
type: 'verificationNotification';
data: VerificationNotificationProps;
};
type GroupNotificationType = {
type: 'groupNotification';
data: GroupNotificationProps;
};
2020-09-09 02:25:05 +00:00
type GroupV2ChangeType = {
type: 'groupV2Change';
data: GroupV2ChangeProps;
};
2020-11-20 17:30:45 +00:00
type GroupV1MigrationType = {
type: 'groupV1Migration';
data: GroupV1MigrationProps;
};
type ResetSessionNotificationType = {
type: 'resetSessionNotification';
data: null;
};
type ProfileChangeNotificationType = {
type: 'profileChange';
data: ProfileChangeNotificationPropsType;
};
2022-01-26 23:05:26 +00:00
export type TimelineItemType = (
2020-06-04 18:16:19 +00:00
| CallHistoryType
2021-02-18 16:40:26 +00:00
| ChatSessionRefreshedType
2021-05-28 19:11:19 +00:00
| DeliveryIssueType
| GroupNotificationType
2020-11-20 17:30:45 +00:00
| GroupV1MigrationType
2020-09-09 02:25:05 +00:00
| GroupV2ChangeType
| LinkNotificationType
| MessageType
| ProfileChangeNotificationType
| ResetSessionNotificationType
| SafetyNumberNotificationType
| TimerNotificationType
2021-06-01 20:45:43 +00:00
| UniversalTimerNotificationType
2021-08-05 23:34:49 +00:00
| ChangeNumberNotificationType
| UnsupportedMessageType
2022-01-26 23:05:26 +00:00
| VerificationNotificationType
) & { timestamp: number };
2019-11-07 21:36:16 +00:00
type PropsLocalType = {
containerElementRef: RefObject<HTMLElement>;
2019-11-07 21:36:16 +00:00
conversationId: string;
item?: TimelineItemType;
2019-11-07 21:36:16 +00:00
id: string;
isSelected: boolean;
selectMessage: (messageId: string, conversationId: string) => unknown;
renderContact: SmartContactRendererType<FullJSXType>;
2021-06-01 20:45:43 +00:00
renderUniversalTimerNotification: () => JSX.Element;
i18n: LocalizerType;
interactionMode: InteractionModeType;
2022-01-26 23:05:26 +00:00
isOldestTimelineItem: boolean;
theme: ThemeType;
previousItem: undefined | TimelineItemType;
nextItem: undefined | TimelineItemType;
};
2019-11-07 21:36:16 +00:00
type PropsActionsType = MessageActionsType &
CallingNotificationActionsType &
DeliveryIssueActionProps &
2021-02-18 16:40:26 +00:00
PropsChatSessionRefreshedActionsType &
UnsupportedMessageActionsType &
SafetyNumberActionsType;
export type PropsType = PropsLocalType &
PropsActionsType &
Pick<
AllMessageProps,
| 'containerWidthBreakpoint'
2021-11-17 21:11:46 +00:00
| 'getPreferredBadge'
| 'renderEmojiPicker'
| 'renderAudioAttachment'
| 'renderReactionPicker'
>;
2019-11-07 21:36:16 +00:00
export class TimelineItem extends React.PureComponent<PropsType> {
public override render(): JSX.Element | null {
2019-11-07 21:36:16 +00:00
const {
containerElementRef,
2019-11-07 21:36:16 +00:00
conversationId,
2021-11-17 21:11:46 +00:00
getPreferredBadge,
2019-11-07 21:36:16 +00:00
id,
2022-01-26 23:05:26 +00:00
isOldestTimelineItem,
2019-11-07 21:36:16 +00:00
isSelected,
item,
i18n,
theme,
messageSizeChanged,
nextItem,
2022-01-26 23:05:26 +00:00
previousItem,
2020-09-09 02:25:05 +00:00
renderContact,
2021-06-01 20:45:43 +00:00
renderUniversalTimerNotification,
returnToActiveCall,
2019-11-07 21:36:16 +00:00
selectMessage,
startCallingLobby,
2019-11-07 21:36:16 +00:00
} = this.props;
if (!item) {
log.warn(`TimelineItem: item ${id} provided was falsey`);
return null;
}
2022-01-26 23:05:26 +00:00
let itemContents: ReactChild;
if (item.type === 'message') {
2022-01-26 23:05:26 +00:00
itemContents = (
<Message
2021-08-11 16:23:21 +00:00
{...omit(this.props, ['item'])}
{...item.data}
containerElementRef={containerElementRef}
2021-11-17 21:11:46 +00:00
getPreferredBadge={getPreferredBadge}
i18n={i18n}
theme={theme}
renderingContext="conversation/TimelineItem"
/>
);
2022-01-26 23:05:26 +00:00
} else {
let notification;
2019-11-07 21:36:16 +00:00
2022-01-26 23:05:26 +00:00
if (item.type === 'unsupportedMessage') {
notification = (
<UnsupportedMessage {...this.props} {...item.data} i18n={i18n} />
);
} else if (item.type === 'callHistory') {
notification = (
<CallingNotification
conversationId={conversationId}
i18n={i18n}
messageId={id}
messageSizeChanged={messageSizeChanged}
nextItem={nextItem}
returnToActiveCall={returnToActiveCall}
startCallingLobby={startCallingLobby}
{...item.data}
/>
);
} else if (item.type === 'chatSessionRefreshed') {
notification = (
<ChatSessionRefreshedNotification
{...this.props}
{...item.data}
i18n={i18n}
/>
);
} else if (item.type === 'deliveryIssue') {
notification = (
<DeliveryIssueNotification
{...item.data}
{...this.props}
i18n={i18n}
/>
);
} else if (item.type === 'linkNotification') {
notification = <LinkNotification i18n={i18n} />;
} else if (item.type === 'timerNotification') {
notification = (
<TimerNotification {...this.props} {...item.data} i18n={i18n} />
);
} else if (item.type === 'universalTimerNotification') {
notification = renderUniversalTimerNotification();
} else if (item.type === 'changeNumberNotification') {
notification = (
<ChangeNumberNotification
{...this.props}
{...item.data}
i18n={i18n}
/>
);
} else if (item.type === 'safetyNumberNotification') {
notification = (
<SafetyNumberNotification
{...this.props}
{...item.data}
i18n={i18n}
/>
);
} else if (item.type === 'verificationNotification') {
notification = (
<VerificationNotification
{...this.props}
{...item.data}
i18n={i18n}
/>
);
} else if (item.type === 'groupNotification') {
notification = (
<GroupNotification {...this.props} {...item.data} i18n={i18n} />
);
} else if (item.type === 'groupV2Change') {
notification = (
<GroupV2Change
renderContact={renderContact}
{...item.data}
i18n={i18n}
/>
);
} else if (item.type === 'groupV1Migration') {
notification = (
<GroupV1Migration {...this.props} {...item.data} i18n={i18n} />
);
} else if (item.type === 'resetSessionNotification') {
notification = (
<ResetSessionNotification
{...this.props}
{...item.data}
i18n={i18n}
/>
);
} else if (item.type === 'profileChange') {
notification = (
<ProfileChangeNotification
{...this.props}
{...item.data}
i18n={i18n}
/>
);
} else {
// Weird, yes, but the idea is to get a compile error when we aren't comprehensive
// with our if/else checks above, but also log out the type we don't understand
// if we encounter it at runtime.
const unknownItem: never = item;
const asItem = unknownItem as TimelineItemType;
throw new Error(`TimelineItem: Unknown type: ${asItem.type}`);
}
2019-11-07 21:36:16 +00:00
2022-01-26 23:05:26 +00:00
itemContents = (
<InlineNotificationWrapper
id={id}
conversationId={conversationId}
2022-01-26 23:05:26 +00:00
isSelected={isSelected}
selectMessage={selectMessage}
>
{notification}
</InlineNotificationWrapper>
2021-08-05 23:34:49 +00:00
);
2022-01-26 23:05:26 +00:00
}
const shouldRenderDateHeader =
isOldestTimelineItem ||
Boolean(
previousItem &&
// This comparison avoids strange header behavior for out-of-order messages.
item.timestamp > previousItem.timestamp &&
!moment(previousItem.timestamp).isSame(item.timestamp, 'day')
);
2022-01-26 23:05:26 +00:00
if (shouldRenderDateHeader) {
return (
<>
<TimelineDateHeader i18n={i18n} timestamp={item.timestamp} />
{itemContents}
</>
);
}
2022-01-26 23:05:26 +00:00
return itemContents;
}
}