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

202 lines
6.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';
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';
2017-03-29 02:21:49 +00:00
import Account from '../settings/account';
import Plugins from '../settings/plugins';
import Theme from '../settings/theme';
2017-01-20 17:51:18 +00:00
import * as models from '../../../models/index';
2017-07-21 18:59:17 +00:00
import {Curl} from 'node-libcurl';
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';
2017-07-21 18:59:17 +00:00
import Tooltip from '../tooltip';
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
_handleImportFile () {
2017-02-13 08:12:02 +00:00
this.props.handleImportFile();
this.modal.hide();
}
2017-02-13 08:12:02 +00:00
_handleImportUri (uri) {
this.props.handleImportUri(uri);
this.modal.hide();
}
_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 freshState {...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()}
2017-07-21 18:59:17 +00:00
<Tooltip position="bottom" message={Curl.getVersion()}>
<i className="fa fa-info-circle"/>
</Tooltip>
2017-02-13 08:12:02 +00:00
{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}>
Data
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}>
2017-03-29 02:21:49 +00:00
<Button value="Account" onClick={this._trackTab}>
Account
</Button>
</Tab>
<Tab selected={this._currentTabIndex === 1}>
<Button value="Plugins" onClick={this._trackTab}>
Plugins
</Button>
</Tab>
2017-03-29 02:21:49 +00:00
<Tab selected={this._currentTabIndex === 5}>
2017-02-13 08:12:02 +00:00
<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}
handleToggleMenuBar={this.props.handleToggleMenuBar}
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}
handleImportFile={this._handleImportFile}
handleImportUri={this._handleImportUri}
/>
</TabPanel>
2017-03-29 02:21:49 +00:00
<TabPanel className="scrollable">
<Theme
handleChangeTheme={this._handleChangeTheme}
activeTheme={settings.theme}
/>
</TabPanel>
<TabPanel className="pad scrollable"><SettingsShortcuts/></TabPanel>
<TabPanel className="pad scrollable"><Account/></TabPanel>
<TabPanel className="pad scrollable"><Plugins/></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,
handleImportUri: PropTypes.func.isRequired,
handleToggleMenuBar: PropTypes.func.isRequired,
// Properties
settings: PropTypes.object.isRequired
};
2016-05-07 17:29:24 +00:00
export default SettingsModal;