insomnia/app/ui/components/dropdowns/environments-dropdown.js
2017-06-06 17:18:22 -07:00

103 lines
3.3 KiB
JavaScript

import React, {PureComponent, PropTypes} from 'react';
import autobind from 'autobind-decorator';
import classnames from 'classnames';
import EnvironmentsModal from '../modals/workspace-environments-edit-modal';
import {Dropdown, DropdownDivider, DropdownButton, DropdownItem, DropdownHint} from '../base/dropdown';
import {showModal} from '../modals/index';
import {trackEvent} from '../../../analytics/index';
import Tooltip from '../tooltip';
@autobind
class EnvironmentsDropdown extends PureComponent {
_handleActivateEnvironment (environmentId) {
this.props.handleChangeEnvironment(environmentId);
trackEvent('Environment', environmentId ? 'Activate' : 'Deactivate');
}
_handleShowEnvironmentModal () {
showModal(EnvironmentsModal, this.props.workspace);
}
renderEnvironmentItem (environment) {
return (
<DropdownItem key={environment._id}
value={environment._id}
onClick={this._handleActivateEnvironment}>
<i className="fa fa-random"/> Use <strong>{environment.name}</strong>
</DropdownItem>
);
}
render () {
const {
className,
workspace,
environments,
activeEnvironment,
...other
} = this.props;
// NOTE: Base environment might not exist if the users hasn't managed environments yet.
const baseEnvironment = environments.find(e => e.parentId === workspace._id);
const subEnvironments = environments.filter(
e => e.parentId === (baseEnvironment && baseEnvironment._id)
);
let description;
if (!activeEnvironment || activeEnvironment === baseEnvironment) {
description = 'No Environment';
} else {
description = activeEnvironment.name;
}
return (
<Dropdown {...other} className={classnames(className, 'wide')}>
<DropdownButton className="btn btn--super-compact no-wrap">
<div className="sidebar__menu__thing">
{(!activeEnvironment && subEnvironments.length > 0) && (
<Tooltip message="No environments active. Please select one to use."
className="space-right"
position="right">
<i className="fa fa-exclamation-triangle notice"/>
</Tooltip>
)}
<div className="sidebar__menu__thing__text">
{description}
</div>
<i className="space-left fa fa-caret-down"/>
</div>
</DropdownButton>
<DropdownDivider>Activate Environment</DropdownDivider>
{subEnvironments.map(this.renderEnvironmentItem)}
<DropdownItem value={null} onClick={this._handleActivateEnvironment}>
<i className="fa fa-empty"/> No Environment
</DropdownItem>
<DropdownDivider>General</DropdownDivider>
<DropdownItem onClick={this._handleShowEnvironmentModal}>
<i className="fa fa-wrench"/> Manage Environments
<DropdownHint char="E"/>
</DropdownItem>
</Dropdown>
);
}
}
EnvironmentsDropdown.propTypes = {
// Functions
handleChangeEnvironment: PropTypes.func.isRequired,
// Other
workspace: PropTypes.object.isRequired,
environments: PropTypes.arrayOf(PropTypes.object).isRequired,
// Optional
activeEnvironment: PropTypes.object,
className: PropTypes.string
};
export default EnvironmentsDropdown;