2017-08-10 01:56:27 +00:00
|
|
|
import React, {PureComponent} from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import * as classnames from 'classnames';
|
2017-03-08 05:52:17 +00:00
|
|
|
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 SettingsModal, {TAB_INDEX_EXPORT} from '../modals/settings-modal';
|
2016-11-16 17:18:39 +00:00
|
|
|
import * as models from '../../../models';
|
|
|
|
import {getAppVersion} from '../../../common/constants';
|
2017-06-01 02:04:27 +00:00
|
|
|
import {showModal, showPrompt} from '../modals/index';
|
2016-11-23 19:33:24 +00:00
|
|
|
import {trackEvent} from '../../../analytics/index';
|
2017-03-08 05:52:17 +00:00
|
|
|
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';
|
2017-03-08 05:52:17 +00:00
|
|
|
import LoginModal from '../modals/login-modal';
|
2017-07-27 22:59:07 +00:00
|
|
|
import Tooltip from '../tooltip';
|
2016-11-16 17:18:39 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class WorkspaceDropdown extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
loggedIn: false
|
|
|
|
};
|
|
|
|
}
|
2016-12-23 19:31:38 +00:00
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
async _handleDropdownOpen () {
|
2016-12-23 19:31:38 +00:00
|
|
|
if (this.state.loggedIn !== session.isLoggedIn()) {
|
|
|
|
this.setState({loggedIn: session.isLoggedIn()});
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-12-23 19:31:38 +00:00
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
async _handleDropdownHide () {
|
|
|
|
// Mark all unseen workspace as seen
|
|
|
|
for (const workspace of this.props.unseenWorkspaces) {
|
|
|
|
const workspaceMeta = await models.workspaceMeta.getOrCreateByParentId(workspace._id);
|
|
|
|
if (!workspaceMeta.hasSeen) {
|
|
|
|
models.workspaceMeta.update(workspaceMeta, {hasSeen: true});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleShowLogin () {
|
2016-12-23 19:31:38 +00:00
|
|
|
showModal(LoginModal);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-12-23 19:31:38 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleShowExport () {
|
|
|
|
showModal(SettingsModal, TAB_INDEX_EXPORT);
|
|
|
|
}
|
2017-03-03 20:09:08 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleShowSettings () {
|
|
|
|
showModal(SettingsModal);
|
|
|
|
}
|
2017-03-03 20:09:08 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleShowWorkspaceSettings () {
|
2016-11-28 20:16:07 +00:00
|
|
|
showModal(WorkspaceSettingsModal, {
|
2017-03-03 20:09:08 +00:00
|
|
|
workspace: this.props.activeWorkspace
|
2016-11-16 17:18:39 +00:00
|
|
|
});
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-12-30 23:06:27 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleShowShareSettings () {
|
2016-12-30 23:06:27 +00:00
|
|
|
showModal(WorkspaceShareSettingsModal, {
|
2017-03-03 20:09:08 +00:00
|
|
|
workspace: this.props.activeWorkspace
|
2016-12-30 23:06:27 +00:00
|
|
|
});
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-16 17:18:39 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleSwitchWorkspace (workspaceId) {
|
2016-11-28 20:16:07 +00:00
|
|
|
this.props.handleSetActiveWorkspace(workspaceId);
|
|
|
|
trackEvent('Workspace', 'Switch');
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-16 17:18:39 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
_handleWorkspaceCreate (noTrack) {
|
|
|
|
showPrompt({
|
2017-07-20 01:55:40 +00:00
|
|
|
title: 'Create New Workspace',
|
2016-11-16 17:18:39 +00:00
|
|
|
defaultValue: 'My Workspace',
|
|
|
|
submitName: 'Create',
|
2017-06-01 02:04:27 +00:00
|
|
|
selectText: true,
|
|
|
|
onComplete: async name => {
|
|
|
|
const workspace = await models.workspace.create({name});
|
|
|
|
this.props.handleSetActiveWorkspace(workspace._id);
|
2016-11-16 17:18:39 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
if (!noTrack) {
|
|
|
|
trackEvent('Workspace', 'Create');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-28 20:16:07 +00:00
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
render () {
|
|
|
|
const {
|
|
|
|
className,
|
|
|
|
workspaces,
|
|
|
|
activeWorkspace,
|
2017-07-27 22:59:07 +00:00
|
|
|
unseenWorkspaces,
|
2016-11-16 17:18:39 +00:00
|
|
|
isLoading,
|
|
|
|
...other
|
|
|
|
} = this.props;
|
|
|
|
|
2016-11-28 20:16:07 +00:00
|
|
|
const nonActiveWorkspaces = workspaces.filter(w => w._id !== activeWorkspace._id);
|
2017-07-27 22:59:07 +00:00
|
|
|
const addedWorkspaceNames = unseenWorkspaces.map(w => `"${w.name}"`).join(', ');
|
2016-12-23 19:31:38 +00:00
|
|
|
const classes = classnames(className, 'wide', 'workspace-dropdown');
|
2017-07-27 22:59:07 +00:00
|
|
|
|
|
|
|
const unseenWorkspacesMessage = (
|
|
|
|
<div>
|
|
|
|
The following workspaces were added<br/>
|
|
|
|
{addedWorkspaceNames}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
return (
|
2017-07-27 22:59:07 +00:00
|
|
|
<Dropdown beside
|
|
|
|
className={classes}
|
|
|
|
onOpen={this._handleDropdownOpen}
|
|
|
|
onHide={this._handleDropdownHide}
|
|
|
|
{...other}>
|
2016-11-16 17:18:39 +00:00
|
|
|
<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}
|
2017-07-27 22:59:07 +00:00
|
|
|
{unseenWorkspaces.length > 0 && (
|
|
|
|
<Tooltip message={unseenWorkspacesMessage} position="bottom">
|
|
|
|
<i className="fa fa-asterisk space-left"/>
|
|
|
|
</Tooltip>
|
|
|
|
)}
|
|
|
|
<i className="fa fa-caret-down space-left"/>
|
2016-11-16 17:18:39 +00:00
|
|
|
</div>
|
|
|
|
{activeWorkspace.name}
|
|
|
|
</h1>
|
|
|
|
</DropdownButton>
|
2016-12-21 23:37:48 +00:00
|
|
|
<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
|
2017-03-28 22:45:23 +00:00
|
|
|
<DropdownHint shift char=","/>
|
2016-11-16 17:18:39 +00:00
|
|
|
</DropdownItem>
|
2016-12-30 23:06:27 +00:00
|
|
|
|
2017-01-13 00:45:15 +00:00
|
|
|
<DropdownItem onClick={this._handleShowShareSettings}>
|
2017-01-19 22:36:42 +00:00
|
|
|
<i className="fa fa-globe"/> Share <strong>{activeWorkspace.name}</strong>
|
2017-01-13 00:45:15 +00:00
|
|
|
</DropdownItem>
|
2016-11-16 17:18:39 +00:00
|
|
|
|
2016-12-21 23:37:48 +00:00
|
|
|
<DropdownDivider>Switch Workspace</DropdownDivider>
|
2016-11-16 17:18:39 +00:00
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
{nonActiveWorkspaces.map(w => {
|
|
|
|
const isUnseen = !!unseenWorkspaces.find(v => v._id === w._id);
|
|
|
|
return (
|
|
|
|
<DropdownItem key={w._id} onClick={this._handleSwitchWorkspace} value={w._id}>
|
|
|
|
<i className="fa fa-random"/> To <strong>{w.name}</strong>
|
|
|
|
{isUnseen && (
|
|
|
|
<Tooltip message="This workspace is new">
|
|
|
|
<i className="width-auto fa fa-asterisk surprise"/>
|
|
|
|
</Tooltip>
|
|
|
|
)}
|
|
|
|
</DropdownItem>
|
|
|
|
);
|
|
|
|
})}
|
2017-03-03 20:09:08 +00:00
|
|
|
|
2016-11-28 20:16:07 +00:00
|
|
|
<DropdownItem onClick={this._handleWorkspaceCreate}>
|
2016-11-28 20:58:41 +00:00
|
|
|
<i className="fa fa-empty"/> New Workspace
|
2016-11-16 17:18:39 +00:00
|
|
|
</DropdownItem>
|
|
|
|
|
2016-12-21 23:37:48 +00:00
|
|
|
<DropdownDivider>Insomnia Version {getAppVersion()}</DropdownDivider>
|
2016-11-16 17:18:39 +00:00
|
|
|
|
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=","/>
|
2016-11-16 17:18:39 +00:00
|
|
|
</DropdownItem>
|
2016-11-28 20:58:41 +00:00
|
|
|
<DropdownItem onClick={this._handleShowExport}>
|
|
|
|
<i className="fa fa-share"/> Import/Export
|
|
|
|
</DropdownItem>
|
2017-03-03 20:09:08 +00:00
|
|
|
|
|
|
|
{/* Not Logged In */}
|
|
|
|
|
2017-05-17 19:02:09 +00:00
|
|
|
{!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
|
2017-07-17 22:51:50 +00:00
|
|
|
<i className="fa fa-star surprise fa-outline"/>
|
2017-05-17 19:02:09 +00:00
|
|
|
</DropdownItem>
|
|
|
|
)}
|
2016-11-16 17:18:39 +00:00
|
|
|
</Dropdown>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-16 17:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkspaceDropdown.propTypes = {
|
2017-03-08 05:52:17 +00:00
|
|
|
// Required
|
2016-11-16 17:18:39 +00:00
|
|
|
isLoading: PropTypes.bool.isRequired,
|
|
|
|
handleImportFile: PropTypes.func.isRequired,
|
|
|
|
handleExportFile: PropTypes.func.isRequired,
|
|
|
|
handleSetActiveWorkspace: PropTypes.func.isRequired,
|
|
|
|
workspaces: PropTypes.arrayOf(PropTypes.object).isRequired,
|
2017-07-27 22:59:07 +00:00
|
|
|
unseenWorkspaces: PropTypes.arrayOf(PropTypes.object).isRequired,
|
2017-03-08 05:52:17 +00:00
|
|
|
activeWorkspace: PropTypes.object.isRequired,
|
|
|
|
|
|
|
|
// Optional
|
|
|
|
className: PropTypes.string
|
2016-11-16 17:18:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default WorkspaceDropdown;
|