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

317 lines
7.6 KiB
TypeScript
Raw Normal View History

import React from 'react';
import { Avatar } from '../Avatar';
2020-10-22 07:34:41 +02:00
import { LocalizerType } from '../../types/Util';
import {
SessionIconButton,
SessionIconSize,
SessionIconType,
} from '../session/icon';
import {
SessionButton,
SessionButtonColor,
SessionButtonType,
} from '../session/SessionButton';
import {
ConversationAvatar,
usingClosedConversationDetails,
} from '../session/usingClosedConversationDetails';
2020-10-22 07:34:41 +02:00
import { MenuProvider } from 'react-contexify';
import {
ConversationHeaderMenu,
PropsConversationHeaderMenu,
} from '../session/menu/ConversationHeaderMenu';
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;
isKickedFromGroup: boolean;
selectionMode: boolean; // is the UI on the message selection mode or not
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
memberAvatars?: Array<ConversationAvatar>; // this is added by usingClosedConversationDetails
}
class ConversationHeader extends React.Component<Props> {
public onAvatarClickBound: (userPubKey: string) => void;
public constructor(props: Props) {
super(props);
this.onAvatarClickBound = this.onAvatarClick.bind(this);
}
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,
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;
2020-11-24 23:14:22 +01:00
const { i18n } = window;
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 renderSelectionOverlay() {
2020-11-24 23:14:22 +01:00
const { onDeleteSelectedMessages, onCloseOverlay, isPublic } = this.props;
const { i18n } = window;
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() {
2020-11-24 23:14:22 +01:00
const { isKickedFromGroup, selectionMode } = this.props;
const triggerId = 'conversation-header';
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">
<MenuProvider id={triggerId} event="onClick">
<SessionIconButton
iconType={SessionIconType.Ellipses}
iconSize={SessionIconSize.Medium}
/>
</MenuProvider>
{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-10-22 07:34:41 +02:00
<ConversationHeaderMenu {...this.getHeaderMenuProps(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();
}
2020-10-22 07:34:41 +02:00
private getHeaderMenuProps(triggerId: string): PropsConversationHeaderMenu {
return {
triggerId,
...this.props,
};
}
}
export const ConversationHeaderWithDetails = usingClosedConversationDetails(
ConversationHeader
);