insomnia/app/ui/components/viewers/response-viewer.js

204 lines
5.3 KiB
JavaScript
Raw Normal View History

import React, {PureComponent, PropTypes} from 'react';
import autobind from 'autobind-decorator';
import {shell} from 'electron';
import Editor from '../codemirror/editor';
import ResponseWebView from './response-webview';
import ResponseRaw from './response-raw';
import ResponseError from './response-error';
2016-11-10 02:40:53 +00:00
import {LARGE_RESPONSE_MB, PREVIEW_MODE_FRIENDLY, PREVIEW_MODE_SOURCE} from '../../../common/constants';
2016-10-02 22:10:43 +00:00
let alwaysShowLargeResponses = false;
2016-04-30 05:01:57 +00:00
@autobind
class ResponseViewer extends PureComponent {
constructor (props) {
super(props);
this.state = {
blockingBecauseTooLarge: false
};
}
2016-10-02 22:10:43 +00:00
_handleOpenLink (link) {
shell.openExternal(link);
}
_handleDismissBlocker () {
2016-10-02 22:10:43 +00:00
this.setState({blockingBecauseTooLarge: false});
}
2016-10-02 22:10:43 +00:00
_handleDisableBlocker () {
2016-10-02 22:10:43 +00:00
alwaysShowLargeResponses = true;
this._handleDismissBlocker();
}
2016-10-02 22:10:43 +00:00
_checkResponseBlocker (props) {
if (alwaysShowLargeResponses) {
return;
}
// Block the response if it's too large
if (props.bytes > LARGE_RESPONSE_MB * 1024 * 1024) {
this.setState({blockingBecauseTooLarge: true});
}
}
componentWillMount () {
this._checkResponseBlocker(this.props);
}
componentWillReceiveProps (nextProps) {
this._checkResponseBlocker(nextProps);
}
shouldComponentUpdate (nextProps, nextState) {
for (let k of Object.keys(nextProps)) {
2016-10-02 22:10:43 +00:00
const value = nextProps[k];
if (typeof value !== 'function' && this.props[k] !== value) {
return true;
}
}
for (let k of Object.keys(nextState)) {
const value = nextState[k];
if (typeof value !== 'function' && this.state[k] !== value) {
return true;
}
}
return false;
}
2016-04-30 05:01:57 +00:00
render () {
2016-07-19 22:28:29 +00:00
const {
previewMode,
2016-09-09 00:32:36 +00:00
filter,
2016-07-19 22:28:29 +00:00
contentType,
editorLineWrapping,
editorFontSize,
2017-01-24 22:18:11 +00:00
editorKeyMap,
2016-09-09 00:32:36 +00:00
updateFilter,
statusCode,
2016-10-26 23:22:15 +00:00
body: base64Body,
encoding,
2016-08-25 17:06:01 +00:00
url,
error
2016-07-19 22:28:29 +00:00
} = this.props;
2016-04-30 05:01:57 +00:00
2016-10-26 23:22:15 +00:00
const bodyBuffer = new Buffer(base64Body, encoding);
2016-08-25 17:06:01 +00:00
if (error) {
return (
<ResponseError
url={url}
error={bodyBuffer.toString('utf8')}
fontSize={editorFontSize}
statusCode={statusCode}
/>
);
2016-10-02 22:10:43 +00:00
}
const {blockingBecauseTooLarge} = this.state;
if (blockingBecauseTooLarge) {
2016-08-25 17:06:01 +00:00
return (
<div className="response-pane__notify">
2016-10-02 22:10:43 +00:00
<p className="pad faint">
2016-10-02 22:17:55 +00:00
Previewing responses over {LARGE_RESPONSE_MB}MB may cause
slowdowns on some computers
2016-10-02 22:10:43 +00:00
</p>
<p>
<button onClick={this._handleDismissBlocker}
2016-11-26 00:49:38 +00:00
className="inline-block btn btn--clicky">
2016-10-02 22:10:43 +00:00
Show Response
</button>
{' '}
2016-10-02 22:10:43 +00:00
<button className="faint inline-block btn btn--super-compact"
onClick={this._handleDisableBlocker}>
2016-10-02 22:10:43 +00:00
Always Show
</button>
</p>
</div>
);
2016-08-25 17:06:01 +00:00
}
2016-04-30 05:01:57 +00:00
switch (previewMode) {
case PREVIEW_MODE_FRIENDLY:
2016-10-26 23:22:15 +00:00
if (contentType.toLowerCase().indexOf('image/') === 0) {
const justContentType = contentType.split(';')[0];
return (
<div className="scrollable-container tall wide">
<div className="scrollable">
<img src={`data:${justContentType};base64,${base64Body}`}
className="pad block"
style={{maxWidth: '100%', maxHeight: '100%', margin: 'auto'}}/>
2016-10-26 23:22:15 +00:00
</div>
</div>
);
2016-10-26 23:22:15 +00:00
} else {
return (
2016-11-10 02:40:53 +00:00
<ResponseWebView
2016-10-26 23:22:15 +00:00
body={bodyBuffer.toString('utf8')}
contentType={contentType}
url={url}
/>
);
}
2016-04-30 05:01:57 +00:00
case PREVIEW_MODE_SOURCE:
let mode = contentType;
2016-10-26 23:22:15 +00:00
const body = bodyBuffer.toString('utf8');
try {
// FEATURE: Detect JSON even without content-type
contentType.indexOf('json') === -1 && JSON.parse(body);
mode = 'application/json';
} catch (e) {
// Nothing
}
2016-04-30 05:01:57 +00:00
return (
2016-05-01 19:56:30 +00:00
<Editor
onClickLink={this._handleOpenLink}
defaultValue={body}
2016-09-09 00:32:36 +00:00
updateFilter={updateFilter}
filter={filter}
autoPrettify
noMatchBrackets
readOnly
mode={mode}
2016-07-19 22:28:29 +00:00
lineWrapping={editorLineWrapping}
fontSize={editorFontSize}
2017-01-24 22:18:11 +00:00
keyMap={editorKeyMap}
2016-08-31 05:37:21 +00:00
placeholder="..."
2016-05-01 19:56:30 +00:00
/>
2016-04-30 05:01:57 +00:00
);
default: // Raw
return (
2016-10-26 23:22:15 +00:00
<ResponseRaw
value={bodyBuffer.toString('utf8')}
fontSize={editorFontSize}
/>
);
2016-04-30 05:01:57 +00:00
}
}
}
ResponseViewer.propTypes = {
2016-04-30 05:01:57 +00:00
body: PropTypes.string.isRequired,
2016-10-26 23:22:15 +00:00
encoding: PropTypes.string.isRequired,
2016-06-18 21:02:27 +00:00
previewMode: PropTypes.string.isRequired,
2016-09-09 00:32:36 +00:00
filter: PropTypes.string.isRequired,
2016-07-19 22:28:29 +00:00
editorFontSize: PropTypes.number.isRequired,
2017-01-24 22:18:11 +00:00
editorKeyMap: PropTypes.string.isRequired,
2016-07-19 22:28:29 +00:00
editorLineWrapping: PropTypes.bool.isRequired,
url: PropTypes.string.isRequired,
2016-10-02 22:10:43 +00:00
bytes: PropTypes.number.isRequired,
statusCode: PropTypes.number.isRequired,
2016-10-02 22:10:43 +00:00
responseId: PropTypes.string.isRequired,
2016-10-27 16:45:44 +00:00
contentType: PropTypes.string.isRequired,
// Optional
2016-09-09 00:32:36 +00:00
updateFilter: PropTypes.func,
2016-08-25 17:06:01 +00:00
error: PropTypes.bool
2016-04-30 05:01:57 +00:00
};
export default ResponseViewer;