import React from 'react'; import classNames from 'classnames'; import { debounce, get } from 'lodash'; 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 { LocalizerType } from '../types/Util'; export interface PropsType { searchTerm: string; searchConversationName?: string; searchConversationId?: string; startSearchCounter: number; // To be used as an ID ourNumber: string; regionCode: string; // For display phoneNumber: string; isMe: boolean; name?: string; color: string; verified: boolean; profileName?: string; avatarPath?: string; i18n: LocalizerType; updateSearchTerm: (searchTerm: string) => void; searchMessages: ( query: string, options: { searchConversationId?: string; regionCode: string; } ) => void; searchDiscussions: ( query: string, options: { ourNumber: string; noteToSelf: string; } ) => void; clearConversationSearch: () => void; clearSearch: () => void; showArchivedConversations: () => void; } interface StateType { showingAvatarPopup: boolean; popperRoot: HTMLDivElement | null; } export class MainHeader extends React.Component { private readonly inputRef: React.RefObject; constructor(props: PropsType) { super(props); this.inputRef = React.createRef(); this.state = { showingAvatarPopup: false, popperRoot: null, }; } public componentDidUpdate(prevProps: PropsType) { const { searchConversationId, startSearchCounter } = this.props; // When user chooses to search in a given conversation we focus the field for them if ( searchConversationId && searchConversationId !== prevProps.searchConversationId ) { this.setFocus(); } // When user chooses to start a new search, we focus the field if (startSearchCounter !== prevProps.startSearchCounter) { this.setSelected(); } } public handleOutsideClick = ({ target }: MouseEvent) => { const { popperRoot, showingAvatarPopup } = this.state; if ( showingAvatarPopup && popperRoot && !popperRoot.contains(target as Node) ) { this.hideAvatarPopup(); } }; public handleOutsideKeyDown = (event: KeyboardEvent) => { if (event.key === 'Escape') { this.hideAvatarPopup(); } }; public showAvatarPopup = () => { const popperRoot = document.createElement('div'); document.body.appendChild(popperRoot); this.setState({ showingAvatarPopup: true, popperRoot, }); document.addEventListener('click', this.handleOutsideClick); document.addEventListener('keydown', this.handleOutsideKeyDown); }; public hideAvatarPopup = () => { const { popperRoot } = this.state; document.removeEventListener('click', this.handleOutsideClick); document.removeEventListener('keydown', this.handleOutsideKeyDown); this.setState({ showingAvatarPopup: false, popperRoot: null, }); if (popperRoot && document.body.contains(popperRoot)) { document.body.removeChild(popperRoot); } }; public componentWillUnmount() { const { popperRoot } = this.state; document.removeEventListener('click', this.handleOutsideClick); document.removeEventListener('keydown', this.handleOutsideKeyDown); if (popperRoot && document.body.contains(popperRoot)) { document.body.removeChild(popperRoot); } } // tslint:disable-next-line member-ordering public search = debounce((searchTerm: string) => { const { i18n, ourNumber, regionCode, searchDiscussions, searchMessages, searchConversationId, } = this.props; if (searchDiscussions && !searchConversationId) { searchDiscussions(searchTerm, { noteToSelf: i18n('noteToSelf').toLowerCase(), ourNumber, }); } if (searchMessages) { searchMessages(searchTerm, { searchConversationId, regionCode, }); } }, 200); public updateSearch = (event: React.FormEvent) => { const { updateSearchTerm, clearConversationSearch, clearSearch, searchConversationId, } = this.props; const searchTerm = event.currentTarget.value; if (!searchTerm) { if (searchConversationId) { clearConversationSearch(); } else { clearSearch(); } return; } if (updateSearchTerm) { updateSearchTerm(searchTerm); } if (searchTerm.length < 2) { return; } this.search(searchTerm); }; public clearSearch = () => { const { clearSearch } = this.props; clearSearch(); this.setFocus(); }; public clearConversationSearch = () => { const { clearConversationSearch } = this.props; clearConversationSearch(); this.setFocus(); }; public handleKeyDown = (event: React.KeyboardEvent) => { const { clearConversationSearch, clearSearch, searchConversationId, searchTerm, } = this.props; const { ctrlKey, metaKey, key } = event; const commandKey = get(window, 'platform') === 'darwin' && metaKey; const controlKey = get(window, 'platform') !== 'darwin' && ctrlKey; const commandOrCtrl = commandKey || controlKey; // On linux, this keyboard combination selects all text if (commandOrCtrl && key === '/') { event.preventDefault(); event.stopPropagation(); return; } if (key !== 'Escape') { return; } if (searchConversationId && searchTerm) { clearConversationSearch(); } else { clearSearch(); } event.preventDefault(); event.stopPropagation(); }; public handleXButton = () => { const { searchConversationId, clearConversationSearch, clearSearch, } = this.props; if (searchConversationId) { clearConversationSearch(); } else { clearSearch(); } this.setFocus(); }; public setFocus = () => { if (this.inputRef.current) { // @ts-ignore this.inputRef.current.focus(); } }; public setSelected = () => { if (this.inputRef.current) { // @ts-ignore this.inputRef.current.select(); } }; // tslint:disable-next-line:max-func-body-length public render() { const { avatarPath, color, i18n, name, phoneNumber, profileName, searchConversationId, searchConversationName, searchTerm, showArchivedConversations, } = this.props; const { showingAvatarPopup, popperRoot } = this.state; const placeholder = searchConversationName ? i18n('searchIn', [searchConversationName]) : i18n('search'); return (
{({ ref }) => ( )} {showingAvatarPopup && popperRoot ? createPortal( {({ ref, style }) => ( { showSettings(); this.hideAvatarPopup(); }} onViewArchive={() => { showArchivedConversations(); this.hideAvatarPopup(); }} /> )} , popperRoot ) : null}
{searchConversationId ? ( ) : (
); } }