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

176 lines
5.4 KiB
JavaScript
Raw Normal View History

2016-11-17 18:45:54 +00:00
import React, {Component, PropTypes} from 'react';
import HTTPSnippet, {availableTargets} from 'httpsnippet';
import CopyButton from '../base/CopyButton';
2016-11-11 22:01:21 +00:00
import {Dropdown, DropdownButton, DropdownItem} from '../base/dropdown';
import Editor from '../codemirror/Editor';
import Modal from '../base/Modal';
import ModalBody from '../base/ModalBody';
import ModalHeader from '../base/ModalHeader';
import ModalFooter from '../base/ModalFooter';
import {exportHar} from '../../../common/har';
2016-11-23 20:44:46 +00:00
import {trackEvent} from '../../../analytics/index';
const DEFAULT_TARGET = availableTargets().find(t => t.key === 'shell');
const DEFAULT_CLIENT = DEFAULT_TARGET.clients.find(t => t.key === 'curl');
const MODE_MAP = {
c: 'clike',
java: 'clike',
csharp: 'clike',
node: 'javascript',
objc: 'clike',
ocaml: 'mllike'
};
2016-08-15 22:31:30 +00:00
const TO_ADD_CONTENT_LENGTH = {
node: ['native']
};
class GenerateCodeModal extends Component {
constructor (props) {
super(props);
let client;
let target;
// Load preferences from localStorage
try {
target = JSON.parse(localStorage.getItem('insomnia::generateCode::target'));
} catch (e) {
}
try {
client = JSON.parse(localStorage.getItem('insomnia::generateCode::client'));
} catch (e) {
}
this.state = {
cmd: '',
request: null,
2016-11-25 23:09:17 +00:00
target: target || DEFAULT_TARGET,
client: client || DEFAULT_CLIENT,
};
}
_handleClientChange (client) {
const {target, request} = this.state;
this._generateCode(request, target, client);
2016-11-23 20:44:46 +00:00
trackEvent('Generate Code', 'Client Change', `${target.title}/${client.title}`);
}
_handleTargetChange (target) {
const {target: currentTarget} = this.state;
if (currentTarget.key === target.key) {
// No change
return;
}
const client = target.clients.find(c => c.key === target.default);
this._generateCode(this.state.request, target, client);
2016-11-23 20:44:46 +00:00
trackEvent('Generate Code', 'Target Change', target.title);
}
async _generateCode (request, target, client) {
2016-08-15 22:31:30 +00:00
// Some clients need a content-length for the request to succeed
const addContentLength = (TO_ADD_CONTENT_LENGTH[target.key] || []).find(c => c === client.key);
2016-11-17 18:45:54 +00:00
const {environmentId} = this.props;
const har = await exportHar(request._id, environmentId, addContentLength);
const snippet = new HTTPSnippet(har);
const cmd = snippet.convert(target.key, client.key);
this.setState({request, cmd, client, target});
// Save client/target for next time
localStorage.setItem('insomnia::generateCode::client', JSON.stringify(client));
localStorage.setItem('insomnia::generateCode::target', JSON.stringify(target));
}
show (request) {
const {client, target} = this.state;
this._generateCode(request, target, client);
this.modal.show();
}
render () {
const {cmd, target, client} = this.state;
2017-01-24 22:18:11 +00:00
const {editorFontSize, editorKeyMap} = this.props;
const targets = availableTargets();
// NOTE: Just some extra precautions in case the target is messed up
let clients = [];
if (target && Array.isArray(target.clients)) {
clients = target.clients;
}
return (
<Modal ref={m => this.modal = m} tall={true} {...this.props}>
<ModalHeader>Generate Client Code</ModalHeader>
<ModalBody noScroll={true} style={{
display: 'grid',
2016-09-20 20:53:34 +00:00
gridTemplateColumns: 'minmax(0, 1fr)',
gridTemplateRows: 'auto minmax(0, 1fr)'
}}>
<div className="pad">
<Dropdown outline={true}>
2016-11-26 00:49:38 +00:00
<DropdownButton className="btn btn--clicky">
2016-11-25 23:09:17 +00:00
{target ? target.title : 'n/a'}
<i className="fa fa-caret-down"></i>
2016-11-11 22:01:21 +00:00
</DropdownButton>
{targets.map(target => (
<DropdownItem key={target.key} onClick={() => this._handleTargetChange(target)}>
{target.title}
</DropdownItem>
))}
</Dropdown>
&nbsp;&nbsp;
<Dropdown outline={true}>
2016-11-26 00:49:38 +00:00
<DropdownButton className="btn btn--clicky">
2016-11-25 23:09:17 +00:00
{client ? client.title : 'n/a'}
<i className="fa fa-caret-down"></i>
2016-11-11 23:06:24 +00:00
</DropdownButton>
{clients.map(client => (
2016-11-11 23:06:24 +00:00
<DropdownItem key={client.key} onClick={() => this._handleClientChange(client)}>
{client.title}
</DropdownItem>
))}
</Dropdown>
&nbsp;&nbsp;
<CopyButton content={cmd}
2016-11-26 00:49:38 +00:00
className="pull-right btn btn--clicky"/>
</div>
<Editor
className="border-top"
key={Date.now()}
mode={MODE_MAP[target.key] || target.key}
ref={n => this._editor = n}
2017-01-23 22:41:31 +00:00
fontSize={editorFontSize}
2017-01-24 22:18:11 +00:00
keyMap={editorKeyMap}
lightTheme={true}
lineWrapping={true}
value={cmd}
/>
</ModalBody>
<ModalFooter>
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
<div className="margin-left faint italic txt-sm tall">
* copy/paste this command into a Unix terminal
</div>
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
<button className="btn" onClick={e => this.modal.hide()}>
Done
</button>
</ModalFooter>
</Modal>
);
}
}
2016-11-17 18:45:54 +00:00
GenerateCodeModal.propTypes = {
environmentId: PropTypes.string.isRequired,
2017-01-23 22:41:31 +00:00
editorFontSize: PropTypes.number.isRequired,
2017-01-24 22:18:11 +00:00
editorKeyMap: PropTypes.string.isRequired,
2016-11-17 18:45:54 +00:00
};
export default GenerateCodeModal;