import React from 'react'; import { ContactName } from './ContactName'; import { Avatar } from '../Avatar'; import { Colors, LocalizerType } from '../../types/Util'; import { ContextMenu, ContextMenuTrigger, MenuItem, SubMenu, } from 'react-contextmenu'; interface TimerOption { name: string; value: number; } interface Props { id: string; name?: string; phoneNumber: string; profileName?: string; color: string; avatarPath?: string; isVerified: boolean; isMe: boolean; isClosable?: boolean; isGroup: boolean; isArchived: boolean; isPublic: boolean; amMod: boolean; members: Array; expirationSettingName?: string; showBackButton: boolean; timerOptions: Array; hasNickname?: boolean; isBlocked: boolean; isFriend: boolean; isFriendRequestPending: boolean; isOnline?: boolean; onSetDisappearingMessages: (seconds: number) => void; onDeleteMessages: () => void; onDeleteContact: () => void; onResetSession: () => void; onArchive: () => void; onMoveToInbox: () => void; onShowSafetyNumber: () => void; onShowAllMedia: () => void; onShowGroupMembers: () => void; onGoBack: () => void; onBlockUser: () => void; onUnblockUser: () => void; onClearNickname: () => void; onChangeNickname: () => void; onCopyPublicKey: () => void; onUpdateGroup: () => void; onLeaveGroup: () => void; onAddModerators: () => void; onRemoveModerators: () => void; onInviteFriends: () => void; onShowUserDetails?: (userPubKey: string) => void; i18n: LocalizerType; } export class ConversationHeader extends React.Component { public showMenuBound: (event: React.MouseEvent) => void; public onShowUserDetailsBound: (userPubKey: string) => void; public menuTriggerRef: React.RefObject; public constructor(props: Props) { super(props); this.menuTriggerRef = React.createRef(); this.showMenuBound = this.showMenu.bind(this); this.onShowUserDetailsBound = this.onShowUserDetails.bind(this); } public showMenu(event: React.MouseEvent) { if (this.menuTriggerRef.current) { this.menuTriggerRef.current.handleContextClick(event); } } public renderBackButton() { const { onGoBack, showBackButton } = this.props; if (!showBackButton) { return null; } return (
); } public renderTitle() { const { phoneNumber, i18n, profileName, isFriend, isGroup, isFriendRequestPending, isMe, name, } = this.props; if (isMe) { return (
{i18n('noteToSelf')}
); } let text = ''; if (isFriendRequestPending) { text = `(${i18n('pending')})`; } else if (!isFriend && !isGroup) { text = `(${i18n('notFriends')})`; } const textEl = text === '' ? null : ( {text} ); return (
{textEl}
); } public renderAvatar() { const { avatarPath, color, i18n, isGroup, isMe, name, phoneNumber, profileName, isOnline, } = this.props; const borderColor = isOnline ? Colors.ONLINE : Colors.OFFLINE_LIGHT; const conversationType = isGroup ? 'group' : 'direct'; return ( { this.onShowUserDetailsBound(phoneNumber); }} /> ); } public renderExpirationLength() { const { expirationSettingName } = this.props; if (!expirationSettingName) { return null; } return (
{expirationSettingName}
); } public renderGear(triggerId: string) { const { showBackButton } = this.props; if (showBackButton) { return null; } return (
); } public renderMenu(triggerId: string) { const { i18n, isMe, isClosable, isPublic, isGroup, amMod, onDeleteMessages, onDeleteContact, onCopyPublicKey, onUpdateGroup, onLeaveGroup, onAddModerators, onRemoveModerators, onInviteFriends, } = this.props; const isPrivateGroup = isGroup && !isPublic; const copyIdLabel = isGroup ? i18n('copyChatId') : i18n('copyPublicKey'); return ( {this.renderPublicMenuItems()} {copyIdLabel} {i18n('deleteMessages')} {isPrivateGroup || amMod ? ( {i18n('updateGroup')} ) : null} {amMod ? ( {i18n('addModerators')} ) : null} {amMod ? ( {i18n('removeModerators')} ) : null} {isPrivateGroup ? ( {i18n('leaveGroup')} ) : null} {/* TODO: add delete group */} {isGroup && isPublic ? ( {i18n('inviteFriends')} ) : null} {!isMe && isClosable && !isPrivateGroup ? ( !isPublic ? ( {i18n('deleteContact')} ) : ( {i18n('deletePublicChannel')} ) ) : null} ); } public render() { const { id, isGroup, isPublic } = this.props; const triggerId = `conversation-${id}`; const isPrivateGroup = isGroup && !isPublic; return (
{this.renderBackButton()}
{this.renderAvatar()} {this.renderTitle()} {isPrivateGroup ? this.renderMemberCount() : null}
{this.renderExpirationLength()} {this.renderGear(triggerId)} {this.renderMenu(triggerId)}
); } public onShowUserDetails(userPubKey: string) { if (this.props.onShowUserDetails) { this.props.onShowUserDetails(userPubKey); } } private renderMemberCount() { const memberCount = this.props.members.length; if (memberCount === 0) { return null; } const wordForm = memberCount === 1 ? 'member' : 'members'; return ( {`(${memberCount} ${wordForm})`} ); } private renderPublicMenuItems() { const { i18n, isBlocked, isMe, isGroup, isArchived, isPublic, onResetSession, onSetDisappearingMessages, // onShowAllMedia, onShowGroupMembers, onShowSafetyNumber, onArchive, onMoveToInbox, timerOptions, onBlockUser, onUnblockUser, hasNickname, onClearNickname, onChangeNickname, } = this.props; if (isPublic) { return null; } const disappearingTitle = i18n('disappearingMessages') as any; const blockTitle = isBlocked ? i18n('unblockUser') : i18n('blockUser'); const blockHandler = isBlocked ? onUnblockUser : onBlockUser; const disappearingMessagesMenuItem = ( {(timerOptions || []).map(item => ( { onSetDisappearingMessages(item.value); }} > {item.name} ))} ); const showMembersMenuItem = isGroup && ( {i18n('showMembers')} ); const showSafetyNumberMenuItem = !isGroup && !isMe && ( {i18n('showSafetyNumber')} ); const resetSessionMenuItem = !isGroup && ( {i18n('resetSession')} ); const blockHandlerMenuItem = !isMe && !isGroup && {blockTitle}; const changeNicknameMenuItem = !isMe && !isGroup && ( {i18n('changeNickname')} ); const clearNicknameMenuItem = !isMe && !isGroup && hasNickname && ( {i18n('clearNickname')} ); const archiveConversationMenuItem = isArchived ? ( {i18n('moveConversationToInbox')} ) : ( {i18n('archiveConversation')} ); return ( {/* {i18n('viewAllMedia')} */} {disappearingMessagesMenuItem} {showMembersMenuItem} {showSafetyNumberMenuItem} {resetSessionMenuItem} {blockHandlerMenuItem} {changeNicknameMenuItem} {clearNicknameMenuItem} {archiveConversationMenuItem} ); } }