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

438 lines
11 KiB
TypeScript
Raw Normal View History

import React from 'react';
2019-07-31 16:16:29 +00:00
import classNames from 'classnames';
import {
ContextMenu,
ContextMenuTrigger,
MenuItem,
SubMenu,
} from 'react-contextmenu';
2020-07-24 01:35:32 +00:00
import { Emojify } from './Emojify';
import { Avatar } from '../Avatar';
import { InContactsIcon } from '../InContactsIcon';
import { LocalizerType } from '../../types/Util';
import { ColorType } from '../../types/Colors';
2020-08-27 19:45:08 +00:00
import { getMuteOptions } from '../../util/getMuteOptions';
2020-07-24 01:35:32 +00:00
interface TimerOption {
name: string;
value: number;
}
2020-07-24 01:35:32 +00:00
export interface PropsDataType {
id: string;
2019-03-12 00:20:16 +00:00
name?: string;
2020-07-24 01:35:32 +00:00
phoneNumber?: string;
profileName?: string;
color?: ColorType;
avatarPath?: string;
2020-07-24 01:35:32 +00:00
type: 'direct' | 'group';
title: string;
2019-03-12 00:20:16 +00:00
acceptedMessageRequest?: boolean;
isVerified?: boolean;
isMe?: boolean;
isArchived?: boolean;
2020-10-02 18:30:43 +00:00
isPinned?: boolean;
2019-03-12 00:20:16 +00:00
2020-09-09 02:25:05 +00:00
disableTimerChanges?: boolean;
expirationSettingName?: string;
2020-08-27 19:45:08 +00:00
muteExpirationLabel?: string;
showBackButton?: boolean;
timerOptions?: Array<TimerOption>;
}
2020-07-24 01:35:32 +00:00
export interface PropsActionsType {
2020-08-27 19:45:08 +00:00
onSetMuteNotifications: (seconds: number) => void;
onSetDisappearingMessages: (seconds: number) => void;
onDeleteMessages: () => void;
onResetSession: () => void;
2019-08-09 23:12:29 +00:00
onSearchInConversation: () => void;
2020-06-04 18:16:19 +00:00
onOutgoingAudioCallInConversation: () => void;
onOutgoingVideoCallInConversation: () => void;
2020-10-02 18:30:43 +00:00
onSetPin: (value: boolean) => void;
onShowSafetyNumber: () => void;
onShowAllMedia: () => void;
onShowGroupMembers: () => void;
onGoBack: () => void;
2019-03-12 00:20:16 +00:00
onArchive: () => void;
onMoveToInbox: () => void;
}
2019-03-12 00:20:16 +00:00
2020-07-24 01:35:32 +00:00
export interface PropsHousekeepingType {
2019-03-12 00:20:16 +00:00
i18n: LocalizerType;
}
2020-07-24 01:35:32 +00:00
export type PropsType = PropsDataType &
PropsActionsType &
PropsHousekeepingType;
2020-07-24 01:35:32 +00:00
export class ConversationHeader extends React.Component<PropsType> {
public showMenuBound: (event: React.MouseEvent<HTMLButtonElement>) => void;
2020-09-14 19:51:27 +00:00
// Comes from a third-party dependency
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2019-01-14 21:49:58 +00:00
public menuTriggerRef: React.RefObject<any>;
2020-07-24 01:35:32 +00:00
public constructor(props: PropsType) {
super(props);
2019-01-14 21:49:58 +00:00
this.menuTriggerRef = React.createRef();
this.showMenuBound = this.showMenu.bind(this);
}
2020-09-14 19:51:27 +00:00
public showMenu(event: React.MouseEvent<HTMLButtonElement>): void {
2019-01-14 21:49:58 +00:00
if (this.menuTriggerRef.current) {
this.menuTriggerRef.current.handleContextClick(event);
}
}
2020-09-14 19:51:27 +00:00
public renderBackButton(): JSX.Element {
const { i18n, onGoBack, showBackButton } = this.props;
return (
<button
2020-09-14 19:51:27 +00:00
type="button"
onClick={onGoBack}
2019-07-31 16:16:29 +00:00
className={classNames(
'module-conversation-header__back-icon',
showBackButton ? 'module-conversation-header__back-icon--show' : null
)}
disabled={!showBackButton}
2020-09-14 19:51:27 +00:00
aria-label={i18n('goBack')}
/>
);
}
2020-09-14 19:51:27 +00:00
public renderTitle(): JSX.Element {
2019-01-31 01:45:58 +00:00
const {
name,
phoneNumber,
2020-07-24 01:35:32 +00:00
title,
type,
2019-01-31 01:45:58 +00:00
i18n,
isMe,
profileName,
isVerified,
} = this.props;
if (isMe) {
return (
<div className="module-conversation-header__title">
{i18n('noteToSelf')}
</div>
);
}
2020-07-24 01:35:32 +00:00
const shouldShowIcon = Boolean(name && type === 'direct');
const shouldShowNumber = Boolean(phoneNumber && (name || profileName));
return (
<div className="module-conversation-header__title">
2020-07-24 01:35:32 +00:00
<Emojify text={title} />
{shouldShowIcon ? (
<span>
{' '}
<InContactsIcon i18n={i18n} />
</span>
) : null}
2020-07-24 01:35:32 +00:00
{shouldShowNumber ? ` · ${phoneNumber}` : null}
{isVerified ? (
<span>
2020-07-24 01:35:32 +00:00
{' · '}
<span className="module-conversation-header__title__verified-icon" />
{i18n('verified')}
</span>
) : null}
</div>
);
}
2020-09-14 19:51:27 +00:00
public renderAvatar(): JSX.Element {
const {
avatarPath,
color,
i18n,
2020-07-24 01:35:32 +00:00
type,
2019-01-31 01:45:58 +00:00
isMe,
name,
phoneNumber,
profileName,
2020-07-24 01:35:32 +00:00
title,
} = this.props;
return (
<span className="module-conversation-header__avatar">
<Avatar
avatarPath={avatarPath}
color={color}
2020-07-24 01:35:32 +00:00
conversationType={type}
i18n={i18n}
2019-01-31 01:45:58 +00:00
noteToSelf={isMe}
2020-07-24 01:35:32 +00:00
title={title}
name={name}
phoneNumber={phoneNumber}
profileName={profileName}
size={28}
/>
</span>
);
}
2020-09-14 19:51:27 +00:00
public renderExpirationLength(): JSX.Element | null {
2019-08-09 23:12:29 +00:00
const { expirationSettingName, showBackButton } = this.props;
if (!expirationSettingName) {
return null;
}
return (
2019-08-09 23:12:29 +00:00
<div
className={classNames(
'module-conversation-header__expiration',
showBackButton
? 'module-conversation-header__expiration--hidden'
: null
)}
>
<div className="module-conversation-header__expiration__clock-icon" />
<div className="module-conversation-header__expiration__setting">
{expirationSettingName}
</div>
</div>
);
}
2020-09-14 19:51:27 +00:00
public renderMoreButton(triggerId: string): JSX.Element {
const { i18n, showBackButton } = this.props;
return (
2019-01-14 21:49:58 +00:00
<ContextMenuTrigger id={triggerId} ref={this.menuTriggerRef}>
<button
2020-09-14 19:51:27 +00:00
type="button"
onClick={this.showMenuBound}
2019-07-31 16:16:29 +00:00
className={classNames(
2019-08-09 23:12:29 +00:00
'module-conversation-header__more-button',
showBackButton
? null
2019-08-09 23:12:29 +00:00
: 'module-conversation-header__more-button--show'
)}
disabled={showBackButton}
2020-09-14 19:51:27 +00:00
aria-label={i18n('moreInfo')}
/>
</ContextMenuTrigger>
);
}
2020-09-14 19:51:27 +00:00
public renderSearchButton(): JSX.Element {
const { i18n, onSearchInConversation, showBackButton } = this.props;
2019-08-09 23:12:29 +00:00
return (
<button
2020-09-14 19:51:27 +00:00
type="button"
2019-08-09 23:12:29 +00:00
onClick={onSearchInConversation}
className={classNames(
'module-conversation-header__search-button',
showBackButton
? null
: 'module-conversation-header__search-button--show'
)}
disabled={showBackButton}
2020-09-14 19:51:27 +00:00
aria-label={i18n('search')}
2019-08-09 23:12:29 +00:00
/>
);
}
2020-09-14 19:51:27 +00:00
public renderOutgoingAudioCallButton(): JSX.Element | null {
const {
i18n,
isMe,
onOutgoingAudioCallInConversation,
showBackButton,
type,
} = this.props;
if (type === 'group' || isMe) {
2020-06-04 18:16:19 +00:00
return null;
}
return (
<button
2020-09-14 19:51:27 +00:00
type="button"
2020-06-04 18:16:19 +00:00
onClick={onOutgoingAudioCallInConversation}
className={classNames(
'module-conversation-header__audio-calling-button',
showBackButton
? null
: 'module-conversation-header__audio-calling-button--show'
)}
disabled={showBackButton}
2020-09-14 19:51:27 +00:00
aria-label={i18n('makeOutgoingCall')}
2020-06-04 18:16:19 +00:00
/>
);
}
2020-09-14 19:51:27 +00:00
public renderOutgoingVideoCallButton(): JSX.Element | null {
const { i18n, isMe, type } = this.props;
if (type === 'group' || isMe) {
2020-06-04 18:16:19 +00:00
return null;
}
const { onOutgoingVideoCallInConversation, showBackButton } = this.props;
return (
<button
2020-09-14 19:51:27 +00:00
type="button"
2020-06-04 18:16:19 +00:00
onClick={onOutgoingVideoCallInConversation}
className={classNames(
'module-conversation-header__video-calling-button',
showBackButton
? null
: 'module-conversation-header__video-calling-button--show'
)}
disabled={showBackButton}
2020-09-14 19:51:27 +00:00
aria-label={i18n('makeOutgoingVideoCall')}
2020-06-04 18:16:19 +00:00
/>
);
}
2020-09-14 19:51:27 +00:00
public renderMenu(triggerId: string): JSX.Element {
const {
2020-09-09 02:25:05 +00:00
disableTimerChanges,
i18n,
acceptedMessageRequest,
isMe,
2020-10-02 18:30:43 +00:00
isPinned,
2020-07-24 01:35:32 +00:00
type,
2019-03-12 00:20:16 +00:00
isArchived,
2020-08-27 19:45:08 +00:00
muteExpirationLabel,
onDeleteMessages,
onResetSession,
onSetDisappearingMessages,
2020-08-27 19:45:08 +00:00
onSetMuteNotifications,
onShowAllMedia,
onShowGroupMembers,
onShowSafetyNumber,
2019-03-12 00:20:16 +00:00
onArchive,
2020-10-02 18:30:43 +00:00
onSetPin,
2019-03-12 00:20:16 +00:00
onMoveToInbox,
timerOptions,
} = this.props;
2020-08-27 19:45:08 +00:00
const muteOptions = [];
if (muteExpirationLabel) {
muteOptions.push(
...[
{
name: i18n('muteExpirationLabel', [muteExpirationLabel]),
disabled: true,
value: 0,
},
{
name: i18n('unmute'),
value: 0,
},
]
);
}
muteOptions.push(...getMuteOptions(i18n));
2020-09-14 19:51:27 +00:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const disappearingTitle = i18n('disappearingMessages') as any;
2020-09-14 19:51:27 +00:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2020-08-27 19:45:08 +00:00
const muteTitle = i18n('muteNotificationsTitle') as any;
2020-07-24 01:35:32 +00:00
const isGroup = type === 'group';
return (
<ContextMenu id={triggerId}>
2020-09-09 02:25:05 +00:00
{disableTimerChanges ? null : (
<SubMenu title={disappearingTitle}>
{(timerOptions || []).map(item => (
<MenuItem
key={item.value}
onClick={() => {
onSetDisappearingMessages(item.value);
}}
>
{item.name}
</MenuItem>
))}
</SubMenu>
2020-09-09 02:25:05 +00:00
)}
2020-08-27 19:45:08 +00:00
<SubMenu title={muteTitle}>
{muteOptions.map(item => (
<MenuItem
key={item.name}
disabled={item.disabled}
onClick={() => {
onSetMuteNotifications(item.value);
}}
>
{item.name}
</MenuItem>
))}
</SubMenu>
2020-06-09 19:54:09 +00:00
<MenuItem onClick={onShowAllMedia}>{i18n('viewRecentMedia')}</MenuItem>
{isGroup ? (
<MenuItem onClick={onShowGroupMembers}>
{i18n('showMembers')}
</MenuItem>
) : null}
{!isGroup && !isMe ? (
<MenuItem onClick={onShowSafetyNumber}>
{i18n('showSafetyNumber')}
</MenuItem>
) : null}
{!isGroup && acceptedMessageRequest ? (
<MenuItem onClick={onResetSession}>{i18n('resetSession')}</MenuItem>
) : null}
2019-03-12 00:20:16 +00:00
{isArchived ? (
<MenuItem onClick={onMoveToInbox}>
{i18n('moveConversationToInbox')}
</MenuItem>
) : (
<MenuItem onClick={onArchive}>{i18n('archiveConversation')}</MenuItem>
)}
2020-10-02 18:30:43 +00:00
{isPinned ? (
<MenuItem onClick={() => onSetPin(false)}>
{i18n('unpinConversation')}
</MenuItem>
) : (
<MenuItem onClick={() => onSetPin(true)}>
{i18n('pinConversation')}
</MenuItem>
)}
<MenuItem onClick={onDeleteMessages}>{i18n('deleteMessages')}</MenuItem>
</ContextMenu>
);
}
2020-09-14 19:51:27 +00:00
public render(): JSX.Element {
const { id } = this.props;
2019-01-14 21:49:58 +00:00
const triggerId = `conversation-${id}`;
return (
<div className="module-conversation-header">
{this.renderBackButton()}
<div className="module-conversation-header__title-container">
<div className="module-conversation-header__title-flex">
{this.renderAvatar()}
{this.renderTitle()}
</div>
</div>
{this.renderExpirationLength()}
2019-08-09 23:12:29 +00:00
{this.renderSearchButton()}
2020-06-04 18:16:19 +00:00
{this.renderOutgoingVideoCallButton()}
{this.renderOutgoingAudioCallButton()}
2019-08-09 23:12:29 +00:00
{this.renderMoreButton(triggerId)}
2019-01-14 21:49:58 +00:00
{this.renderMenu(triggerId)}
</div>
);
}
}