2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2019-03-28 17:53:13 +01:00
|
|
|
import * as PAGES from 'constants/pages';
|
2018-11-26 02:21:25 +01:00
|
|
|
import * as ICONS from 'constants/icons';
|
2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
2018-03-26 23:32:43 +02:00
|
|
|
import classnames from 'classnames';
|
2019-03-28 17:53:13 +01:00
|
|
|
import { normalizeURI, SEARCH_TYPES, isURIValid, buildURI } from 'lbry-redux';
|
2018-03-26 23:32:43 +02:00
|
|
|
import Icon from 'component/common/icon';
|
2018-11-21 22:20:55 +01:00
|
|
|
import { parseQueryParams } from 'util/query-params';
|
2018-03-27 23:25:23 +02:00
|
|
|
import Autocomplete from './internal/autocomplete';
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2018-10-04 07:59:47 +02:00
|
|
|
const L_KEY_CODE = 76;
|
|
|
|
const ESC_KEY_CODE = 27;
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
2019-03-28 17:53:13 +01:00
|
|
|
searchQuery: ?string,
|
2018-03-26 23:32:43 +02:00
|
|
|
updateSearchQuery: string => void,
|
2019-02-18 18:24:56 +01:00
|
|
|
onSearch: string => void,
|
2019-03-28 17:53:13 +01:00
|
|
|
onSubmit: string => void,
|
2018-03-26 23:32:43 +02:00
|
|
|
wunderbarValue: ?string,
|
|
|
|
suggestions: Array<string>,
|
2018-05-16 20:32:25 +02:00
|
|
|
doFocus: () => void,
|
|
|
|
doBlur: () => void,
|
2018-10-04 07:59:47 +02:00
|
|
|
focused: boolean,
|
2019-04-19 20:05:55 +02:00
|
|
|
doShowSnackBar: string => void,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
2019-03-28 17:53:13 +01:00
|
|
|
type State = {
|
|
|
|
query: ?string,
|
|
|
|
};
|
|
|
|
|
|
|
|
class WunderBar extends React.PureComponent<Props, State> {
|
2018-10-04 07:59:47 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
2017-05-04 05:44:08 +02:00
|
|
|
|
2019-03-28 17:53:13 +01:00
|
|
|
this.state = {
|
|
|
|
query: null,
|
|
|
|
};
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
(this: any).handleSubmit = this.handleSubmit.bind(this);
|
|
|
|
(this: any).handleChange = this.handleChange.bind(this);
|
2018-10-05 20:20:28 +02:00
|
|
|
(this: any).handleKeyDown = this.handleKeyDown.bind(this);
|
2018-10-04 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-10-05 20:20:28 +02:00
|
|
|
window.addEventListener('keydown', this.handleKeyDown);
|
2018-10-04 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('keydown', this.handleKeyDown);
|
2018-01-09 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
getSuggestionIcon = (type: string) => {
|
|
|
|
switch (type) {
|
|
|
|
case 'file':
|
2019-01-23 18:18:24 +01:00
|
|
|
return ICONS.FILE;
|
2018-03-26 23:32:43 +02:00
|
|
|
case 'channel':
|
2019-01-22 21:36:28 +01:00
|
|
|
return ICONS.CHANNEL;
|
2018-03-26 23:32:43 +02:00
|
|
|
default:
|
2018-11-26 02:21:25 +01:00
|
|
|
return ICONS.SEARCH;
|
2018-01-09 02:15:44 +01:00
|
|
|
}
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
2017-05-04 05:44:08 +02:00
|
|
|
|
2018-10-04 07:59:47 +02:00
|
|
|
handleKeyDown(event: SyntheticKeyboardEvent<*>) {
|
|
|
|
const { ctrlKey, metaKey, keyCode } = event;
|
|
|
|
const { doFocus, doBlur, focused } = this.props;
|
|
|
|
|
2019-01-19 19:54:06 +01:00
|
|
|
if (this.input) {
|
|
|
|
if (focused && keyCode === ESC_KEY_CODE) {
|
|
|
|
this.input.blur();
|
|
|
|
doBlur();
|
|
|
|
return;
|
|
|
|
}
|
2018-10-04 07:59:47 +02:00
|
|
|
|
2019-03-28 17:53:13 +01:00
|
|
|
// @if TARGET='app'
|
2019-01-19 19:54:06 +01:00
|
|
|
const shouldFocus =
|
2019-05-07 23:38:29 +02:00
|
|
|
process.platform === 'darwin' ? keyCode === L_KEY_CODE && metaKey : keyCode === L_KEY_CODE && ctrlKey;
|
2018-10-04 07:59:47 +02:00
|
|
|
|
2019-01-19 19:54:06 +01:00
|
|
|
if (shouldFocus) {
|
|
|
|
this.input.focus();
|
|
|
|
doFocus();
|
|
|
|
}
|
2019-03-28 17:53:13 +01:00
|
|
|
// @endif
|
2018-10-04 07:59:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
handleChange(e: SyntheticInputEvent<*>) {
|
|
|
|
const { value } = e.target;
|
2019-03-28 17:53:13 +01:00
|
|
|
const { updateSearchQuery } = this.props;
|
2018-03-26 23:32:43 +02:00
|
|
|
updateSearchQuery(value);
|
|
|
|
}
|
2018-01-09 02:15:44 +01:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
handleSubmit(value: string, suggestion?: { value: string, type: string }) {
|
2019-03-15 00:42:49 +01:00
|
|
|
const { onSubmit, onSearch, doShowSnackBar } = this.props;
|
2018-01-09 02:15:44 +01:00
|
|
|
|
2019-03-28 17:53:13 +01:00
|
|
|
const query = value.trim();
|
2019-03-15 00:42:49 +01:00
|
|
|
const showSnackError = () => {
|
2019-04-19 20:05:55 +02:00
|
|
|
doShowSnackBar('Invalid LBRY URL entered. Only A-Z, a-z, 0-9, and "-" allowed.');
|
2019-03-15 00:42:49 +01:00
|
|
|
};
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
// User selected a suggestion
|
|
|
|
if (suggestion) {
|
|
|
|
if (suggestion.type === 'search') {
|
2019-02-18 18:24:56 +01:00
|
|
|
onSearch(query);
|
2018-10-18 23:05:32 +02:00
|
|
|
} else if (isURIValid(query)) {
|
|
|
|
const uri = normalizeURI(query);
|
2019-03-28 17:53:13 +01:00
|
|
|
onSubmit(uri);
|
2018-01-09 02:15:44 +01:00
|
|
|
} else {
|
2019-03-15 00:42:49 +01:00
|
|
|
showSnackError();
|
2018-01-09 02:15:44 +01:00
|
|
|
}
|
2017-05-04 05:44:08 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
return;
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
2018-03-26 23:32:43 +02:00
|
|
|
// Currently no suggestion is highlighted. The user may have started
|
|
|
|
// typing, then lost focus and came back later on the same page
|
|
|
|
try {
|
2018-10-11 07:56:47 +02:00
|
|
|
if (isURIValid(query)) {
|
|
|
|
const uri = normalizeURI(query);
|
2019-03-28 17:53:13 +01:00
|
|
|
onSubmit(uri);
|
2018-10-11 07:56:47 +02:00
|
|
|
} else {
|
2019-03-15 00:42:49 +01:00
|
|
|
showSnackError();
|
2018-10-11 07:56:47 +02:00
|
|
|
}
|
2018-03-26 23:32:43 +02:00
|
|
|
} catch (e) {
|
2019-02-18 18:24:56 +01:00
|
|
|
onSearch(query);
|
2018-01-04 06:05:20 +01:00
|
|
|
}
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
input: ?HTMLInputElement;
|
2018-01-04 06:05:20 +01:00
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
render() {
|
2019-03-28 17:53:13 +01:00
|
|
|
const { suggestions, doFocus, doBlur, searchQuery } = this.props;
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2017-05-04 05:44:08 +02:00
|
|
|
return (
|
2018-03-26 23:32:43 +02:00
|
|
|
<div className="wunderbar">
|
2018-11-26 02:21:25 +01:00
|
|
|
<Icon icon={ICONS.SEARCH} />
|
2018-03-26 23:32:43 +02:00
|
|
|
<Autocomplete
|
|
|
|
autoHighlight
|
2018-06-21 00:58:55 +02:00
|
|
|
wrapperStyle={{ flex: 1, position: 'relative' }}
|
2019-03-28 17:53:13 +01:00
|
|
|
value={searchQuery}
|
2018-03-26 23:32:43 +02:00
|
|
|
items={suggestions}
|
|
|
|
getItemValue={item => item.value}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
onSelect={this.handleSubmit}
|
2018-05-16 20:32:25 +02:00
|
|
|
inputProps={{
|
|
|
|
onFocus: doFocus,
|
|
|
|
onBlur: doBlur,
|
|
|
|
}}
|
2018-03-26 23:32:43 +02:00
|
|
|
renderInput={props => (
|
|
|
|
<input
|
|
|
|
{...props}
|
2018-10-04 07:59:47 +02:00
|
|
|
ref={el => {
|
|
|
|
props.ref(el);
|
|
|
|
this.input = el;
|
|
|
|
}}
|
2018-03-26 23:32:43 +02:00
|
|
|
className="wunderbar__input"
|
2019-05-01 07:16:12 +02:00
|
|
|
placeholder="Enter a LBRY URL here or search for videos, music, games and more"
|
2018-03-26 23:32:43 +02:00
|
|
|
/>
|
|
|
|
)}
|
2018-06-19 19:59:55 +02:00
|
|
|
renderItem={({ value, type }, isHighlighted) => (
|
2018-03-26 23:32:43 +02:00
|
|
|
<div
|
|
|
|
key={value}
|
|
|
|
className={classnames('wunderbar__suggestion', {
|
|
|
|
'wunderbar__active-suggestion': isHighlighted,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<Icon icon={this.getSuggestionIcon(type)} />
|
2018-06-19 19:59:55 +02:00
|
|
|
<span className="wunderbar__suggestion-label">{value}</span>
|
|
|
|
{isHighlighted && (
|
2018-03-26 23:32:43 +02:00
|
|
|
<span className="wunderbar__suggestion-label--action">
|
2018-06-19 19:59:55 +02:00
|
|
|
{type === SEARCH_TYPES.SEARCH && __('Search')}
|
|
|
|
{type === SEARCH_TYPES.CHANNEL && __('View channel')}
|
|
|
|
{type === SEARCH_TYPES.FILE && __('View file')}
|
2018-03-26 23:32:43 +02:00
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
2017-06-06 23:19:12 +02:00
|
|
|
/>
|
2017-05-04 05:44:08 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WunderBar;
|