import React from 'react'; import classNames from 'classnames'; import { isEmpty } from 'lodash'; import { ContextMenu, ContextMenuTrigger, MenuItem } from 'react-contextmenu'; import { Portal } from 'react-portal'; import { Avatar } from './Avatar'; import { MessageBody } from './conversation/MessageBody'; import { Timestamp } from './conversation/Timestamp'; import { ContactName } from './conversation/ContactName'; import { TypingAnimation } from './conversation/TypingAnimation'; import { Colors, LocalizerType } from '../types/Util'; export type PropsData = { id: string; phoneNumber: string; color?: string; profileName?: string; name?: string; type: 'group' | 'direct'; avatarPath?: string; isMe: boolean; isPublic?: boolean; isClosable?: boolean; lastUpdated: number; unreadCount: number; mentionedUs: boolean; isSelected: boolean; isTyping: boolean; lastMessage?: { status: 'sending' | 'sent' | 'delivered' | 'read' | 'error'; text: string; isRss: boolean; }; showFriendRequestIndicator?: boolean; isBlocked?: boolean; isOnline?: boolean; hasNickname?: boolean; isFriendItem?: boolean; }; type PropsHousekeeping = { i18n: LocalizerType; style?: Object; onClick?: (id: string) => void; onDeleteMessages?: () => void; onDeleteContact?: () => void; onBlockContact?: () => void; onChangeNickname?: () => void; onClearNickname?: () => void; onCopyPublicKey?: () => void; onUnblockContact?: () => void; }; type Props = PropsData & PropsHousekeeping; export class ConversationListItem extends React.PureComponent { public renderAvatar() { const { avatarPath, color, type, i18n, isMe, name, phoneNumber, profileName, isOnline, } = this.props; const borderColor = isOnline ? Colors.ONLINE : Colors.OFFLINE; return (
{this.renderUnread()}
); } public renderUnread() { const { unreadCount, mentionedUs } = this.props; if (unreadCount > 0) { const atSymbol = mentionedUs ?

@

: null; return (

{unreadCount}

{atSymbol}
); } return null; } public renderHeader() { const { unreadCount, i18n, isMe, lastUpdated, name, phoneNumber, profileName, isFriendItem, } = this.props; return (
0 ? 'module-conversation-list-item__header__name--with-unread' : null )} > {isMe ? ( i18n('noteToSelf') ) : ( )}
{!isFriendItem && (
0 ? 'module-conversation-list-item__header__date--has-unread' : null )} >
)}
); } public renderContextMenu(triggerId: string) { const { i18n, isBlocked, isMe, isClosable, isPublic, hasNickname, onDeleteContact, onDeleteMessages, onBlockContact, onChangeNickname, onClearNickname, onCopyPublicKey, onUnblockContact, } = this.props; const blockTitle = isBlocked ? i18n('unblockUser') : i18n('blockUser'); const blockHandler = isBlocked ? onUnblockContact : onBlockContact; return ( {!isPublic && !isMe ? ( {blockTitle} ) : null} {!isPublic && !isMe ? ( {i18n('changeNickname')} ) : null} {!isPublic && !isMe && hasNickname ? ( {i18n('clearNickname')} ) : null} {!isPublic ? ( {i18n('copyPublicKey')} ) : null} {i18n('deleteMessages')} {!isMe && isClosable ? ( !isPublic ? ( {i18n('deleteContact')} ) : ( {i18n('deletePublicChannel')} ) ) : null} ); } public renderMessage() { const { lastMessage, isTyping, unreadCount, i18n, isFriendItem, } = this.props; if (isFriendItem) { return null; } if (!lastMessage && !isTyping) { return null; } let text = lastMessage && lastMessage.text ? lastMessage.text : ''; // if coming from Rss feed if (lastMessage && lastMessage.isRss) { // strip any HTML text = text.replace(/<[^>]*>?/gm, ''); } if (isEmpty(text)) { return null; } return (
0 ? 'module-conversation-list-item__message__text--has-unread' : null )} > {isTyping ? ( ) : ( )}
{lastMessage && lastMessage.status ? (
) : null}
); } public render() { const { phoneNumber, unreadCount, onClick, id, isSelected, showFriendRequestIndicator, isBlocked, style, mentionedUs, } = this.props; const triggerId = `${phoneNumber}-ctxmenu-${Date.now()}`; return (
{ if (onClick) { onClick(id); } }} style={style} className={classNames( 'module-conversation-list-item', unreadCount > 0 ? 'module-conversation-list-item--has-unread' : null, unreadCount > 0 && mentionedUs ? 'module-conversation-list-item--mentioned-us' : null, isSelected ? 'module-conversation-list-item--is-selected' : null, showFriendRequestIndicator ? 'module-conversation-list-item--has-friend-request' : null, isBlocked ? 'module-conversation-list-item--is-blocked' : null )} > {this.renderAvatar()}
{this.renderHeader()} {this.renderMessage()}
{this.renderContextMenu(triggerId)}
); } }