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';
|
|
|
|
import Dropdown from '../components/base/Dropdown';
|
|
|
|
import DropdownDivider from '../components/base/DropdownDivider';
|
2016-11-07 20:24:38 +00:00
|
|
|
import {showModal} from '../components/modals/index';
|
2016-10-02 20:57:00 +00:00
|
|
|
import * as db from '../../backend/database';
|
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-09-21 20:49:54 +00:00
|
|
|
db.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-08-15 17:04:36 +00:00
|
|
|
|
|
|
|
return (
|
2016-09-15 22:45:08 +00:00
|
|
|
<Dropdown {...other} className={classnames(className, 'wide')}>
|
2016-08-15 17:04:36 +00:00
|
|
|
<button className="btn btn--super-compact no-wrap">
|
|
|
|
<div className="sidebar__menu__thing">
|
|
|
|
<span>
|
|
|
|
{activeEnvironment && activeEnvironment !== baseEnvironment ? activeEnvironment.name : 'No Environment'}
|
|
|
|
</span>
|
|
|
|
|
|
|
|
<i className="fa fa-caret-down"></i>
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
<ul>
|
|
|
|
<DropdownDivider name="Change Environment"/>
|
|
|
|
{subEnvironments.map(environment => (
|
|
|
|
<li key={environment._id}>
|
|
|
|
<button onClick={() => this._handleActivateEnvironment(environment)}>
|
|
|
|
<i className="fa fa-random"></i> Use <strong>{environment.name}</strong>
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
<li>
|
2016-08-17 18:05:52 +00:00
|
|
|
<button onClick={() => baseEnvironment && this._handleActivateEnvironment(baseEnvironment)}>
|
2016-08-15 17:04:36 +00:00
|
|
|
<i className="fa fa-empty"></i> No Environment
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
<DropdownDivider name="General"/>
|
|
|
|
<li>
|
2016-11-07 20:24:38 +00:00
|
|
|
<button onClick={e => showModal(EnvironmentsModal, workspace)}>
|
2016-08-15 17:04:36 +00:00
|
|
|
<i className="fa fa-wrench"></i> Manage Environments
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</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);
|