// Copyright 2018-2022 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React from 'react'; import { Manager, Popper, Reference } from 'react-popper'; import { createPortal } from 'react-dom'; import { showSettings } from '../shims/Whisper'; import { Avatar } from './Avatar'; import { AvatarPopup } from './AvatarPopup'; import type { LocalizerType, ThemeType } from '../types/Util'; import type { AvatarColorType } from '../types/Colors'; import type { BadgeType } from '../badges/types'; export type PropsType = { areStoriesEnabled: boolean; avatarPath?: string; badge?: BadgeType; color?: AvatarColorType; hasPendingUpdate: boolean; i18n: LocalizerType; isMe?: boolean; isVerified?: boolean; name?: string; phoneNumber?: string; profileName?: string; theme: ThemeType; title: string; showArchivedConversations: () => void; startComposing: () => void; startUpdate: () => unknown; toggleProfileEditor: () => void; toggleStoriesView: () => unknown; }; type StateType = { showingAvatarPopup: boolean; popperRoot: HTMLDivElement | null; }; export class MainHeader extends React.Component { constructor(props: PropsType) { super(props); this.state = { showingAvatarPopup: false, popperRoot: null, }; } public handleOutsideClick = ({ target }: MouseEvent): void => { const { popperRoot, showingAvatarPopup } = this.state; if ( showingAvatarPopup && popperRoot && !popperRoot.contains(target as Node) ) { this.hideAvatarPopup(); } }; public showAvatarPopup = (): void => { const popperRoot = document.createElement('div'); document.body.appendChild(popperRoot); this.setState({ showingAvatarPopup: true, popperRoot, }); document.addEventListener('click', this.handleOutsideClick); }; public hideAvatarPopup = (): void => { const { popperRoot } = this.state; document.removeEventListener('click', this.handleOutsideClick); this.setState({ showingAvatarPopup: false, popperRoot: null, }); if (popperRoot && document.body.contains(popperRoot)) { document.body.removeChild(popperRoot); } }; public override componentDidMount(): void { document.addEventListener('keydown', this.handleGlobalKeyDown); } public override componentWillUnmount(): void { const { popperRoot } = this.state; document.removeEventListener('click', this.handleOutsideClick); document.removeEventListener('keydown', this.handleGlobalKeyDown); if (popperRoot && document.body.contains(popperRoot)) { document.body.removeChild(popperRoot); } } public handleGlobalKeyDown = (event: KeyboardEvent): void => { const { showingAvatarPopup } = this.state; const { key } = event; if (showingAvatarPopup && key === 'Escape') { this.hideAvatarPopup(); } }; public override render(): JSX.Element { const { areStoriesEnabled, avatarPath, badge, color, hasPendingUpdate, i18n, name, phoneNumber, profileName, showArchivedConversations, startComposing, startUpdate, theme, title, toggleProfileEditor, toggleStoriesView, } = this.props; const { showingAvatarPopup, popperRoot } = this.state; return (
{({ ref }) => (
` needs it to determine blurring. sharedGroupNames={[]} size={28} innerRef={ref} onClick={this.showAvatarPopup} /> {hasPendingUpdate && (
)}
)} {showingAvatarPopup && popperRoot ? createPortal( {({ ref, style }) => ( { toggleProfileEditor(); this.hideAvatarPopup(); }} onViewPreferences={() => { showSettings(); this.hideAvatarPopup(); }} onViewArchive={() => { showArchivedConversations(); this.hideAvatarPopup(); }} /> )} , popperRoot ) : null}
{areStoriesEnabled && (
); } }