insomnia/app/ui/components/modals/SettingsModal.js

176 lines
5.2 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';
2017-02-13 08:12:02 +00:00
import Button from '../base/Button';
import ModalBody from '../base/ModalBody';
import ModalHeader from '../base/ModalHeader';
import SettingsShortcuts from '../settings/SettingsShortcuts';
import SettingsAbout from '../settings/SettingsAbout';
import SettingsGeneral from '../settings/SettingsGeneral';
import SettingsImportExport from '../settings/SettingsImportExport';
import SettingsTheme from '../settings/SettingsTheme';
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) {
console.log('THIS', this);
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">
<SettingsGeneral
settings={settings}
2017-02-13 08:12:02 +00:00
updateSetting={this._handleUpdateSetting}
/>
</TabPanel>
<TabPanel className="pad scrollable">
<SettingsImportExport
2017-02-13 08:12:02 +00:00
handleExportAll={this._handleExportAllToFile}
handleExportWorkspace={this._handleExportWorkspace}
handleImport={this._handleImport}
/>
</TabPanel>
<TabPanel className="pad scrollable">
<SettingsTheme
handleChangeTheme={this._handleChangeTheme}
activeTheme={settings.theme}
/>
</TabPanel>
<TabPanel className="pad scrollable">
<SettingsShortcuts />
</TabPanel>
<TabPanel className="pad scrollable">
<SettingsAbout/>
</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;