2016-08-15 17:04:36 +00:00
|
|
|
import React, {Component, PropTypes} from 'react';
|
|
|
|
import {ipcRenderer} from 'electron';
|
2016-09-15 22:45:08 +00:00
|
|
|
import {connect} from 'react-redux';
|
|
|
|
import classnames from 'classnames';
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
|
|
import EnvironmentsModal from '../components/modals/WorkspaceEnvironmentsEditModal';
|
2016-11-11 22:01:21 +00:00
|
|
|
import {Dropdown, DropdownDivider, DropdownButton, DropdownItem} from '../components/base/dropdown';
|
2016-11-07 20:24:38 +00:00
|
|
|
import {showModal} from '../components/modals/index';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as models from '../../models';
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EnvironmentsDropdown extends Component {
|
|
|
|
_getActiveWorkspace (props) {
|
|
|
|
// TODO: Factor this out into a selector
|
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
const {entities, global} = props || this.props;
|
|
|
|
let workspace = entities.workspaces[global.activeWorkspaceId];
|
2016-08-15 17:04:36 +00:00
|
|
|
if (!workspace) {
|
|
|
|
workspace = entities.workspaces[Object.keys(entities.workspaces)[0]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return workspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleActivateEnvironment (environment) {
|
|
|
|
const workspace = this._getActiveWorkspace();
|
2016-11-10 01:15:27 +00:00
|
|
|
models.workspace.update(workspace, {metaActiveEnvironmentId: environment._id});
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {className, entities, ...other} = this.props;
|
|
|
|
const workspace = this._getActiveWorkspace(this.props);
|
|
|
|
|
|
|
|
const allEnvironments = Object.keys(entities.environments).map(id => entities.environments[id]);
|
2016-08-17 18:05:52 +00:00
|
|
|
|
|
|
|
// NOTE: Base environment might not exist if the users hasn't managed environments yet.
|
2016-08-15 17:04:36 +00:00
|
|
|
const baseEnvironment = allEnvironments.find(e => e.parentId === workspace._id);
|
|
|
|
const subEnvironments = allEnvironments.filter(e => e.parentId === (baseEnvironment && baseEnvironment._id));
|
2016-08-15 22:31:30 +00:00
|
|
|
const activeEnvironment = allEnvironments.find(e => e._id === workspace.metaActiveEnvironmentId) || baseEnvironment;
|
2016-11-12 08:38:55 +00:00
|
|
|
const description = activeEnvironment !== baseEnvironment ? activeEnvironment.name : 'No Environment';
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
|
|
return (
|
2016-09-15 22:45:08 +00:00
|
|
|
<Dropdown {...other} className={classnames(className, 'wide')}>
|
2016-11-11 22:01:21 +00:00
|
|
|
<DropdownButton className="btn btn--super-compact no-wrap">
|
2016-08-15 17:04:36 +00:00
|
|
|
<div className="sidebar__menu__thing">
|
2016-11-12 08:38:55 +00:00
|
|
|
<span>{description}</span> <i className="fa fa-caret-down"></i>
|
2016-08-15 17:04:36 +00:00
|
|
|
</div>
|
2016-11-11 22:01:21 +00:00
|
|
|
</DropdownButton>
|
2016-11-12 01:08:35 +00:00
|
|
|
<DropdownDivider name="Switch Environment"/>
|
2016-11-11 22:01:21 +00:00
|
|
|
{subEnvironments.map(environment => (
|
|
|
|
<DropdownItem key={environment._id}
|
2016-11-12 01:08:35 +00:00
|
|
|
disabled={environment._id === activeEnvironment._id}
|
2016-11-11 22:01:21 +00:00
|
|
|
onClick={() => this._handleActivateEnvironment(environment)}>
|
|
|
|
<i className="fa fa-random"></i> Use <strong>{environment.name}</strong>
|
|
|
|
</DropdownItem>
|
|
|
|
))}
|
2016-11-12 01:08:35 +00:00
|
|
|
<DropdownItem disabled={!activeEnvironment || activeEnvironment._id === baseEnvironment._id}
|
2016-11-12 08:38:55 +00:00
|
|
|
onClick={() => baseEnvironment && this._handleActivateEnvironment(baseEnvironment)}>
|
2016-11-11 22:01:21 +00:00
|
|
|
<i className="fa fa-empty"></i> No Environment
|
|
|
|
</DropdownItem>
|
|
|
|
<DropdownDivider name="General"/>
|
|
|
|
<DropdownItem onClick={e => showModal(EnvironmentsModal, workspace)}>
|
|
|
|
<i className="fa fa-wrench"></i> Manage Environments
|
|
|
|
</DropdownItem>
|
2016-08-15 17:04:36 +00:00
|
|
|
</Dropdown>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EnvironmentsDropdown.propTypes = {
|
2016-10-21 17:20:36 +00:00
|
|
|
global: PropTypes.shape({
|
|
|
|
activeWorkspaceId: PropTypes.string
|
2016-08-15 17:04:36 +00:00
|
|
|
}),
|
|
|
|
entities: PropTypes.shape({
|
|
|
|
workspaces: PropTypes.object.isRequired,
|
|
|
|
environments: PropTypes.object.isRequired
|
|
|
|
}).isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
function mapStateToProps (state) {
|
|
|
|
return {
|
2016-10-21 17:20:36 +00:00
|
|
|
global: state.global,
|
2016-08-15 17:04:36 +00:00
|
|
|
entities: state.entities,
|
|
|
|
actions: state.actions
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(EnvironmentsDropdown);
|