mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-01-11 22:40:31 +00:00
91 lines
3.5 KiB
JavaScript
91 lines
3.5 KiB
JavaScript
// Copyright (C) 2017-2022 Smart code 203358507
|
|
|
|
const React = require('react');
|
|
const PropTypes = require('prop-types');
|
|
const classnames = require('classnames');
|
|
const { useTranslation } = require('react-i18next');
|
|
const { default: Icon } = require('@stremio/stremio-icons/react');
|
|
const { useRouteFocused } = require('stremio-router');
|
|
const Button = require('stremio/common/Button');
|
|
const TextInput = require('stremio/common/TextInput');
|
|
const useTorrent = require('stremio/common/useTorrent');
|
|
const { withCoreSuspender } = require('stremio/common/CoreSuspender');
|
|
const styles = require('./styles');
|
|
|
|
const SearchBar = ({ className, query, active }) => {
|
|
const { t } = useTranslation();
|
|
const routeFocused = useRouteFocused();
|
|
const { createTorrentFromMagnet } = useTorrent();
|
|
const searchInputRef = React.useRef(null);
|
|
const searchBarOnClick = React.useCallback(() => {
|
|
if (!active) {
|
|
window.location = '#/search';
|
|
}
|
|
}, [active]);
|
|
const queryInputOnChange = React.useCallback(() => {
|
|
try {
|
|
createTorrentFromMagnet(searchInputRef.current.value);
|
|
// eslint-disable-next-line no-empty
|
|
} catch { }
|
|
}, []);
|
|
const queryInputOnSubmit = React.useCallback(() => {
|
|
if (searchInputRef.current !== null) {
|
|
const queryParams = new URLSearchParams([['search', searchInputRef.current.value]]);
|
|
window.location = `#/search?${queryParams.toString()}`;
|
|
}
|
|
}, []);
|
|
React.useEffect(() => {
|
|
if (routeFocused && active) {
|
|
searchInputRef.current.focus();
|
|
}
|
|
}, [routeFocused, active, query]);
|
|
return (
|
|
<label className={classnames(className, styles['search-bar-container'], { 'active': active })} onClick={searchBarOnClick}>
|
|
{
|
|
active ?
|
|
<TextInput
|
|
key={query}
|
|
ref={searchInputRef}
|
|
className={styles['search-input']}
|
|
type={'text'}
|
|
placeholder={t('SEARCH_OR_PASTE_LINK')}
|
|
defaultValue={query}
|
|
tabIndex={-1}
|
|
onChange={queryInputOnChange}
|
|
onSubmit={queryInputOnSubmit}
|
|
/>
|
|
:
|
|
<div className={styles['search-input']}>
|
|
<div className={styles['placeholder-label']}>{ t('SEARCH_OR_PASTE_LINK') }</div>
|
|
</div>
|
|
}
|
|
<Button className={styles['submit-button-container']} tabIndex={-1} onClick={queryInputOnSubmit}>
|
|
<Icon className={styles['icon']} name={'ic_search_link'} />
|
|
</Button>
|
|
</label>
|
|
);
|
|
};
|
|
|
|
SearchBar.propTypes = {
|
|
className: PropTypes.string,
|
|
query: PropTypes.string,
|
|
active: PropTypes.bool
|
|
};
|
|
|
|
const SearchBarFallback = ({ className }) => {
|
|
const { t } = useTranslation();
|
|
return (
|
|
<label className={classnames(className, styles['search-bar-container'])}>
|
|
<div className={styles['search-input']}>
|
|
<div className={styles['placeholder-label']}>{ t('SEARCH_OR_PASTE_LINK') }</div>
|
|
</div>
|
|
<Button className={styles['submit-button-container']} tabIndex={-1}>
|
|
<Icon className={styles['icon']} name={'ic_search_link'} />
|
|
</Button>
|
|
</label>
|
|
);
|
|
};
|
|
|
|
SearchBarFallback.propTypes = SearchBar.propTypes;
|
|
|
|
module.exports = withCoreSuspender(SearchBar, SearchBarFallback);
|