2018-09-29 00:42:13 +00:00
|
|
|
import React from 'react';
|
2019-08-09 23:12:29 +00:00
|
|
|
import classNames from 'classnames';
|
2019-01-14 21:49:58 +00:00
|
|
|
import { debounce } from 'lodash';
|
2019-10-17 18:22:07 +00:00
|
|
|
import { Manager, Popper, Reference } from 'react-popper';
|
|
|
|
import { createPortal } from 'react-dom';
|
2018-09-29 00:42:13 +00:00
|
|
|
|
2019-10-17 18:22:07 +00:00
|
|
|
import { showSettings } from '../shims/Whisper';
|
2018-09-29 00:42:13 +00:00
|
|
|
import { Avatar } from './Avatar';
|
2019-10-17 18:22:07 +00:00
|
|
|
import { AvatarPopup } from './AvatarPopup';
|
2019-01-14 21:49:58 +00:00
|
|
|
import { LocalizerType } from '../types/Util';
|
2018-09-29 00:42:13 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
export interface PropsType {
|
2019-01-14 21:49:58 +00:00
|
|
|
searchTerm: string;
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationName?: string;
|
|
|
|
searchConversationId?: string;
|
2019-11-07 21:36:16 +00:00
|
|
|
startSearchCounter: number;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
// To be used as an ID
|
|
|
|
ourNumber: string;
|
|
|
|
regionCode: string;
|
|
|
|
|
|
|
|
// For display
|
2018-09-29 00:42:13 +00:00
|
|
|
phoneNumber: string;
|
2019-01-14 21:49:58 +00:00
|
|
|
isMe: boolean;
|
2018-09-29 00:42:13 +00:00
|
|
|
name?: string;
|
2019-01-14 21:49:58 +00:00
|
|
|
color: string;
|
2018-09-29 00:42:13 +00:00
|
|
|
verified: boolean;
|
|
|
|
profileName?: string;
|
|
|
|
avatarPath?: string;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
i18n: LocalizerType;
|
|
|
|
updateSearchTerm: (searchTerm: string) => void;
|
2019-09-04 14:46:28 +00:00
|
|
|
searchMessages: (
|
2019-01-14 21:49:58 +00:00
|
|
|
query: string,
|
|
|
|
options: {
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationId?: string;
|
2019-01-14 21:49:58 +00:00
|
|
|
regionCode: string;
|
2019-09-04 14:46:28 +00:00
|
|
|
}
|
|
|
|
) => void;
|
|
|
|
searchDiscussions: (
|
|
|
|
query: string,
|
|
|
|
options: {
|
2019-01-14 21:49:58 +00:00
|
|
|
ourNumber: string;
|
|
|
|
noteToSelf: string;
|
|
|
|
}
|
|
|
|
) => void;
|
2019-08-09 23:12:29 +00:00
|
|
|
|
|
|
|
clearConversationSearch: () => void;
|
2019-01-14 21:49:58 +00:00
|
|
|
clearSearch: () => void;
|
2019-10-17 18:22:07 +00:00
|
|
|
|
|
|
|
showArchivedConversations: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface StateType {
|
|
|
|
showingAvatarPopup: boolean;
|
|
|
|
popperRoot: HTMLDivElement | null;
|
2018-09-29 00:42:13 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 18:22:07 +00:00
|
|
|
export class MainHeader extends React.Component<PropsType, StateType> {
|
2019-01-14 21:49:58 +00:00
|
|
|
private readonly inputRef: React.RefObject<HTMLInputElement>;
|
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
constructor(props: PropsType) {
|
2019-01-14 21:49:58 +00:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.inputRef = React.createRef();
|
2019-10-17 18:22:07 +00:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
showingAvatarPopup: false,
|
|
|
|
popperRoot: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentDidMount() {
|
|
|
|
const popperRoot = document.createElement('div');
|
|
|
|
document.body.appendChild(popperRoot);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
popperRoot,
|
|
|
|
});
|
2019-08-09 23:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public componentDidUpdate(prevProps: PropsType) {
|
2019-11-07 21:36:16 +00:00
|
|
|
const { searchConversationId, startSearchCounter } = this.props;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
// When user chooses to search in a given conversation we focus the field for them
|
|
|
|
if (
|
|
|
|
searchConversationId &&
|
|
|
|
searchConversationId !== prevProps.searchConversationId
|
|
|
|
) {
|
|
|
|
this.setFocus();
|
|
|
|
}
|
2019-11-07 21:36:16 +00:00
|
|
|
// When user chooses to start a new search, we focus the field
|
|
|
|
if (startSearchCounter !== prevProps.startSearchCounter) {
|
|
|
|
this.setSelected();
|
|
|
|
}
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 18:22:07 +00:00
|
|
|
public handleOutsideClick = ({ target }: MouseEvent) => {
|
|
|
|
const { popperRoot, showingAvatarPopup } = this.state;
|
|
|
|
|
|
|
|
if (
|
|
|
|
showingAvatarPopup &&
|
|
|
|
popperRoot &&
|
|
|
|
!popperRoot.contains(target as Node)
|
|
|
|
) {
|
|
|
|
this.hideAvatarPopup();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
public handleOutsideKeyDown = (event: KeyboardEvent) => {
|
2019-10-17 18:22:07 +00:00
|
|
|
if (event.key === 'Escape') {
|
|
|
|
this.hideAvatarPopup();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public showAvatarPopup = () => {
|
|
|
|
this.setState({
|
|
|
|
showingAvatarPopup: true,
|
|
|
|
});
|
|
|
|
document.addEventListener('click', this.handleOutsideClick);
|
2019-11-07 21:36:16 +00:00
|
|
|
document.addEventListener('keydown', this.handleOutsideKeyDown);
|
2019-10-17 18:22:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public hideAvatarPopup = () => {
|
|
|
|
document.removeEventListener('click', this.handleOutsideClick);
|
2019-11-07 21:36:16 +00:00
|
|
|
document.removeEventListener('keydown', this.handleOutsideKeyDown);
|
2019-10-17 18:22:07 +00:00
|
|
|
this.setState({
|
|
|
|
showingAvatarPopup: false,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
public componentWillUnmount() {
|
|
|
|
const { popperRoot } = this.state;
|
|
|
|
|
|
|
|
if (popperRoot) {
|
|
|
|
document.body.removeChild(popperRoot);
|
|
|
|
document.removeEventListener('click', this.handleOutsideClick);
|
2019-11-07 21:36:16 +00:00
|
|
|
document.removeEventListener('keydown', this.handleOutsideKeyDown);
|
2019-10-17 18:22:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
// tslint:disable-next-line member-ordering
|
|
|
|
public search = debounce((searchTerm: string) => {
|
|
|
|
const {
|
|
|
|
i18n,
|
|
|
|
ourNumber,
|
|
|
|
regionCode,
|
2019-09-04 14:46:28 +00:00
|
|
|
searchDiscussions,
|
|
|
|
searchMessages,
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationId,
|
|
|
|
} = this.props;
|
|
|
|
|
2019-09-05 15:59:21 +00:00
|
|
|
if (searchDiscussions && !searchConversationId) {
|
2019-09-04 14:46:28 +00:00
|
|
|
searchDiscussions(searchTerm, {
|
2019-01-14 21:49:58 +00:00
|
|
|
noteToSelf: i18n('noteToSelf').toLowerCase(),
|
|
|
|
ourNumber,
|
2019-09-04 14:46:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (searchMessages) {
|
|
|
|
searchMessages(searchTerm, {
|
|
|
|
searchConversationId,
|
2019-01-14 21:49:58 +00:00
|
|
|
regionCode,
|
|
|
|
});
|
|
|
|
}
|
2019-09-05 15:59:21 +00:00
|
|
|
}, 200);
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
public updateSearch = (event: React.FormEvent<HTMLInputElement>) => {
|
|
|
|
const {
|
|
|
|
updateSearchTerm,
|
|
|
|
clearConversationSearch,
|
|
|
|
clearSearch,
|
|
|
|
searchConversationId,
|
|
|
|
} = this.props;
|
2019-01-14 21:49:58 +00:00
|
|
|
const searchTerm = event.currentTarget.value;
|
|
|
|
|
|
|
|
if (!searchTerm) {
|
2019-08-09 23:12:29 +00:00
|
|
|
if (searchConversationId) {
|
|
|
|
clearConversationSearch();
|
|
|
|
} else {
|
|
|
|
clearSearch();
|
|
|
|
}
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateSearchTerm) {
|
|
|
|
updateSearchTerm(searchTerm);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (searchTerm.length < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
this.search(searchTerm);
|
|
|
|
};
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
public clearSearch = () => {
|
2019-01-14 21:49:58 +00:00
|
|
|
const { clearSearch } = this.props;
|
|
|
|
|
|
|
|
clearSearch();
|
|
|
|
this.setFocus();
|
2019-08-09 23:12:29 +00:00
|
|
|
};
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
public clearConversationSearch = () => {
|
|
|
|
const { clearConversationSearch } = this.props;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
clearConversationSearch();
|
|
|
|
this.setFocus();
|
|
|
|
};
|
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
public handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
2019-08-09 23:12:29 +00:00
|
|
|
const {
|
|
|
|
clearConversationSearch,
|
|
|
|
clearSearch,
|
|
|
|
searchConversationId,
|
|
|
|
searchTerm,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (event.key !== 'Escape') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (searchConversationId && searchTerm) {
|
|
|
|
clearConversationSearch();
|
|
|
|
} else {
|
2019-01-14 21:49:58 +00:00
|
|
|
clearSearch();
|
|
|
|
}
|
2019-11-07 21:36:16 +00:00
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2019-08-09 23:12:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public handleXButton = () => {
|
|
|
|
const {
|
|
|
|
searchConversationId,
|
|
|
|
clearConversationSearch,
|
|
|
|
clearSearch,
|
|
|
|
} = this.props;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
if (searchConversationId) {
|
|
|
|
clearConversationSearch();
|
|
|
|
} else {
|
|
|
|
clearSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setFocus();
|
|
|
|
};
|
|
|
|
|
|
|
|
public setFocus = () => {
|
2019-01-14 21:49:58 +00:00
|
|
|
if (this.inputRef.current) {
|
|
|
|
// @ts-ignore
|
|
|
|
this.inputRef.current.focus();
|
|
|
|
}
|
2019-08-09 23:12:29 +00:00
|
|
|
};
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
public setSelected = () => {
|
|
|
|
if (this.inputRef.current) {
|
|
|
|
// @ts-ignore
|
|
|
|
this.inputRef.current.select();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-17 18:22:07 +00:00
|
|
|
// tslint:disable-next-line:max-func-body-length
|
2018-09-29 00:42:13 +00:00
|
|
|
public render() {
|
|
|
|
const {
|
|
|
|
avatarPath,
|
|
|
|
color,
|
2019-08-09 23:12:29 +00:00
|
|
|
i18n,
|
2018-09-29 00:42:13 +00:00
|
|
|
name,
|
|
|
|
phoneNumber,
|
|
|
|
profileName,
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationId,
|
|
|
|
searchConversationName,
|
|
|
|
searchTerm,
|
2019-10-17 18:22:07 +00:00
|
|
|
showArchivedConversations,
|
2018-09-29 00:42:13 +00:00
|
|
|
} = this.props;
|
2019-10-17 18:22:07 +00:00
|
|
|
const { showingAvatarPopup, popperRoot } = this.state;
|
2018-09-29 00:42:13 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
const placeholder = searchConversationName
|
|
|
|
? i18n('searchIn', [searchConversationName])
|
|
|
|
: i18n('search');
|
|
|
|
|
2018-09-29 00:42:13 +00:00
|
|
|
return (
|
|
|
|
<div className="module-main-header">
|
2019-10-17 18:22:07 +00:00
|
|
|
<Manager>
|
|
|
|
<Reference>
|
|
|
|
{({ ref }) => (
|
|
|
|
<Avatar
|
|
|
|
avatarPath={avatarPath}
|
|
|
|
color={color}
|
|
|
|
conversationType="direct"
|
|
|
|
i18n={i18n}
|
|
|
|
name={name}
|
|
|
|
phoneNumber={phoneNumber}
|
|
|
|
profileName={profileName}
|
|
|
|
size={28}
|
|
|
|
innerRef={ref}
|
|
|
|
onClick={this.showAvatarPopup}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Reference>
|
|
|
|
{showingAvatarPopup && popperRoot
|
|
|
|
? createPortal(
|
|
|
|
<Popper placement="bottom-end">
|
|
|
|
{({ ref, style }) => (
|
|
|
|
<AvatarPopup
|
|
|
|
innerRef={ref}
|
|
|
|
i18n={i18n}
|
|
|
|
style={style}
|
|
|
|
color={color}
|
|
|
|
conversationType="direct"
|
|
|
|
name={name}
|
|
|
|
phoneNumber={phoneNumber}
|
|
|
|
profileName={profileName}
|
|
|
|
avatarPath={avatarPath}
|
|
|
|
size={28}
|
|
|
|
onViewPreferences={() => {
|
|
|
|
showSettings();
|
|
|
|
this.hideAvatarPopup();
|
|
|
|
}}
|
|
|
|
onViewArchive={() => {
|
|
|
|
showArchivedConversations();
|
|
|
|
this.hideAvatarPopup();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Popper>,
|
|
|
|
popperRoot
|
|
|
|
)
|
|
|
|
: null}
|
|
|
|
</Manager>
|
2019-01-14 21:49:58 +00:00
|
|
|
<div className="module-main-header__search">
|
2019-08-09 23:12:29 +00:00
|
|
|
{searchConversationId ? (
|
2019-08-20 19:34:52 +00:00
|
|
|
<button
|
|
|
|
className="module-main-header__search__in-conversation-pill"
|
|
|
|
onClick={this.clearSearch}
|
2019-11-07 21:36:16 +00:00
|
|
|
tabIndex={-1}
|
2019-08-20 19:34:52 +00:00
|
|
|
>
|
2019-08-09 23:12:29 +00:00
|
|
|
<div className="module-main-header__search__in-conversation-pill__avatar-container">
|
|
|
|
<div className="module-main-header__search__in-conversation-pill__avatar" />
|
|
|
|
</div>
|
2019-08-20 19:34:52 +00:00
|
|
|
<div className="module-main-header__search__in-conversation-pill__x-button" />
|
|
|
|
</button>
|
2019-08-09 23:12:29 +00:00
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
className="module-main-header__search__icon"
|
|
|
|
onClick={this.setFocus}
|
2019-11-07 21:36:16 +00:00
|
|
|
tabIndex={-1}
|
2019-08-09 23:12:29 +00:00
|
|
|
/>
|
|
|
|
)}
|
2019-01-14 21:49:58 +00:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
ref={this.inputRef}
|
2019-08-09 23:12:29 +00:00
|
|
|
className={classNames(
|
|
|
|
'module-main-header__search__input',
|
2019-08-20 19:34:52 +00:00
|
|
|
searchTerm
|
|
|
|
? 'module-main-header__search__input--with-text'
|
|
|
|
: null,
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationId
|
|
|
|
? 'module-main-header__search__input--in-conversation'
|
|
|
|
: null
|
|
|
|
)}
|
|
|
|
placeholder={placeholder}
|
2019-01-14 21:49:58 +00:00
|
|
|
dir="auto"
|
2019-11-07 21:36:16 +00:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2019-01-14 21:49:58 +00:00
|
|
|
value={searchTerm}
|
2019-08-09 23:12:29 +00:00
|
|
|
onChange={this.updateSearch}
|
2019-01-14 21:49:58 +00:00
|
|
|
/>
|
|
|
|
{searchTerm ? (
|
2019-11-07 21:36:16 +00:00
|
|
|
<button
|
|
|
|
tabIndex={-1}
|
2019-01-14 21:49:58 +00:00
|
|
|
className="module-main-header__search__cancel-icon"
|
2019-08-09 23:12:29 +00:00
|
|
|
onClick={this.handleXButton}
|
2019-01-14 21:49:58 +00:00
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2018-09-29 00:42:13 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|