signal-desktop/ts/components/MainHeader.tsx

242 lines
5.5 KiB
TypeScript
Raw Normal View History

import React from 'react';
2019-08-09 16:12:29 -07:00
import classNames from 'classnames';
2019-01-14 13:49:58 -08:00
import { debounce } from 'lodash';
import { Avatar } from './Avatar';
2019-01-14 13:49:58 -08:00
import { LocalizerType } from '../types/Util';
2019-08-09 16:12:29 -07:00
export interface PropsType {
2019-01-14 13:49:58 -08:00
searchTerm: string;
2019-08-09 16:12:29 -07:00
searchConversationName?: string;
searchConversationId?: string;
2019-01-14 13:49:58 -08:00
// To be used as an ID
ourNumber: string;
regionCode: string;
// For display
phoneNumber: string;
2019-01-14 13:49:58 -08:00
isMe: boolean;
name?: string;
2019-01-14 13:49:58 -08:00
color: string;
verified: boolean;
profileName?: string;
avatarPath?: string;
2019-01-14 13:49:58 -08:00
i18n: LocalizerType;
updateSearchTerm: (searchTerm: string) => void;
search: (
query: string,
options: {
2019-08-09 16:12:29 -07:00
searchConversationId?: string;
2019-01-14 13:49:58 -08:00
regionCode: string;
ourNumber: string;
noteToSelf: string;
}
) => void;
2019-08-09 16:12:29 -07:00
clearConversationSearch: () => void;
2019-01-14 13:49:58 -08:00
clearSearch: () => void;
}
2019-08-09 16:12:29 -07:00
export class MainHeader extends React.Component<PropsType> {
2019-01-14 13:49:58 -08:00
private readonly inputRef: React.RefObject<HTMLInputElement>;
2019-08-09 16:12:29 -07:00
constructor(props: PropsType) {
2019-01-14 13:49:58 -08:00
super(props);
this.inputRef = React.createRef();
2019-08-09 16:12:29 -07:00
}
public componentDidUpdate(prevProps: PropsType) {
const { searchConversationId } = this.props;
2019-01-14 13:49:58 -08:00
2019-08-09 16:12:29 -07:00
// When user chooses to search in a given conversation we focus the field for them
if (
searchConversationId &&
searchConversationId !== prevProps.searchConversationId
) {
this.setFocus();
}
2019-01-14 13:49:58 -08:00
}
2019-08-09 16:12:29 -07:00
// tslint:disable-next-line member-ordering
public search = debounce((searchTerm: string) => {
const {
i18n,
ourNumber,
regionCode,
search,
searchConversationId,
} = this.props;
2019-01-14 13:49:58 -08:00
if (search) {
search(searchTerm, {
2019-08-09 16:12:29 -07:00
searchConversationId,
2019-01-14 13:49:58 -08:00
noteToSelf: i18n('noteToSelf').toLowerCase(),
ourNumber,
regionCode,
});
}
2019-08-09 16:12:29 -07:00
}, 50);
2019-01-14 13:49:58 -08:00
2019-08-09 16:12:29 -07:00
public updateSearch = (event: React.FormEvent<HTMLInputElement>) => {
const {
updateSearchTerm,
clearConversationSearch,
clearSearch,
searchConversationId,
} = this.props;
2019-01-14 13:49:58 -08:00
const searchTerm = event.currentTarget.value;
if (!searchTerm) {
2019-08-09 16:12:29 -07:00
if (searchConversationId) {
clearConversationSearch();
} else {
clearSearch();
}
2019-01-14 13:49:58 -08:00
return;
}
if (updateSearchTerm) {
updateSearchTerm(searchTerm);
}
if (searchTerm.length < 2) {
return;
}
2019-08-09 16:12:29 -07:00
this.search(searchTerm);
};
2019-01-14 13:49:58 -08:00
2019-08-09 16:12:29 -07:00
public clearSearch = () => {
2019-01-14 13:49:58 -08:00
const { clearSearch } = this.props;
clearSearch();
this.setFocus();
2019-08-09 16:12:29 -07:00
};
2019-01-14 13:49:58 -08:00
2019-08-09 16:12:29 -07:00
public clearConversationSearch = () => {
const { clearConversationSearch } = this.props;
2019-01-14 13:49:58 -08:00
2019-08-09 16:12:29 -07:00
clearConversationSearch();
this.setFocus();
};
public handleKeyUp = (event: React.KeyboardEvent<HTMLInputElement>) => {
const {
clearConversationSearch,
clearSearch,
searchConversationId,
searchTerm,
} = this.props;
if (event.key !== 'Escape') {
return;
}
if (searchConversationId && searchTerm) {
clearConversationSearch();
} else {
2019-01-14 13:49:58 -08:00
clearSearch();
}
2019-08-09 16:12:29 -07:00
};
public handleXButton = () => {
const {
searchConversationId,
clearConversationSearch,
clearSearch,
} = this.props;
2019-01-14 13:49:58 -08:00
2019-08-09 16:12:29 -07:00
if (searchConversationId) {
clearConversationSearch();
} else {
clearSearch();
}
this.setFocus();
};
public setFocus = () => {
2019-01-14 13:49:58 -08:00
if (this.inputRef.current) {
// @ts-ignore
this.inputRef.current.focus();
}
2019-08-09 16:12:29 -07:00
};
2019-01-14 13:49:58 -08:00
public render() {
const {
avatarPath,
color,
2019-08-09 16:12:29 -07:00
i18n,
name,
phoneNumber,
profileName,
2019-08-09 16:12:29 -07:00
searchConversationId,
searchConversationName,
searchTerm,
} = this.props;
2019-08-09 16:12:29 -07:00
const placeholder = searchConversationName
? i18n('searchIn', [searchConversationName])
: i18n('search');
return (
<div className="module-main-header">
<Avatar
avatarPath={avatarPath}
color={color}
conversationType="direct"
i18n={i18n}
name={name}
phoneNumber={phoneNumber}
profileName={profileName}
size={28}
/>
2019-01-14 13:49:58 -08:00
<div className="module-main-header__search">
2019-08-09 16:12:29 -07:00
{searchConversationId ? (
<div className="module-main-header__search__in-conversation-pill">
<div className="module-main-header__search__in-conversation-pill__avatar-container">
<div className="module-main-header__search__in-conversation-pill__avatar" />
</div>
<button
className="module-main-header__search__in-conversation-pill__x-button"
onClick={this.clearSearch}
/>
</div>
) : (
<button
className="module-main-header__search__icon"
onClick={this.setFocus}
/>
)}
2019-01-14 13:49:58 -08:00
<input
type="text"
ref={this.inputRef}
2019-08-09 16:12:29 -07:00
className={classNames(
'module-main-header__search__input',
searchConversationId
? 'module-main-header__search__input--in-conversation'
: null
)}
placeholder={placeholder}
2019-01-14 13:49:58 -08:00
dir="auto"
2019-08-09 16:12:29 -07:00
onKeyUp={this.handleKeyUp}
2019-01-14 13:49:58 -08:00
value={searchTerm}
2019-08-09 16:12:29 -07:00
onChange={this.updateSearch}
2019-01-14 13:49:58 -08:00
/>
{searchTerm ? (
<div
role="button"
className="module-main-header__search__cancel-icon"
2019-08-09 16:12:29 -07:00
onClick={this.handleXButton}
2019-01-14 13:49:58 -08:00
/>
) : null}
</div>
</div>
);
}
}