import React from 'react'; import classNames from 'classnames'; import { debounce } from 'lodash'; import { Avatar } from './Avatar'; import { LocalizerType } from '../types/Util'; export interface PropsType { searchTerm: string; searchConversationName?: string; searchConversationId?: string; // 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; search: ( query: string, options: { searchConversationId?: string; regionCode: string; ourNumber: string; noteToSelf: string; } ) => void; clearConversationSearch: () => void; clearSearch: () => void; } export class MainHeader extends React.Component { private readonly inputRef: React.RefObject; constructor(props: PropsType) { super(props); this.inputRef = React.createRef(); } public componentDidUpdate(prevProps: PropsType) { const { searchConversationId } = this.props; // When user chooses to search in a given conversation we focus the field for them if ( searchConversationId && searchConversationId !== prevProps.searchConversationId ) { this.setFocus(); } } // tslint:disable-next-line member-ordering public search = debounce((searchTerm: string) => { const { i18n, ourNumber, regionCode, search, searchConversationId, } = this.props; if (search) { search(searchTerm, { searchConversationId, noteToSelf: i18n('noteToSelf').toLowerCase(), ourNumber, regionCode, }); } }, 50); 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 handleKeyUp = (event: React.KeyboardEvent) => { const { clearConversationSearch, clearSearch, searchConversationId, searchTerm, } = this.props; if (event.key !== 'Escape') { return; } if (searchConversationId && searchTerm) { clearConversationSearch(); } else { clearSearch(); } }; 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 render() { const { avatarPath, color, i18n, name, phoneNumber, profileName, searchConversationId, searchConversationName, searchTerm, } = this.props; const placeholder = searchConversationName ? i18n('searchIn', [searchConversationName]) : i18n('search'); return (
{searchConversationId ? (
) : (
); } }