signal-desktop/ts/components/conversationList/ConversationListItem.tsx

231 lines
6.2 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { FunctionComponent, ReactNode } from 'react';
import React, { useCallback } from 'react';
import classNames from 'classnames';
import {
BaseConversationListItem,
2021-10-14 15:48:48 +00:00
HEADER_NAME_CLASS_NAME,
HEADER_CONTACT_NAME_CLASS_NAME,
MESSAGE_TEXT_CLASS_NAME,
} from './BaseConversationListItem';
import { MessageBody } from '../conversation/MessageBody';
import { ContactName } from '../conversation/ContactName';
import { TypingAnimation } from '../conversation/TypingAnimation';
2021-11-02 23:01:13 +00:00
import type { LocalizerType, ThemeType } from '../../types/Util';
import type { ConversationType } from '../../state/ducks/conversations';
2021-11-02 23:01:13 +00:00
import type { BadgeType } from '../../badges/types';
2022-11-09 02:38:19 +00:00
import { isSignalConversation } from '../../util/isSignalConversation';
import { RenderLocation } from '../conversation/MessageTextRenderer';
const EMPTY_OBJECT = Object.freeze(Object.create(null));
const MESSAGE_STATUS_ICON_CLASS_NAME = `${MESSAGE_TEXT_CLASS_NAME}__status-icon`;
export const MessageStatuses = [
'sending',
'sent',
'delivered',
'read',
'paused',
'error',
'partial-sent',
] as const;
export type MessageStatusType = typeof MessageStatuses[number];
2021-05-07 22:21:10 +00:00
export type PropsData = Pick<
ConversationType,
| 'acceptedMessageRequest'
| 'avatarPath'
2021-11-02 23:01:13 +00:00
| 'badges'
2021-05-07 22:21:10 +00:00
| 'color'
| 'draftPreview'
2023-01-13 00:24:59 +00:00
| 'groupId'
2021-05-07 22:21:10 +00:00
| 'id'
| 'isMe'
2022-01-31 22:01:34 +00:00
// NOTE: Passed for CI, not used for rendering
2021-05-07 22:21:10 +00:00
| 'isPinned'
| 'isSelected'
| 'lastMessage'
| 'lastUpdated'
| 'markedUnread'
| 'muteExpiresAt'
| 'phoneNumber'
| 'profileName'
2023-04-05 20:48:00 +00:00
| 'removalStage'
2021-05-07 22:21:10 +00:00
| 'sharedGroupNames'
| 'shouldShowDraft'
| 'title'
| 'type'
2023-09-27 21:23:52 +00:00
| 'typingContactIdTimestamps'
2021-05-07 22:21:10 +00:00
| 'unblurredAvatarPath'
| 'unreadCount'
| 'unreadMentionsCount'
2023-08-16 20:54:39 +00:00
| 'serviceId'
2021-11-02 23:01:13 +00:00
> & {
badge?: BadgeType;
};
type PropsHousekeeping = {
2023-04-21 21:23:30 +00:00
buttonAriaLabel?: string;
i18n: LocalizerType;
onClick: (id: string) => void;
2021-11-02 23:01:13 +00:00
theme: ThemeType;
};
export type Props = PropsData & PropsHousekeeping;
export const ConversationListItem: FunctionComponent<Props> = React.memo(
2021-08-11 19:29:07 +00:00
function ConversationListItem({
acceptedMessageRequest,
avatarPath,
2021-11-02 23:01:13 +00:00
badge,
2023-04-21 21:23:30 +00:00
buttonAriaLabel,
color,
draftPreview,
2023-01-13 00:24:59 +00:00
groupId,
i18n,
id,
isMe,
isSelected,
lastMessage,
lastUpdated,
markedUnread,
muteExpiresAt,
onClick,
phoneNumber,
profileName,
2023-04-05 20:48:00 +00:00
removalStage,
sharedGroupNames,
shouldShowDraft,
2021-11-02 23:01:13 +00:00
theme,
title,
type,
2023-09-27 21:23:52 +00:00
typingContactIdTimestamps,
unblurredAvatarPath,
unreadCount,
unreadMentionsCount,
2023-08-16 20:54:39 +00:00
serviceId,
2021-08-11 19:29:07 +00:00
}) {
2021-10-14 15:48:48 +00:00
const isMuted = Boolean(muteExpiresAt && Date.now() < muteExpiresAt);
2023-09-27 21:23:52 +00:00
const isSomeoneTyping =
Object.keys(typingContactIdTimestamps ?? {}).length > 0;
2021-10-14 15:48:48 +00:00
const headerName = (
<>
{isMe ? (
2023-03-02 06:57:35 +00:00
<ContactName
module={HEADER_CONTACT_NAME_CLASS_NAME}
isMe={isMe}
2023-03-30 00:03:25 +00:00
title={i18n('icu:noteToSelf')}
2023-03-02 06:57:35 +00:00
/>
2021-10-14 15:48:48 +00:00
) : (
2022-11-09 02:38:19 +00:00
<ContactName
module={HEADER_CONTACT_NAME_CLASS_NAME}
2023-08-16 20:54:39 +00:00
isSignalConversation={isSignalConversation({ id, serviceId })}
2022-11-09 02:38:19 +00:00
title={title}
/>
2021-10-14 15:48:48 +00:00
)}
{isMuted && <div className={`${HEADER_NAME_CLASS_NAME}__mute-icon`} />}
</>
);
let messageText: ReactNode = null;
let messageStatusIcon: ReactNode = null;
2023-04-05 20:48:00 +00:00
if (!acceptedMessageRequest && removalStage !== 'justNotification') {
messageText = (
<span className={`${MESSAGE_TEXT_CLASS_NAME}__message-request`}>
2023-03-30 00:03:25 +00:00
{i18n('icu:ConversationListItem--message-request')}
</span>
);
} else if (isSomeoneTyping) {
messageText = <TypingAnimation i18n={i18n} />;
} else if (shouldShowDraft && draftPreview) {
messageText = (
<>
<span className={`${MESSAGE_TEXT_CLASS_NAME}__draft-prefix`}>
2023-03-30 00:03:25 +00:00
{i18n('icu:ConversationListItem--draft-prefix')}
</span>
<MessageBody
bodyRanges={draftPreview.bodyRanges}
disableJumbomoji
disableLinks
i18n={i18n}
isSpoilerExpanded={{}}
prefix={draftPreview.prefix}
renderLocation={RenderLocation.ConversationList}
text={draftPreview.text}
/>
</>
);
} else if (lastMessage?.deletedForEveryone) {
messageText = (
<span className={`${MESSAGE_TEXT_CLASS_NAME}__deleted-for-everyone`}>
2023-03-30 00:03:25 +00:00
{i18n('icu:message--deletedForEveryone')}
</span>
);
} else if (lastMessage) {
messageText = (
<MessageBody
author={type === 'group' ? lastMessage.author : undefined}
bodyRanges={lastMessage.bodyRanges}
disableJumbomoji
disableLinks
i18n={i18n}
isSpoilerExpanded={EMPTY_OBJECT}
prefix={lastMessage.prefix}
renderLocation={RenderLocation.ConversationList}
text={lastMessage.text}
/>
);
if (lastMessage.status) {
messageStatusIcon = (
<div
className={classNames(
MESSAGE_STATUS_ICON_CLASS_NAME,
`${MESSAGE_STATUS_ICON_CLASS_NAME}--${lastMessage.status}`
)}
/>
);
}
}
const onClickItem = useCallback(() => onClick(id), [onClick, id]);
return (
<BaseConversationListItem
acceptedMessageRequest={acceptedMessageRequest}
avatarPath={avatarPath}
2021-11-02 23:01:13 +00:00
badge={badge}
2023-04-21 21:23:30 +00:00
buttonAriaLabel={buttonAriaLabel}
color={color}
conversationType={type}
2023-01-13 00:24:59 +00:00
groupId={groupId}
headerDate={lastUpdated}
headerName={headerName}
i18n={i18n}
id={id}
isMe={isMe}
isSelected={Boolean(isSelected)}
markedUnread={markedUnread}
messageStatusIcon={messageStatusIcon}
messageText={messageText}
messageTextIsAlwaysFullSize
onClick={onClickItem}
phoneNumber={phoneNumber}
profileName={profileName}
sharedGroupNames={sharedGroupNames}
2021-11-02 23:01:13 +00:00
theme={theme}
title={title}
unreadCount={unreadCount}
unreadMentionsCount={unreadMentionsCount}
unblurredAvatarPath={unblurredAvatarPath}
2023-08-16 20:54:39 +00:00
serviceId={serviceId}
/>
);
}
);