insomnia/app/ui/components/modals/RequestSwitcherModal.js

289 lines
8.6 KiB
JavaScript
Raw Normal View History

import React, {PropTypes, PureComponent} from 'react';
2016-07-20 18:35:08 +00:00
import ReactDOM from 'react-dom';
import classnames from 'classnames';
import Button from '../base/Button';
import Modal from '../base/Modal';
import ModalHeader from '../base/ModalHeader';
import ModalBody from '../base/ModalBody';
import MethodTag from '../tags/MethodTag';
2016-11-10 05:56:23 +00:00
import * as models from '../../../models';
2016-07-20 18:35:08 +00:00
class RequestSwitcherModal extends PureComponent {
2016-11-26 07:33:55 +00:00
state = {
searchString: '',
requestGroups: [],
requests: [],
workspaces: [],
matchedRequests: [],
matchedWorkspaces: [],
activeIndex: -1
};
2016-07-20 18:35:08 +00:00
_setModalRef = n => this.modal = n;
_focusRef = n => n && n.focus();
2016-07-20 18:35:08 +00:00
_setActiveIndex (activeIndex) {
const maxIndex = this.state.matchedRequests.length + this.state.matchedWorkspaces.length;
2016-07-20 18:35:08 +00:00
if (activeIndex < 0) {
activeIndex = this.state.matchedRequests.length - 1;
} else if (activeIndex >= maxIndex) {
2016-07-20 18:35:08 +00:00
activeIndex = 0;
}
this.setState({activeIndex});
}
_activateCurrentIndex = () => {
const {
activeIndex,
matchedRequests,
matchedWorkspaces
} = this.state;
if (activeIndex < matchedRequests.length) {
2016-07-20 18:35:08 +00:00
// Activate the request if there is one
const request = matchedRequests[activeIndex];
this._activateRequest(request);
} else if (activeIndex < (matchedRequests.length + matchedWorkspaces.length)) {
// Activate the workspace if there is one
const index = activeIndex - matchedRequests.length;
const workspace = matchedWorkspaces[index];
this._activateWorkspace(workspace);
2016-07-20 18:35:08 +00:00
} else {
// Create request if no match
this._createRequestFromSearch();
}
};
2016-07-20 18:35:08 +00:00
async _createRequestFromSearch () {
const {activeRequestParentId} = this.props;
const {searchString} = this.state;
// Create the request if nothing matched
const patch = {
name: searchString,
parentId: activeRequestParentId
};
2016-11-10 01:15:27 +00:00
const request = await models.request.create(patch);
this._activateRequest(request);
}
_activateWorkspace = workspace => {
if (!workspace) {
return;
2016-07-20 18:35:08 +00:00
}
this.props.handleSetActiveWorkspace(workspace._id);
this.modal.hide();
};
2016-07-20 18:35:08 +00:00
_activateRequest = request => {
2016-07-20 18:35:08 +00:00
if (!request) {
return;
}
2016-11-25 23:09:17 +00:00
this.props.activateRequest(request._id);
this.modal.hide();
};
_handleChange = e => this._handleChangeValue(e.target.value);
2016-07-20 18:35:08 +00:00
_handleChangeValue = async searchString => {
const {workspaceChildren, workspaces} = this.props;
2016-11-26 07:33:55 +00:00
const {workspaceId, activeRequestParentId} = this.props;
// OPTIMIZATION: This only filters if we have a filter
let matchedRequests = workspaceChildren.filter(d => d.type === models.request.type);
if (searchString) {
matchedRequests = matchedRequests.filter(r => {
const name = r.name.toLowerCase();
2017-02-26 23:47:45 +00:00
const id = r._id.toLowerCase();
const toMatch = searchString.toLowerCase();
2017-02-26 23:47:45 +00:00
// Match substring of name
const matchesName = name.indexOf(toMatch) >= 0;
// Match exact Id
const matchesId = id === toMatch;
return matchesName || matchesId;
});
}
matchedRequests = matchedRequests.sort((a, b) => {
if (a.parentId === b.parentId) {
// Sort Requests by name inside of the same parent
// TODO: Sort by quality of match (eg. start vs mid string, etc)
return a.name > b.name ? 1 : -1;
} else {
// Sort RequestGroups by relevance if Request isn't in same parent
if (a.parentId === activeRequestParentId) {
return -1;
} else if (b.parentId === activeRequestParentId) {
return 1;
} else {
return a.parentId > b.parentId ? -1 : 1;
2016-07-20 18:35:08 +00:00
}
}
}).slice(0, 20); // show 20 max
const matchedWorkspaces = workspaces
.filter(w => w._id !== workspaceId)
.filter(w => {
const name = w.name.toLowerCase();
const toMatch = searchString.toLowerCase();
2017-02-26 23:47:45 +00:00
return name.indexOf(toMatch) !== -1;
});
const activeIndex = searchString ? 0 : -1;
2016-07-20 18:35:08 +00:00
this.setState({
activeIndex,
2016-11-26 07:33:55 +00:00
searchString,
matchedRequests,
matchedWorkspaces,
2016-07-20 18:35:08 +00:00
});
};
2016-07-20 18:35:08 +00:00
2016-11-26 07:33:55 +00:00
async show () {
this.modal.show();
this._handleChangeValue('');
2016-07-20 18:35:08 +00:00
}
2016-11-26 07:33:55 +00:00
async toggle () {
this.modal.toggle();
this._handleChangeValue('');
2016-07-20 18:35:08 +00:00
}
componentDidMount () {
ReactDOM.findDOMNode(this).addEventListener('keydown', e => {
2016-07-20 18:35:08 +00:00
const keyCode = e.keyCode;
if (keyCode === 38 || (keyCode === 9 && e.shiftKey)) {
// Up or Shift+Tab
this._setActiveIndex(this.state.activeIndex - 1);
} else if (keyCode === 40 || keyCode === 9) {
// Down or Tab
this._setActiveIndex(this.state.activeIndex + 1);
} else if (keyCode === 13) {
// Enter
this._activateCurrentIndex();
} else {
return;
}
e.preventDefault();
})
}
render () {
const {
searchString,
2016-11-26 07:33:55 +00:00
activeIndex,
matchedRequests,
matchedWorkspaces
} = this.state;
2016-07-20 18:35:08 +00:00
2016-11-26 07:33:55 +00:00
const {workspaceChildren} = this.props;
const requestGroups = workspaceChildren.filter(d => d.type === models.requestGroup.type);
return (
<Modal ref={this._setModalRef} top={true} dontFocus={true}>
2016-07-20 18:35:08 +00:00
<ModalHeader hideCloseButton={true}>
<div className="pull-right txt-md pad-right">
2016-07-20 18:35:08 +00:00
<span className="monospace">tab</span> or
&nbsp;
<span className="monospace"> </span> &nbsp;to navigate
&nbsp;&nbsp;&nbsp;
<span className="monospace"></span> &nbsp;to select
&nbsp;&nbsp;&nbsp;
<span className="monospace">esc</span> to dismiss
</div>
<div>Quick Switch</div>
2016-07-20 18:35:08 +00:00
</ModalHeader>
<ModalBody className="pad request-switcher">
<div className="form-control form-control--outlined no-margin">
<input
type="text"
ref={this._focusRef}
value={searchString}
onChange={this._handleChange}
2016-07-20 18:35:08 +00:00
/>
</div>
<ul className="pad-top">
{matchedRequests.map((r, i) => {
2016-11-26 07:33:55 +00:00
const requestGroup = requestGroups.find(rg => rg._id === r.parentId);
const buttonClasses = classnames(
'btn btn--compact wide text-left',
{focus: activeIndex === i}
);
2016-07-20 18:35:08 +00:00
return (
<li key={r._id}>
<Button onClick={this._activateRequest} value={r} className={buttonClasses}>
2016-07-20 18:35:08 +00:00
{requestGroup ? (
<div className="pull-right faint italic">
{requestGroup.name}
&nbsp;&nbsp;
<i className="fa fa-folder-o"></i>
</div>
) : null}
2016-07-20 18:35:08 +00:00
<MethodTag method={r.method}/>
<strong>{r.name}</strong>
</Button>
2016-07-20 18:35:08 +00:00
</li>
)
})}
2016-11-26 07:33:55 +00:00
{matchedRequests.length && matchedWorkspaces.length ? <hr/> : null}
{matchedWorkspaces.map((w, i) => {
const buttonClasses = classnames(
'btn btn--compact wide text-left',
{focus: (activeIndex - matchedRequests.length) === i}
);
return (
<li key={w._id}>
<Button onClick={this._activateWorkspace} value={w} className={buttonClasses}>
2016-09-10 18:42:23 +00:00
<i className="fa fa-random"></i>
&nbsp;&nbsp;&nbsp;
Switch to <strong>{w.name}</strong>
</Button>
</li>
)
})}
2016-07-20 18:35:08 +00:00
</ul>
{!matchedRequests.length && !matchedWorkspaces.length ? (
<div className="text-center">
<p>
No matches found for <strong>{searchString}</strong>
</p>
<button className="btn btn--outlined btn--compact"
disabled={!searchString}
onClick={this._activateCurrentIndex}>
Create a request named {searchString}
</button>
</div>
) : null}
</ModalBody>
</Modal>
);
}
}
2016-07-20 18:35:08 +00:00
RequestSwitcherModal.propTypes = {
handleSetActiveWorkspace: PropTypes.func.isRequired,
2016-07-20 18:35:08 +00:00
activateRequest: PropTypes.func.isRequired,
workspaceId: PropTypes.string.isRequired,
2016-11-26 07:33:55 +00:00
activeRequestParentId: PropTypes.string.isRequired,
workspaceChildren: PropTypes.arrayOf(PropTypes.object).isRequired,
workspaces: PropTypes.arrayOf(PropTypes.object).isRequired,
2016-07-20 18:35:08 +00:00
};
export default RequestSwitcherModal;