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

151 lines
4.7 KiB
React
Raw Normal View History

import variables from 'modules/variables';
2021-08-14 19:10:48 +00:00
import { PureComponent } from 'react';
2021-08-15 21:28:37 +00:00
import { toast } from 'react-toastify';
import { MenuItem, TextField } from '@mui/material';
2021-01-17 18:07:26 +00:00
import Header from '../Header';
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 SettingsItem from '../SettingsItem';
2020-09-22 14:43:09 +00:00
2021-08-28 14:34:12 +00:00
import EventBus from 'modules/helpers/eventbus';
import searchEngines from 'components/widgets/search/search_engines.json';
2020-09-17 11:42:02 +00:00
2021-08-14 19:10:48 +00:00
export default class SearchSettings extends 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(variables.getMessage('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() {
return (
<>
<Header
title={variables.getMessage('modals.main.settings.sections.search.title')}
setting="searchBar"
category="widgets"
switch={true}
/>
<SettingsItem
title={variables.getMessage('modals.main.settings.additional_settings')}
subtitle={variables.getMessage('modals.main.settings.sections.search.additional')}
>
{/* not supported on firefox */}
{navigator.userAgent.includes('Chrome') && typeof InstallTrigger === 'undefined' ? (
<Checkbox
name="voiceSearch"
text={variables.getMessage('modals.main.settings.sections.search.voice_search')}
category="search"
/>
) : null}
<Checkbox
name="searchDropdown"
text={variables.getMessage('modals.main.settings.sections.search.dropdown')}
category="search"
element=".other"
/>
<Checkbox
name="searchFocus"
text={variables.getMessage('modals.main.settings.sections.search.focus')}
category="search"
element=".other"
/>
<Checkbox
name="autocomplete"
text={variables.getMessage('modals.main.settings.sections.search.autocomplete')}
category="search"
/>
</SettingsItem>
<SettingsItem
title={variables.getMessage('modals.main.settings.sections.search.search_engine')}
subtitle={variables.getMessage(
'modals.main.settings.sections.search.search_engine_subtitle',
)}
2023-01-21 12:10:40 +00:00
final={ this.state.customDisplay === 'none' ? true : false}
>
<Dropdown
name="searchEngine"
onChange={(value) => this.setSearchEngine(value)}
manual={true}
>
{searchEngines.map((engine) => (
<MenuItem key={engine.name} value={engine.settingsName}>
{engine.name}
</MenuItem>
))}
<MenuItem value="custom">
{variables.getMessage('modals.main.settings.sections.search.custom').split(' ')[0]}
</MenuItem>
</Dropdown>
</SettingsItem>
<div style={{ display: this.state.customDisplay }}>
2023-01-21 12:10:40 +00:00
<SettingsItem title={variables.getMessage('modals.main.settings.sections.search.custom')} final={true}>
<TextField
label={variables.getMessage('modals.main.settings.sections.search.custom')}
value={this.state.customValue}
onInput={(e) => this.setState({ customValue: e.target.value })}
varient="outlined"
InputLabelProps={{ shrink: true }}
/>
<p style={{ marginTop: '0px' }}>
<span className="link" onClick={() => this.resetSearch()}>
{variables.getMessage('modals.main.settings.buttons.reset')}
</span>
</p>
</SettingsItem>
</div>
</>
2020-09-17 11:42:02 +00:00
);
}
}