import React, {PureComponent} from 'react'; import PropTypes from 'prop-types'; import autobind from 'autobind-decorator'; import * as classnames from 'classnames'; 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'; import * as models from '../../../models'; import {getAppVersion} from '../../../common/constants'; import {showModal, showPrompt} from '../modals/index'; import {trackEvent} from '../../../analytics/index'; import Link from '../base/link'; import WorkspaceSettingsModal from '../modals/workspace-settings-modal'; import WorkspaceShareSettingsModal from '../modals/workspace-share-settings-modal'; import * as session from '../../../sync/session'; import LoginModal from '../modals/login-modal'; import Tooltip from '../tooltip'; @autobind class WorkspaceDropdown extends PureComponent { constructor (props) { super(props); this.state = { loggedIn: false }; } async _handleDropdownOpen () { if (this.state.loggedIn !== session.isLoggedIn()) { this.setState({loggedIn: session.isLoggedIn()}); } } 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}); } } } _handleShowLogin () { showModal(LoginModal); } _handleShowExport () { showModal(SettingsModal, TAB_INDEX_EXPORT); } _handleShowSettings () { showModal(SettingsModal); } _handleShowWorkspaceSettings () { showModal(WorkspaceSettingsModal, { workspace: this.props.activeWorkspace }); } _handleShowShareSettings () { showModal(WorkspaceShareSettingsModal, { workspace: this.props.activeWorkspace }); } _handleSwitchWorkspace (workspaceId) { this.props.handleSetActiveWorkspace(workspaceId); trackEvent('Workspace', 'Switch'); } _handleWorkspaceCreate (noTrack) { showPrompt({ title: 'Create New Workspace', defaultValue: 'My Workspace', submitName: 'Create', selectText: true, onComplete: async name => { const workspace = await models.workspace.create({name}); this.props.handleSetActiveWorkspace(workspace._id); if (!noTrack) { trackEvent('Workspace', 'Create'); } } }); } render () { const { className, workspaces, activeWorkspace, unseenWorkspaces, isLoading, ...other } = this.props; const nonActiveWorkspaces = workspaces.filter(w => w._id !== activeWorkspace._id); const addedWorkspaceNames = unseenWorkspaces.map(w => `"${w.name}"`).join(', '); const classes = classnames(className, 'wide', 'workspace-dropdown'); const unseenWorkspacesMessage = (
The following workspaces were added
{addedWorkspaceNames}
); return (

{isLoading ? : null} {unseenWorkspaces.length > 0 && ( )}
{activeWorkspace.name}

{activeWorkspace.name} Workspace Settings Share {activeWorkspace.name} Switch Workspace {nonActiveWorkspaces.map(w => { const isUnseen = !!unseenWorkspaces.find(v => v._id === w._id); return ( To {w.name} {isUnseen && ( )} ); })} New Workspace Insomnia Version {getAppVersion()} Preferences Import/Export {/* Not Logged In */} {!this.state.loggedIn && ( Log In )} {!this.state.loggedIn && ( Upgrade to Plus )}
); } } WorkspaceDropdown.propTypes = { // Required isLoading: PropTypes.bool.isRequired, handleImportFile: PropTypes.func.isRequired, handleExportFile: PropTypes.func.isRequired, handleSetActiveWorkspace: PropTypes.func.isRequired, workspaces: PropTypes.arrayOf(PropTypes.object).isRequired, unseenWorkspaces: PropTypes.arrayOf(PropTypes.object).isRequired, activeWorkspace: PropTypes.object.isRequired, // Optional className: PropTypes.string }; export default WorkspaceDropdown;