insomnia/app/ui/components/modals/settings-modal.js

178 lines
5.1 KiB
JavaScript
Raw Normal View History

import React, {PureComponent, PropTypes} from 'react';
import {Tab, Tabs, TabList, TabPanel} from 'react-tabs';
import autobind from 'autobind-decorator';
import Modal from '../base/modal';
import Button from '../base/button';
import ModalBody from '../base/modal-body';
import ModalHeader from '../base/modal-header';
import SettingsShortcuts from '../settings/shortcuts';
import About from '../settings/about';
import General from '../settings/general';
import ImportExport from '../settings/import-export';
import Theme from '../settings/theme';
2017-01-20 17:51:18 +00:00
import * as models from '../../../models/index';
import {getAppVersion, getAppName} from '../../../common/constants';
2016-11-11 01:36:23 +00:00
import {trackEvent} from '../../../analytics/index';
import * as session from '../../../sync/session';
2016-05-07 17:29:24 +00:00
export const TAB_INDEX_EXPORT = 1;
@autobind
class SettingsModal extends PureComponent {
constructor (props) {
super(props);
this.state = {};
this._currentTabIndex = -1;
}
_setModalRef (n) {
this.modal = n;
}
_trackTab (name) {
trackEvent('Setting', `Tab ${name}`);
}
_handleTabSelect (currentTabIndex) {
this.setState({currentTabIndex});
}
_handleUpdateSetting (key, value) {
2017-02-13 08:12:02 +00:00
models.settings.update(this.props.settings, {[key]: value});
trackEvent('Setting', 'Change', key);
}
2017-02-13 08:12:02 +00:00
_handleExportAllToFile () {
2017-02-13 08:12:02 +00:00
this.props.handleExportAllToFile();
this.modal.hide();
}
2017-02-13 08:12:02 +00:00
_handleExportWorkspace () {
2017-02-13 08:12:02 +00:00
this.props.handleExportWorkspaceToFile();
this.modal.hide();
}
2017-02-13 08:12:02 +00:00
_handleImport () {
2017-02-13 08:12:02 +00:00
this.props.handleImportFile();
this.modal.hide();
}
2017-02-13 08:12:02 +00:00
_handleChangeTheme (theme, persist = true) {
document.body.setAttribute('theme', theme);
2017-01-24 06:08:21 +00:00
if (persist) {
trackEvent('Setting', 'Change Theme', theme);
models.settings.update(this.props.settings, {theme});
2017-01-24 06:08:21 +00:00
}
}
2017-01-23 22:41:31 +00:00
componentDidMount () {
// Hacky way to set theme on launch
// TODO: move somewhere else
2017-01-24 06:08:21 +00:00
this._handleChangeTheme(this.props.settings.theme, false);
2017-01-23 22:41:31 +00:00
}
show (currentTabIndex = 0) {
this.setState({currentTabIndex});
this.modal.show();
}
hide () {
this.modal.hide();
}
toggle (currentTabIndex = 0) {
this.setState({currentTabIndex});
this.modal.toggle();
}
render () {
2017-02-13 08:12:02 +00:00
const {settings} = this.props;
const {currentTabIndex} = this.state;
const email = session.isLoggedIn() ? session.getEmail() : null;
2016-05-07 17:29:24 +00:00
return (
<Modal ref={this._setModalRef} tall {...this.props}>
2016-07-19 22:28:29 +00:00
<ModalHeader>
{getAppName()} Preferences
<span className="faint txt-sm">
&nbsp;&nbsp;&nbsp;
2017-02-13 08:12:02 +00:00
v{getAppVersion()}
{email ? ` ${email}` : null}
</span>
2016-07-19 22:28:29 +00:00
</ModalHeader>
<ModalBody noScroll>
<Tabs onSelect={this._handleTabSelect}
selectedIndex={currentTabIndex}
forceRenderTabPanel>
<TabList>
<Tab selected={this._currentTabIndex === 0}>
2017-02-13 08:12:02 +00:00
<Button value="General" onClick={this._trackTab}>
General
2017-02-13 08:12:02 +00:00
</Button>
</Tab>
<Tab selected={this._currentTabIndex === 1}>
2017-02-13 08:12:02 +00:00
<Button value="Import/Export" onClick={this._trackTab}>
Import/Export
2017-02-13 08:12:02 +00:00
</Button>
</Tab>
<Tab selected={this._currentTabIndex === 2}>
2017-02-13 08:12:02 +00:00
<Button value="Themes" onClick={this._trackTab}>
2017-01-23 22:41:31 +00:00
Themes
2017-02-13 08:12:02 +00:00
</Button>
</Tab>
<Tab selected={this._currentTabIndex === 3}>
2017-02-13 08:12:02 +00:00
<Button value="shortcuts" onClick={this._trackTab}>
Shortcuts
2017-02-13 08:12:02 +00:00
</Button>
</Tab>
2017-02-13 08:12:02 +00:00
<Tab selected={this._currentTabIndex === 4}>
<Button value="About" onClick={this._trackTab}>
About
2017-02-13 08:12:02 +00:00
</Button>
</Tab>
</TabList>
<TabPanel className="pad scrollable">
<General
settings={settings}
2017-02-13 08:12:02 +00:00
updateSetting={this._handleUpdateSetting}
/>
</TabPanel>
<TabPanel className="pad scrollable">
<ImportExport
2017-02-13 08:12:02 +00:00
handleExportAll={this._handleExportAllToFile}
handleExportWorkspace={this._handleExportWorkspace}
handleImport={this._handleImport}
/>
</TabPanel>
<TabPanel className="pad scrollable">
<Theme
handleChangeTheme={this._handleChangeTheme}
activeTheme={settings.theme}
/>
</TabPanel>
<TabPanel className="pad scrollable">
<SettingsShortcuts />
</TabPanel>
<TabPanel className="pad scrollable">
<About/>
</TabPanel>
</Tabs>
2016-05-07 17:29:24 +00:00
</ModalBody>
</Modal>
);
}
}
SettingsModal.propTypes = {
// Functions
handleExportWorkspaceToFile: PropTypes.func.isRequired,
handleExportAllToFile: PropTypes.func.isRequired,
handleImportFile: PropTypes.func.isRequired,
// Properties
settings: PropTypes.object.isRequired
};
2016-05-07 17:29:24 +00:00
export default SettingsModal;