signal-desktop/ts/components/ConversationListItem.tsx

233 lines
5.6 KiB
TypeScript
Raw Normal View History

import React from 'react';
import classNames from 'classnames';
import { Avatar } from './Avatar';
import { MessageBody } from './conversation/MessageBody';
import { Timestamp } from './conversation/Timestamp';
import { ContactName } from './conversation/ContactName';
2018-11-14 19:10:32 +00:00
import { TypingAnimation } from './conversation/TypingAnimation';
2019-01-14 21:49:58 +00:00
import { LocalizerType } from '../types/Util';
2019-01-14 21:49:58 +00:00
export type PropsData = {
id: string;
phoneNumber: string;
2019-01-14 21:49:58 +00:00
color?: string;
profileName?: string;
name?: string;
2019-01-14 21:49:58 +00:00
type: 'group' | 'direct';
avatarPath?: string;
2019-01-31 01:45:58 +00:00
isMe: boolean;
lastUpdated: number;
unreadCount: number;
isSelected: boolean;
2019-08-07 00:40:25 +00:00
draftPreview?: string;
shouldShowDraft?: boolean;
typingContact?: Object;
lastMessage?: {
status: 'sending' | 'sent' | 'delivered' | 'read' | 'error';
text: string;
};
2019-01-14 21:49:58 +00:00
};
2019-01-14 21:49:58 +00:00
type PropsHousekeeping = {
i18n: LocalizerType;
style?: Object;
2019-01-14 21:49:58 +00:00
onClick?: (id: string) => void;
};
2019-01-14 21:49:58 +00:00
type Props = PropsData & PropsHousekeeping;
export class ConversationListItem extends React.PureComponent<Props> {
public renderAvatar() {
const {
avatarPath,
color,
2019-01-14 21:49:58 +00:00
type,
i18n,
2019-01-31 01:45:58 +00:00
isMe,
name,
phoneNumber,
profileName,
} = this.props;
return (
<div className="module-conversation-list-item__avatar-container">
<Avatar
avatarPath={avatarPath}
color={color}
2019-01-31 01:45:58 +00:00
noteToSelf={isMe}
2019-01-14 21:49:58 +00:00
conversationType={type}
i18n={i18n}
name={name}
phoneNumber={phoneNumber}
profileName={profileName}
2019-10-04 18:06:17 +00:00
size={52}
/>
{this.renderUnread()}
</div>
);
}
public renderUnread() {
const { unreadCount } = this.props;
if (unreadCount > 0) {
return (
<div className="module-conversation-list-item__unread-count">
{unreadCount}
</div>
);
}
return null;
}
public renderHeader() {
const {
unreadCount,
i18n,
2019-01-31 01:45:58 +00:00
isMe,
lastUpdated,
name,
phoneNumber,
profileName,
} = this.props;
return (
<div className="module-conversation-list-item__header">
<div
className={classNames(
'module-conversation-list-item__header__name',
unreadCount > 0
? 'module-conversation-list-item__header__name--with-unread'
: null
)}
>
2019-01-31 01:45:58 +00:00
{isMe ? (
i18n('noteToSelf')
) : (
<ContactName
phoneNumber={phoneNumber}
name={name}
profileName={profileName}
/>
)}
</div>
<div
className={classNames(
'module-conversation-list-item__header__date',
unreadCount > 0
? 'module-conversation-list-item__header__date--has-unread'
: null
)}
>
<Timestamp
timestamp={lastUpdated}
extended={false}
module="module-conversation-list-item__header__timestamp"
2019-10-04 18:06:17 +00:00
withUnread={unreadCount > 0}
i18n={i18n}
/>
</div>
</div>
);
}
public renderMessage() {
2019-08-07 00:40:25 +00:00
const {
draftPreview,
i18n,
lastMessage,
shouldShowDraft,
typingContact,
unreadCount,
} = this.props;
if (!lastMessage && !typingContact) {
return null;
}
const showingDraft = shouldShowDraft && draftPreview;
// Note: instead of re-using showingDraft here we explode it because
// typescript can't tell that draftPreview is truthy otherwise
2019-08-07 00:40:25 +00:00
const text =
shouldShowDraft && draftPreview
? draftPreview
: lastMessage && lastMessage.text
? lastMessage.text
: '';
return (
<div className="module-conversation-list-item__message">
<div
className={classNames(
'module-conversation-list-item__message__text',
unreadCount > 0
? 'module-conversation-list-item__message__text--has-unread'
: null
)}
>
{typingContact ? (
2018-11-14 19:10:32 +00:00
<TypingAnimation i18n={i18n} />
) : (
<>
{showingDraft ? (
<span className="module-conversation-list-item__message__draft-prefix">
{i18n('ConversationListItem--draft-prefix')}
</span>
) : null}
<MessageBody
text={text.split('\n')[0]}
disableJumbomoji={true}
disableLinks={true}
i18n={i18n}
/>
</>
2018-11-14 19:10:32 +00:00
)}
</div>
{!showingDraft && lastMessage && lastMessage.status ? (
<div
className={classNames(
'module-conversation-list-item__message__status-icon',
`module-conversation-list-item__message__status-icon--${
lastMessage.status
}`
)}
/>
) : null}
</div>
);
}
public render() {
const { unreadCount, onClick, id, isSelected, style } = this.props;
return (
<div
role="button"
2019-01-14 21:49:58 +00:00
onClick={() => {
if (onClick) {
onClick(id);
}
}}
style={style}
className={classNames(
'module-conversation-list-item',
unreadCount > 0 ? 'module-conversation-list-item--has-unread' : null,
isSelected ? 'module-conversation-list-item--is-selected' : null
)}
>
{this.renderAvatar()}
<div className="module-conversation-list-item__content">
{this.renderHeader()}
{this.renderMessage()}
</div>
</div>
);
}
}