import React, {PropTypes, PureComponent} from 'react'; import autobind from 'autobind-decorator'; import EnvironmentsModal from '../modals/workspace-environments-edit-modal'; import {Dropdown, DropdownButton, DropdownDivider, DropdownHint, DropdownItem} 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 ( Use {environment.name} ); } 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 (
{(!activeEnvironment && subEnvironments.length > 0) && ( )}
{activeEnvironment && activeEnvironment.color && ( )} {description}
Activate Environment {subEnvironments.map(this.renderEnvironmentItem)} No Environment General Manage Environments
); } } 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;