insomnia/app/ui/components/dropdowns/workspace-dropdown.js

173 lines
5.3 KiB
JavaScript
Raw Normal View History

2017-03-29 02:21:49 +00:00
import React, {PropTypes, PureComponent} from 'react';
import autobind from 'autobind-decorator';
import * as classnames from 'classnames';
import Dropdown from '../base/dropdown/dropdown';
import DropdownDivider from '../base/dropdown/dropdown-divider';
import DropdownButton from '../base/dropdown/dropdown-button';
import DropdownItem from '../base/dropdown/dropdown-item';
import DropdownHint from '../base/dropdown/dropdown-hint';
import PromptModal from '../modals/prompt-modal';
import SettingsModal, {TAB_INDEX_EXPORT} from '../modals/settings-modal';
import * as models from '../../../models';
import {getAppVersion} from '../../../common/constants';
import {showModal} from '../modals/index';
import {trackEvent} from '../../../analytics/index';
import Link from '../base/link';
import WorkspaceSettingsModal from '../modals/workspace-settings-modal';
import WorkspaceShareSettingsModal from '../modals/workspace-share-settings-modal';
2016-12-23 19:31:38 +00:00
import * as session from '../../../sync/session';
import LoginModal from '../modals/login-modal';
@autobind
class WorkspaceDropdown extends PureComponent {
constructor (props) {
super(props);
this.state = {
loggedIn: false
};
}
2016-12-23 19:31:38 +00:00
_handleDropdownOpen () {
2016-12-23 19:31:38 +00:00
if (this.state.loggedIn !== session.isLoggedIn()) {
this.setState({loggedIn: session.isLoggedIn()});
}
}
2016-12-23 19:31:38 +00:00
_handleShowLogin () {
2016-12-23 19:31:38 +00:00
showModal(LoginModal);
}
2016-12-23 19:31:38 +00:00
_handleShowExport () {
showModal(SettingsModal, TAB_INDEX_EXPORT);
}
_handleShowSettings () {
showModal(SettingsModal);
}
_handleShowWorkspaceSettings () {
2016-11-28 20:16:07 +00:00
showModal(WorkspaceSettingsModal, {
workspace: this.props.activeWorkspace
});
}
2016-12-30 23:06:27 +00:00
_handleShowShareSettings () {
2016-12-30 23:06:27 +00:00
showModal(WorkspaceShareSettingsModal, {
workspace: this.props.activeWorkspace
2016-12-30 23:06:27 +00:00
});
}
_handleSwitchWorkspace (workspaceId) {
2016-11-28 20:16:07 +00:00
this.props.handleSetActiveWorkspace(workspaceId);
trackEvent('Workspace', 'Switch');
}
async _handleWorkspaceCreate (noTrack) {
const name = await showModal(PromptModal, {
headerName: 'Create New Workspace',
defaultValue: 'My Workspace',
submitName: 'Create',
selectText: true
});
const workspace = await models.workspace.create({name});
this.props.handleSetActiveWorkspace(workspace._id);
2016-11-28 20:16:07 +00:00
if (!noTrack) {
trackEvent('Workspace', 'Create');
}
}
2016-11-28 20:16:07 +00:00
render () {
const {
className,
workspaces,
activeWorkspace,
isLoading,
...other
} = this.props;
2016-11-28 20:16:07 +00:00
const nonActiveWorkspaces = workspaces.filter(w => w._id !== activeWorkspace._id);
2016-12-23 19:31:38 +00:00
const classes = classnames(className, 'wide', 'workspace-dropdown');
return (
2016-12-23 19:31:38 +00:00
<Dropdown className={classes} onOpen={this._handleDropdownOpen} {...other}>
<DropdownButton className="btn wide">
<h1 className="no-pad text-left">
<div className="pull-right">
2017-01-23 22:41:31 +00:00
{isLoading ? <i className="fa fa-refresh fa-spin"/> : null}
{' '}
2016-11-28 20:16:07 +00:00
<i className="fa fa-caret-down"/>
</div>
{activeWorkspace.name}
</h1>
</DropdownButton>
<DropdownDivider>{activeWorkspace.name}</DropdownDivider>
2016-11-28 20:16:07 +00:00
<DropdownItem onClick={this._handleShowWorkspaceSettings}>
2016-11-29 21:28:22 +00:00
<i className="fa fa-wrench"/> Workspace Settings
<DropdownHint shift char=","/>
</DropdownItem>
2016-12-30 23:06:27 +00:00
<DropdownItem onClick={this._handleShowShareSettings}>
2017-01-19 22:36:42 +00:00
<i className="fa fa-globe"/> Share <strong>{activeWorkspace.name}</strong>
</DropdownItem>
<DropdownDivider>Switch Workspace</DropdownDivider>
2016-11-28 20:16:07 +00:00
{nonActiveWorkspaces.map(w => (
<DropdownItem key={w._id} onClick={this._handleSwitchWorkspace} value={w._id}>
<i className="fa fa-random"/> To <strong>{w.name}</strong>
</DropdownItem>
))}
2016-11-28 20:16:07 +00:00
<DropdownItem onClick={this._handleWorkspaceCreate}>
<i className="fa fa-empty"/> New Workspace
</DropdownItem>
<DropdownDivider>Insomnia Version {getAppVersion()}</DropdownDivider>
2016-11-28 20:16:07 +00:00
<DropdownItem onClick={this._handleShowSettings}>
2017-01-23 22:41:31 +00:00
<i className="fa fa-cog"/> Preferences
2016-11-29 01:47:04 +00:00
<DropdownHint char=","/>
</DropdownItem>
<DropdownItem onClick={this._handleShowExport}>
<i className="fa fa-share"/> Import/Export
</DropdownItem>
{/* Not Logged In */}
{!this.state.loggedIn && (
<DropdownItem key="login" onClick={this._handleShowLogin}>
<i className="fa fa-sign-in"/> Log In
</DropdownItem>
)}
{!this.state.loggedIn && (
<DropdownItem key="invite"
buttonClass={Link}
href="https://insomnia.rest/pricing/"
button>
<i className="fa fa-users"/> Upgrade to Plus
<i className="fa fa-star notice fa-outline txt-lg"/>
</DropdownItem>
)}
</Dropdown>
);
}
}
WorkspaceDropdown.propTypes = {
// Required
isLoading: PropTypes.bool.isRequired,
handleImportFile: PropTypes.func.isRequired,
handleExportFile: PropTypes.func.isRequired,
handleSetActiveWorkspace: PropTypes.func.isRequired,
workspaces: PropTypes.arrayOf(PropTypes.object).isRequired,
activeWorkspace: PropTypes.object.isRequired,
// Optional
className: PropTypes.string
};
export default WorkspaceDropdown;