mue/src/components/modals/main/settings/sections/Search.jsx

99 lines
3.1 KiB
React
Raw Normal View History

2020-09-17 11:42:02 +00:00
import React from 'react';
2021-01-17 18:07:26 +00:00
2020-10-27 14:59:45 +00:00
import Dropdown from '../Dropdown';
2020-11-29 14:32:08 +00:00
import Checkbox from '../Checkbox';
import Switch from '../Switch';
import Radio from '../Radio';
2020-09-22 14:43:09 +00:00
import EventBus from '../../../../../modules/helpers/eventbus';
import { toast } from 'react-toastify';
2021-03-20 12:55:20 +00:00
const searchEngines = require('../../../../widgets/search/search_engines.json');
const autocompleteProviders = require('../../../../widgets/search/autocomplete_providers.json')
2020-09-17 11:42:02 +00:00
export default class SearchSettings extends React.PureComponent {
constructor() {
super();
this.state = {
customEnabled: false,
customDisplay: 'none',
customValue: localStorage.getItem('customSearchEngine') || ''
};
}
2020-09-22 14:43:09 +00:00
resetSearch() {
localStorage.removeItem('customSearchEngine');
this.setState({
customValue: ''
});
toast(window.language.toasts.reset);
2020-09-22 14:43:09 +00:00
}
2020-09-18 11:58:37 +00:00
componentDidMount() {
if (localStorage.getItem('searchEngine') === 'custom') {
this.setState({
customDisplay: 'block',
customEnabled: true
});
2021-01-16 22:43:46 +00:00
} else {
localStorage.removeItem('customSearchEngine');
}
2020-09-18 11:58:37 +00:00
}
2021-02-26 12:29:57 +00:00
componentDidUpdate() {
if (this.state.customEnabled === true && this.state.customValue !== '') {
localStorage.setItem('customSearchEngine', this.state.customValue);
2021-02-26 12:29:57 +00:00
}
EventBus.dispatch('refresh', 'search');
2021-02-26 12:29:57 +00:00
}
setSearchEngine(input) {
if (input === 'custom') {
this.setState({
customDisplay: 'block',
customEnabled: true
});
2021-02-26 12:29:57 +00:00
} else {
this.setState({
customDisplay: 'none',
customEnabled: false
});
localStorage.setItem('searchEngine', input);
2021-02-26 12:29:57 +00:00
}
EventBus.dispatch('refresh', 'search');
2021-02-26 12:29:57 +00:00
}
2020-09-17 11:42:02 +00:00
render() {
const language = window.language.modals.main.settings;
const { search } = language.sections;
2021-03-18 15:22:41 +00:00
2020-09-17 11:42:02 +00:00
return (
<>
2021-03-18 15:22:41 +00:00
<h2>{search.title}</h2>
<Switch name='searchBar' text={language.enabled} category='widgets' />
{(navigator.userAgent.includes('Chrome') && typeof InstallTrigger === 'undefined') ?
<Checkbox name='voiceSearch' text={search.voice_search} category='search'/>
: null}
2021-03-23 12:23:21 +00:00
<Dropdown label={search.search_engine} name='searchEngine' onChange={(value) => this.setSearchEngine(value)}>
{searchEngines.map((engine) => (
2021-03-23 12:23:21 +00:00
<option key={engine.name} value={engine.settingsName}>{engine.name}</option>
))}
2021-03-23 12:23:21 +00:00
<option value='custom'>{search.custom.split(' ')[0]}</option>
</Dropdown>
<ul style={{ display: this.state.customDisplay }}>
<br/>
<p style={{ marginTop: '0px' }}>{search.custom} <span className='modalLink' onClick={() => this.resetSearch()}>{language.buttons.reset}</span></p>
<input type='text' value={this.state.customValue} onInput={(e) => this.setState({ customValue: e.target.value })}></input>
</ul>
<br/>
<Checkbox name='autocomplete' text={search.autocomplete} category='search' element='.other'/>
<Radio title={search.autocomplete_provider} options={autocompleteProviders} name='autocompleteProvider' category='search'/>
</>
2020-09-17 11:42:02 +00:00
);
}
}