2018-01-04 06:05:20 +01:00
|
|
|
// @flow
|
2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
2018-01-04 06:05:20 +01:00
|
|
|
import lbryuri from 'lbryuri';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import Autocomplete from './internal/autocomplete';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
updateSearchQuery: string => void,
|
|
|
|
getSearchSuggestions: string => void,
|
|
|
|
onSearch: string => void,
|
|
|
|
onSubmit: string => void,
|
|
|
|
searchQuery: ?string,
|
|
|
|
isActive: boolean,
|
|
|
|
address: ?string,
|
|
|
|
suggestions: Array<string>,
|
|
|
|
};
|
|
|
|
|
|
|
|
class WunderBar extends React.PureComponent<Props> {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
(this: any).handleSubmit = this.handleSubmit.bind(this);
|
|
|
|
(this: any).handleChange = this.handleChange.bind(this);
|
|
|
|
(this: any).focus = this.focus.bind(this);
|
|
|
|
this.input = undefined;
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 06:05:20 +01:00
|
|
|
input: ?HTMLInputElement;
|
2017-05-04 05:44:08 +02:00
|
|
|
|
2018-01-04 06:05:20 +01:00
|
|
|
handleChange(e: SyntheticInputEvent<*>) {
|
|
|
|
const { updateSearchQuery, getSearchSuggestions } = this.props;
|
|
|
|
const { value } = e.target;
|
2017-05-04 05:44:08 +02:00
|
|
|
|
2018-01-04 06:05:20 +01:00
|
|
|
updateSearchQuery(value);
|
|
|
|
getSearchSuggestions(value);
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 06:05:20 +01:00
|
|
|
focus() {
|
|
|
|
const { input } = this;
|
|
|
|
if (input) {
|
|
|
|
input.focus();
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 06:05:20 +01:00
|
|
|
handleSubmit(value: string) {
|
|
|
|
if (!value) {
|
|
|
|
return;
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 06:05:20 +01:00
|
|
|
const { onSubmit, onSearch } = this.props;
|
2017-05-12 20:36:44 +02:00
|
|
|
|
2018-01-04 06:05:20 +01:00
|
|
|
// if they choose the "search for {value}" in the suggestions
|
|
|
|
// it will contain the {query}?search
|
|
|
|
const choseDoSuggestedSearch = value.endsWith('?search');
|
2017-05-12 20:36:44 +02:00
|
|
|
|
2018-01-04 06:05:20 +01:00
|
|
|
let searchValue = value;
|
|
|
|
if (choseDoSuggestedSearch) {
|
|
|
|
searchValue = value.slice(0, -7); // trim off ?search
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 06:05:20 +01:00
|
|
|
if (this.input) {
|
|
|
|
this.input.blur();
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 06:05:20 +01:00
|
|
|
try {
|
|
|
|
const uri = lbryuri.normalize(value);
|
|
|
|
onSubmit(uri);
|
|
|
|
} catch (e) {
|
|
|
|
// search query isn't a valid uri
|
|
|
|
onSearch(searchValue);
|
|
|
|
}
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-01-04 06:05:20 +01:00
|
|
|
const { searchQuery, isActive, address, suggestions } = this.props;
|
|
|
|
|
|
|
|
// if we are on the file/channel page
|
|
|
|
// use the address in the history stack
|
|
|
|
const wunderbarValue = isActive ? searchQuery : searchQuery || address;
|
|
|
|
|
2017-05-04 05:44:08 +02:00
|
|
|
return (
|
2018-01-04 06:05:20 +01:00
|
|
|
<div
|
|
|
|
className={classnames('header__wunderbar', {
|
|
|
|
'header__wunderbar--active': isActive,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<Autocomplete
|
|
|
|
autoHighlight
|
|
|
|
ref={ref => {
|
|
|
|
this.input = ref;
|
|
|
|
}}
|
|
|
|
wrapperStyle={{ flex: 1, minHeight: 0 }}
|
|
|
|
value={wunderbarValue}
|
|
|
|
items={suggestions}
|
|
|
|
getItemValue={item => item.value}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
onSelect={this.handleSubmit}
|
|
|
|
renderInput={props => (
|
|
|
|
<input
|
|
|
|
{...props}
|
|
|
|
className="wunderbar__input"
|
|
|
|
placeholder="Search for videos, music, games and more"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
renderItem={(item, isHighlighted) => (
|
|
|
|
<div
|
|
|
|
key={item.value}
|
|
|
|
className={classnames('wunderbar__suggestion', {
|
|
|
|
'wunderbar__active-suggestion': isHighlighted,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{item.label}
|
|
|
|
</div>
|
|
|
|
)}
|
2017-06-06 23:19:12 +02:00
|
|
|
/>
|
2017-05-04 05:44:08 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WunderBar;
|