session-desktop/ts/components/conversation/ConversationHeader.tsx

477 lines
11 KiB
TypeScript
Raw Normal View History

import React from 'react';
import { Avatar } from '../Avatar';
import { Colors, LocalizerType } from '../../types/Util';
import { ContextMenu, ContextMenuTrigger, MenuItem } from 'react-contextmenu';
import {
SessionIconButton,
SessionIconSize,
SessionIconType,
} from '../session/icon';
import {
SessionButton,
SessionButtonColor,
SessionButtonType,
} from '../session/SessionButton';
import * as Menu from '../../session/utils/Menu';
import {
ConversationAvatar,
usingClosedConversationDetails,
} from '../session/usingClosedConversationDetails';
2019-12-09 03:48:04 +01:00
export interface TimerOption {
name: string;
value: number;
}
interface Props {
id: string;
2019-03-12 01:20:16 +01:00
name?: string;
phoneNumber: string;
profileName?: string;
avatarPath?: string;
2019-03-12 01:20:16 +01:00
isVerified: boolean;
isMe: boolean;
isClosable?: boolean;
isGroup: boolean;
isPrivate: boolean;
isPublic: boolean;
isRss: boolean;
amMod: boolean;
2019-03-12 01:20:16 +01:00
// We might not always have the full list of members,
// e.g. for open groups where we could have thousands
// of members. We'll keep this for now (for closed chats)
members: Array<any>;
// not equal members.length (see above)
subscriberCount?: number;
expirationSettingName?: string;
showBackButton: boolean;
timerOptions: Array<TimerOption>;
2018-11-27 01:47:20 +01:00
hasNickname?: boolean;
isBlocked: boolean;
isOnline?: boolean;
2020-02-28 01:48:21 +01:00
// We don't pass this as a bool, because in future we
// want to forward messages from Header and will need
// the message ID.
selectedMessages: Array<string>;
isKickedFromGroup: boolean;
2020-07-16 08:39:35 +02:00
onInviteContacts: () => void;
onSetDisappearingMessages: (seconds: number) => void;
onDeleteMessages: () => void;
onDeleteContact: () => void;
onResetSession: () => void;
2019-12-11 07:48:54 +01:00
onCloseOverlay: () => void;
onDeleteSelectedMessages: () => void;
onShowSafetyNumber: () => void;
onGoBack: () => void;
onBlockUser: () => void;
onUnblockUser: () => void;
2018-11-27 01:47:20 +01:00
onCopyPublicKey: () => void;
2019-03-12 01:20:16 +01:00
2019-10-21 04:07:24 +02:00
onLeaveGroup: () => void;
2019-12-10 09:17:45 +01:00
onAddModerators: () => void;
onRemoveModerators: () => void;
onAvatarClick?: (userPubKey: string) => void;
2020-03-04 03:39:51 +01:00
onUpdateGroupName: () => void;
2019-11-22 06:16:43 +01:00
2019-03-12 01:20:16 +01:00
i18n: LocalizerType;
memberAvatars?: Array<ConversationAvatar>; // this is added by usingClosedConversationDetails
}
class ConversationHeader extends React.Component<Props> {
public showMenuBound: (event: React.MouseEvent<HTMLDivElement>) => void;
public onAvatarClickBound: (userPubKey: string) => void;
2019-01-14 22:49:58 +01:00
public menuTriggerRef: React.RefObject<any>;
public constructor(props: Props) {
super(props);
2019-01-14 22:49:58 +01:00
this.menuTriggerRef = React.createRef();
this.showMenuBound = this.showMenu.bind(this);
this.onAvatarClickBound = this.onAvatarClick.bind(this);
}
public showMenu(event: React.MouseEvent<HTMLDivElement>) {
2019-01-14 22:49:58 +01:00
if (this.menuTriggerRef.current) {
this.menuTriggerRef.current.handleContextClick(event);
}
}
public renderBackButton() {
const { onGoBack, showBackButton } = this.props;
if (!showBackButton) {
return null;
}
return (
<div
onClick={onGoBack}
role="button"
className="module-conversation-header__back-icon"
/>
);
}
public renderTitle() {
2019-07-23 05:14:09 +02:00
const {
phoneNumber,
i18n,
profileName,
isGroup,
isPublic,
isRss,
members,
subscriberCount,
2019-07-23 05:14:09 +02:00
isMe,
isKickedFromGroup,
2019-07-23 05:14:09 +02:00
name,
} = this.props;
2019-01-31 02:45:58 +01:00
if (isMe) {
return (
<div className="module-conversation-header__title">
{i18n('noteToSelf')}
</div>
);
}
const memberCount: number = (() => {
if (!isGroup || isRss) {
return 0;
}
if (isPublic) {
return subscriberCount || 0;
} else {
return members.length;
}
})();
let text = '';
if (isGroup && memberCount > 0) {
const count = String(memberCount);
text = i18n('members', [count]);
}
const textEl =
text === '' || isKickedFromGroup ? null : (
<span className="module-conversation-header__title-text">{text}</span>
);
const title = profileName || name || phoneNumber;
return (
<div className="module-conversation-header__title">
2020-01-07 03:24:44 +01:00
<span className="module-contact-name__profile-name">{title}</span>
{textEl}
</div>
);
}
public renderAvatar() {
const {
avatarPath,
memberAvatars,
name,
phoneNumber,
profileName,
} = this.props;
2020-09-15 07:07:22 +02:00
const userName = name || profileName || phoneNumber;
return (
<span className="module-conversation-header__avatar">
<Avatar
avatarPath={avatarPath}
2020-09-15 07:07:22 +02:00
name={userName}
size={36}
onAvatarClick={() => {
this.onAvatarClickBound(phoneNumber);
}}
memberAvatars={memberAvatars}
2020-09-15 07:07:22 +02:00
pubkey={phoneNumber}
/>
</span>
);
}
public renderExpirationLength() {
const { expirationSettingName } = this.props;
if (!expirationSettingName) {
return null;
}
return (
<div className="module-conversation-header__expiration">
<div className="module-conversation-header__expiration__clock-icon" />
<div className="module-conversation-header__expiration__setting">
{expirationSettingName}
</div>
</div>
);
}
2019-12-09 03:48:04 +01:00
public renderSearch() {
return (
<div className="search-icon">
2019-12-09 03:48:04 +01:00
<SessionIconButton
iconType={SessionIconType.Search}
iconSize={SessionIconSize.Large}
2019-12-09 03:57:18 +01:00
iconPadded={true}
onClick={this.highlightMessageSearch}
2019-12-09 03:48:04 +01:00
/>
</div>
);
}
public renderOptions(triggerId: string) {
const { showBackButton } = this.props;
if (showBackButton) {
return null;
}
return (
2020-01-20 04:36:46 +01:00
<ContextMenuTrigger
id={triggerId}
ref={this.menuTriggerRef}
holdToDisplay={1}
>
<SessionIconButton
iconType={SessionIconType.Ellipses}
iconSize={SessionIconSize.Medium}
2020-01-20 04:36:46 +01:00
onClick={this.showMenuBound}
/>
</ContextMenuTrigger>
);
}
public renderMenu(triggerId: string) {
const {
i18n,
isMe,
isClosable,
isPublic,
2019-12-13 06:46:39 +01:00
isRss,
isGroup,
isKickedFromGroup,
amMod,
onDeleteMessages,
onDeleteContact,
onCopyPublicKey,
2019-10-21 04:07:24 +02:00
onLeaveGroup,
2019-12-10 09:17:45 +01:00
onAddModerators,
onRemoveModerators,
2020-06-16 02:00:37 +02:00
onInviteContacts,
2020-03-04 03:39:51 +01:00
onUpdateGroupName,
} = this.props;
return (
<ContextMenu id={triggerId}>
{this.renderPublicMenuItems()}
{Menu.getCopyMenuItem(isPublic, isRss, isGroup, onCopyPublicKey, i18n)}
{Menu.getDeleteMessagesMenuItem(isPublic, onDeleteMessages, i18n)}
{Menu.getAddModeratorsMenuItem(
amMod,
isKickedFromGroup,
onAddModerators,
i18n
)}
{Menu.getRemoveModeratorsMenuItem(
amMod,
isKickedFromGroup,
onRemoveModerators,
i18n
)}
{Menu.getUpdateGroupNameMenuItem(
amMod,
isKickedFromGroup,
onUpdateGroupName,
i18n
)}
{Menu.getLeaveGroupMenuItem(
isKickedFromGroup,
isGroup,
isPublic,
isRss,
onLeaveGroup,
i18n
)}
{/* TODO: add delete group */}
{Menu.getInviteContactMenuItem(
isGroup,
isPublic,
onInviteContacts,
i18n
)}
{Menu.getDeleteContactMenuItem(
isMe,
isClosable,
isGroup,
isPublic,
isRss,
onDeleteContact,
i18n
)}
</ContextMenu>
);
}
public renderSelectionOverlay() {
2020-04-07 08:49:23 +02:00
const {
onDeleteSelectedMessages,
onCloseOverlay,
isPublic,
i18n,
} = this.props;
2020-04-07 10:15:23 +02:00
const isServerDeletable = isPublic;
2020-04-07 08:49:23 +02:00
const deleteMessageButtonText = i18n(
2020-08-13 09:22:02 +02:00
isServerDeletable ? 'deleteForEveryone' : 'delete'
2020-04-07 08:49:23 +02:00
);
return (
<div className="message-selection-overlay">
<div className="close-button">
<SessionIconButton
iconType={SessionIconType.Exit}
iconSize={SessionIconSize.Medium}
2020-07-21 06:43:46 +02:00
onClick={onCloseOverlay}
/>
</div>
<div className="button-group">
<SessionButton
buttonType={SessionButtonType.Default}
buttonColor={SessionButtonColor.Danger}
2020-04-07 08:43:53 +02:00
text={deleteMessageButtonText}
2019-12-11 07:48:54 +01:00
onClick={onDeleteSelectedMessages}
/>
</div>
</div>
);
}
public render() {
const { id, isKickedFromGroup } = this.props;
const triggerId = `conversation-header-${id}-${Date.now()}`;
2020-02-28 01:48:21 +01:00
const selectionMode = !!this.props.selectedMessages.length;
return (
2020-02-28 01:48:21 +01:00
<div className="module-conversation-header">
<div className="conversation-header--items-wrapper">
{this.renderBackButton()}
<div className="module-conversation-header__title-container">
<div className="module-conversation-header__title-flex">
2020-07-21 06:43:46 +02:00
{!selectionMode && this.renderOptions(triggerId)}
{this.renderTitle()}
</div>
</div>
{!isKickedFromGroup && this.renderExpirationLength()}
2020-02-14 04:25:36 +01:00
2020-07-21 06:43:46 +02:00
{!this.props.isRss && !selectionMode && this.renderAvatar()}
2020-02-14 04:25:36 +01:00
2020-07-21 06:43:46 +02:00
{!selectionMode && this.renderMenu(triggerId)}
</div>
2020-02-28 01:48:21 +01:00
2020-03-13 01:10:27 +01:00
{selectionMode && this.renderSelectionOverlay()}
2020-02-28 01:48:21 +01:00
</div>
);
}
public onAvatarClick(userPubKey: string) {
if (this.props.onAvatarClick) {
this.props.onAvatarClick(userPubKey);
2019-11-28 04:54:04 +01:00
}
}
public highlightMessageSearch() {
// This is a temporary fix. In future we want to search
// messages in the current conversation
($('.session-search-input input') as any).focus();
}
// tslint:disable-next-line: cyclomatic-complexity
private renderPublicMenuItems() {
const {
i18n,
isBlocked,
isMe,
isGroup,
isPrivate,
isKickedFromGroup,
isPublic,
2019-12-13 06:46:39 +01:00
isRss,
onResetSession,
onSetDisappearingMessages,
onShowSafetyNumber,
timerOptions,
onBlockUser,
onUnblockUser,
} = this.props;
const disappearingMessagesMenuItem = Menu.getDisappearingMenuItem(
isPublic,
isRss,
isKickedFromGroup,
isBlocked,
timerOptions,
onSetDisappearingMessages,
i18n
2020-05-29 08:25:15 +02:00
);
const showSafetyNumberMenuItem = Menu.getShowSafetyNumberMenuItem(
isPublic,
isRss,
isGroup,
isMe,
onShowSafetyNumber,
i18n
2020-05-29 08:25:15 +02:00
);
const resetSessionMenuItem = Menu.getResetSessionMenuItem(
isPublic,
isRss,
isGroup,
isBlocked,
onResetSession,
i18n
2020-05-29 08:25:15 +02:00
);
const blockHandlerMenuItem = Menu.getBlockMenuItem(
isMe,
isPrivate,
isBlocked,
onBlockUser,
onUnblockUser,
i18n
2020-05-29 08:25:15 +02:00
);
return (
2019-09-04 03:09:05 +02:00
<React.Fragment>
{disappearingMessagesMenuItem}
{showSafetyNumberMenuItem}
{resetSessionMenuItem}
{blockHandlerMenuItem}
</React.Fragment>
);
}
}
export const ConversationHeaderWithDetails = usingClosedConversationDetails(
ConversationHeader
);