2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
2018-01-09 02:15:44 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2018-01-19 16:12:28 +01:00
|
|
|
import { normalizeURI } from 'lbryURI';
|
2018-01-09 02:15:44 +01:00
|
|
|
import Icon from 'component/icon';
|
|
|
|
import { parseQueryParams } from 'util/query_params';
|
|
|
|
|
|
|
|
class WunderBar extends React.PureComponent {
|
|
|
|
static TYPING_TIMEOUT = 800;
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
onSearch: PropTypes.func.isRequired,
|
|
|
|
onSubmit: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this._userTypingTimer = null;
|
|
|
|
this._isSearchDispatchPending = false;
|
|
|
|
this._input = null;
|
|
|
|
this._stateBeforeSearch = null;
|
|
|
|
this._resetOnNextBlur = true;
|
|
|
|
this.onChange = this.onChange.bind(this);
|
|
|
|
this.onFocus = this.onFocus.bind(this);
|
|
|
|
this.onBlur = this.onBlur.bind(this);
|
|
|
|
this.onKeyPress = this.onKeyPress.bind(this);
|
|
|
|
this.onReceiveRef = this.onReceiveRef.bind(this);
|
|
|
|
this.state = {
|
|
|
|
address: this.props.address,
|
|
|
|
icon: this.props.icon,
|
|
|
|
};
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.userTypingTimer) {
|
|
|
|
clearTimeout(this._userTypingTimer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onChange(event) {
|
|
|
|
if (this._userTypingTimer) {
|
|
|
|
clearTimeout(this._userTypingTimer);
|
|
|
|
}
|
2017-05-04 05:44:08 +02:00
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
this.setState({ address: event.target.value });
|
|
|
|
|
|
|
|
this._isSearchDispatchPending = true;
|
|
|
|
|
|
|
|
const searchQuery = event.target.value;
|
|
|
|
|
|
|
|
this._userTypingTimer = setTimeout(() => {
|
|
|
|
const hasQuery = searchQuery.length === 0;
|
|
|
|
this._resetOnNextBlur = hasQuery;
|
|
|
|
this._isSearchDispatchPending = false;
|
|
|
|
if (searchQuery) {
|
|
|
|
this.props.onSearch(searchQuery.trim());
|
|
|
|
}
|
|
|
|
}, WunderBar.TYPING_TIMEOUT); // 800ms delay, tweak for faster/slower
|
|
|
|
}
|
2017-05-04 05:44:08 +02:00
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (
|
|
|
|
nextProps.viewingPage !== this.props.viewingPage ||
|
|
|
|
nextProps.address != this.props.address
|
|
|
|
) {
|
|
|
|
this.setState({ address: nextProps.address, icon: nextProps.icon });
|
|
|
|
}
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
onFocus() {
|
|
|
|
this._stateBeforeSearch = this.state;
|
|
|
|
const newState = {
|
|
|
|
icon: 'icon-search',
|
|
|
|
isActive: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
this._focusPending = true;
|
|
|
|
// below is hacking, improved when we have proper routing
|
|
|
|
if (!this.state.address.startsWith('lbry://') && this.state.icon !== 'icon-search') {
|
|
|
|
// onFocus, if they are not on an exact URL or a search page, clear the bar
|
|
|
|
newState.address = '';
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
2018-01-09 02:15:44 +01:00
|
|
|
this.setState(newState);
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
onBlur() {
|
|
|
|
if (this._isSearchDispatchPending) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.onBlur();
|
|
|
|
}, WunderBar.TYPING_TIMEOUT + 1);
|
|
|
|
} else {
|
|
|
|
const commonState = { isActive: false };
|
|
|
|
if (this._resetOnNextBlur) {
|
|
|
|
this.setState(Object.assign({}, this._stateBeforeSearch, commonState));
|
|
|
|
this._input.value = this.state.address;
|
|
|
|
} else {
|
|
|
|
this._resetOnNextBlur = true;
|
|
|
|
this._stateBeforeSearch = this.state;
|
|
|
|
this.setState(commonState);
|
|
|
|
}
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
2018-01-09 02:15:44 +01:00
|
|
|
}
|
2017-05-04 05:44:08 +02:00
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
componentDidUpdate() {
|
|
|
|
if (this._input) {
|
|
|
|
const start = this._input.selectionStart,
|
|
|
|
end = this._input.selectionEnd;
|
2017-05-12 20:36:44 +02:00
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
this._input.value = this.state.address; // this causes cursor to go to end of input
|
2017-05-12 20:36:44 +02:00
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
this._input.setSelectionRange(start, end);
|
2017-05-04 05:44:08 +02:00
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
if (this._focusPending) {
|
|
|
|
this._input.select();
|
|
|
|
this._focusPending = false;
|
|
|
|
}
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
2018-01-09 02:15:44 +01:00
|
|
|
}
|
2017-05-04 05:44:08 +02:00
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
onKeyPress(event) {
|
|
|
|
if (event.charCode == 13 && this._input.value) {
|
|
|
|
let uri = null,
|
|
|
|
method = 'onSubmit',
|
|
|
|
extraParams = {};
|
|
|
|
|
|
|
|
this._resetOnNextBlur = false;
|
|
|
|
clearTimeout(this._userTypingTimer);
|
|
|
|
|
|
|
|
const parts = this._input.value.trim().split('?');
|
|
|
|
const value = parts.shift();
|
|
|
|
if (parts.length > 0) extraParams = parseQueryParams(parts.join(''));
|
|
|
|
|
|
|
|
try {
|
2018-01-19 16:12:28 +01:00
|
|
|
uri = normalizeURI(value);
|
2018-01-09 02:15:44 +01:00
|
|
|
this.setState({ value: uri });
|
|
|
|
} catch (error) {
|
|
|
|
// then it's not a valid URL, so let's search
|
|
|
|
uri = value;
|
|
|
|
method = 'onSearch';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props[method](uri, extraParams);
|
|
|
|
this._input.blur();
|
2018-01-04 06:05:20 +01:00
|
|
|
}
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
onReceiveRef(ref) {
|
|
|
|
this._input = ref;
|
|
|
|
}
|
2018-01-04 06:05:20 +01:00
|
|
|
|
2018-01-09 02:15:44 +01:00
|
|
|
render() {
|
2017-05-04 05:44:08 +02:00
|
|
|
return (
|
2018-01-09 02:15:44 +01:00
|
|
|
<div className={`wunderbar${this.state.isActive ? ' wunderbar--active' : ''}`}>
|
|
|
|
{this.state.icon ? <Icon fixed icon={this.state.icon} /> : ''}
|
|
|
|
<input
|
|
|
|
className="wunderbar__input"
|
|
|
|
type="search"
|
|
|
|
ref={this.onReceiveRef}
|
|
|
|
onFocus={this.onFocus}
|
|
|
|
onBlur={this.onBlur}
|
|
|
|
onChange={this.onChange}
|
|
|
|
onKeyPress={this.onKeyPress}
|
|
|
|
value={this.state.address}
|
|
|
|
placeholder={__('Find videos, music, games, and more')}
|
2017-06-06 23:19:12 +02:00
|
|
|
/>
|
2017-05-04 05:44:08 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WunderBar;
|