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

307 lines
8.8 KiB
JavaScript
Raw Normal View History

import React, {PropTypes, Component} from 'react';
2016-07-20 18:35:08 +00:00
import ReactDOM from 'react-dom';
import classnames from 'classnames';
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 Component {
2016-07-20 18:35:08 +00:00
constructor (props) {
super(props);
this.state = {
searchString: '',
matchedRequests: [],
matchedWorkspaces: [],
2016-07-20 18:35:08 +00:00
requestGroups: [],
activeIndex: -1
}
}
_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) {
if (!request) {
return;
}
this.props.activateRequest(request);
this.modal.hide();
2016-07-20 18:35:08 +00:00
}
async _handleChange (searchString) {
const {workspaceId} = this.props;
2016-11-10 01:15:27 +00:00
const allRequests = await models.request.all();
const allRequestGroups = await models.requestGroup.all();
const allWorkspaces = await models.workspace.all();
// TODO: Support nested RequestGroups
// Filter out RequestGroups that don't belong to this Workspace
const requestGroups = allRequestGroups.filter(
rg => rg.parentId === workspaceId
);
// Filter out Requests that don't belong to this Workspace
const requests = allRequests.filter(r => {
if (r.parentId === workspaceId) {
return true;
} else {
return !!requestGroups.find(rg => rg._id === r.parentId);
}
});
const parentId = this.props.activeRequestParentId;
2016-07-20 18:35:08 +00:00
// OPTIMIZATION: This only filters if we have a filter
let matchedRequests = requests;
if (searchString) {
matchedRequests = matchedRequests.filter(r => {
const name = r.name.toLowerCase();
const toMatch = searchString.toLowerCase();
return name.indexOf(toMatch) !== -1
});
}
// OPTIMIZATION: Apply sort after the filter so we have to sort less
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 of string 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 === parentId) {
return -1;
} else if (b.parentId === parentId) {
return 1;
} else {
return a.parentId > b.parentId ? -1 : 1;
2016-07-20 18:35:08 +00:00
}
}
}
);
let matchedWorkspaces = [];
if (searchString) {
// Only match workspaces if there is a search
matchedWorkspaces = allWorkspaces
.filter(w => w._id !== workspaceId)
.filter(w => {
const name = w.name.toLowerCase();
const toMatch = searchString.toLowerCase();
return name.indexOf(toMatch) !== -1
});
}
const activeIndex = searchString ? 0 : -1;
2016-07-20 18:35:08 +00:00
this.setState({
activeIndex,
matchedRequests,
matchedWorkspaces,
requestGroups,
searchString
2016-07-20 18:35:08 +00:00
});
}
show () {
this.modal.show();
this._handleChange('');
2016-07-20 18:35:08 +00:00
}
toggle () {
this.modal.toggle();
2016-07-20 18:35:08 +00:00
this._handleChange('');
}
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 {
matchedRequests,
matchedWorkspaces,
requestGroups,
searchString,
activeIndex
} = this.state;
2016-07-20 18:35:08 +00:00
return (
<Modal ref={m => this.modal = m} top={true}
dontFocus={true} {...this.props}>
2016-07-20 18:35:08 +00:00
<ModalHeader hideCloseButton={true}>
<p className="pull-right txt-md">
<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
</p>
<p>Quick Switch</p>
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={n => n && n.focus()}
value={searchString}
2016-07-20 18:35:08 +00:00
onChange={e => this._handleChange(e.target.value)}
/>
</div>
<ul className="pad-top">
{matchedRequests.map((r, i) => {
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={e => this._activateRequest(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}
<MethodTag method={r.method}/>
<strong>{r.name}</strong>
</button>
</li>
)
})}
{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={e => this._activateRequest(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 ? (
2016-07-20 18:35:08 +00:00
<div className="text-center">
<p>
No matches found for <strong>{searchString}</strong>
</p>
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
2016-07-20 18:35:08 +00:00
<button className="btn btn--outlined btn--compact"
onClick={e => 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,
activeRequestParentId: PropTypes.string.isRequired
2016-07-20 18:35:08 +00:00
};
export default RequestSwitcherModal;