import React from 'react'; import classNames from 'classnames'; import { isEmpty } from 'lodash'; import { contextMenu } from 'react-contexify'; 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 { LocalizerType } from '../types/Util'; import { ConversationAvatar, usingClosedConversationDetails, } from './session/usingClosedConversationDetails'; import { ConversationListItemContextMenu, PropsContextConversationItem, } from './session/menu/ConversationListItemContextMenu'; import { createPortal } from 'react-dom'; import { OutgoingMessageStatus } from './conversation/message/OutgoingMessageStatus'; import { DefaultTheme, withTheme } from 'styled-components'; export type ConversationListItemProps = { id: string; phoneNumber: string; color?: string; profileName?: string; name?: string; type: 'group' | 'direct'; avatarPath?: string; isMe: boolean; isPublic?: boolean; isRss?: boolean; isClosable?: boolean; primaryDevice?: string; lastUpdated: number; unreadCount: number; mentionedUs: boolean; isSelected: boolean; isTyping: boolean; lastMessage?: { status: 'sending' | 'sent' | 'delivered' | 'read' | 'error'; text: string; isRss: boolean; }; isBlocked?: boolean; hasNickname?: boolean; isSecondary?: boolean; isGroupInvitation?: boolean; isKickedFromGroup?: boolean; left?: boolean; memberAvatars?: Array; // this is added by usingClosedConversationDetails }; type PropsHousekeeping = { i18n: LocalizerType; style?: Object; onClick?: (id: string) => void; onDeleteMessages?: () => void; onDeleteContact?: () => void; onLeaveGroup?: () => void; onBlockContact?: () => void; onCopyPublicKey?: () => void; onUnblockContact?: () => void; onInviteContacts?: () => void; onClearNickname?: () => void; theme: DefaultTheme; }; type Props = ConversationListItemProps & PropsHousekeeping; const Portal = ({ children }: { children: any }) => { return createPortal( children, document.querySelector('.inbox.index') as Element ); }; class ConversationListItem extends React.PureComponent { public constructor(props: Props) { super(props); } public renderAvatar() { const { avatarPath, i18n, name, phoneNumber, profileName, memberAvatars, } = this.props; const iconSize = 36; const userName = name || profileName || phoneNumber; return (
); } public renderHeader() { const { unreadCount, mentionedUs, i18n, isMe, lastUpdated } = this.props; let atSymbol = null; let unreadCountDiv = null; if (unreadCount > 0) { atSymbol = mentionedUs ?

@

: null; unreadCountDiv = (

{unreadCount}

); } return (
0 ? 'module-conversation-list-item__header__name--with-unread' : null )} > {this.renderUser()}
{unreadCountDiv} {atSymbol} {
0 ? 'module-conversation-list-item__header__date--has-unread' : null )} > { }
}
); } public renderMessage() { const { lastMessage, isTyping, unreadCount, i18n } = this.props; 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, isBlocked, style, mentionedUs, } = this.props; const triggerId = `conversation-item-${phoneNumber}-ctxmenu`; const key = `conversation-item-${phoneNumber}`; return (
{ if (onClick) { onClick(id); } }} onContextMenu={(e: any) => { contextMenu.show({ id: triggerId, event: e, }); }} 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, isBlocked ? 'module-conversation-list-item--is-blocked' : null )} > {this.renderAvatar()}
{this.renderHeader()} {this.renderMessage()}
); } private getMenuProps(triggerId: string): PropsContextConversationItem { return { triggerId, ...this.props, }; } private renderUser() { const { name, phoneNumber, profileName, isMe, i18n } = this.props; const shortenedPubkey = window.shortenPubkey(phoneNumber); const displayedPubkey = profileName ? shortenedPubkey : phoneNumber; const displayName = isMe ? i18n('noteToSelf') : profileName; let shouldShowPubkey = false; if (!name || name.length === 0) { shouldShowPubkey = true; } return (
); } } export const ConversationListItemWithDetails = usingClosedConversationDetails( withTheme(ConversationListItem) );