insomnia/app/ui/components/viewers/ResponseViewer.js

191 lines
5.0 KiB
JavaScript
Raw Normal View History

import React, {Component, PropTypes} from 'react';
import Editor from '../base/Editor';
import ResponseWebview from './ResponseWebview';
2016-09-10 02:51:24 +00:00
import ResponseRaw from './ResponseRaw';
2016-08-25 17:06:01 +00:00
import ResponseError from './ResponseError';
2016-08-31 05:37:21 +00:00
import {
PREVIEW_MODE_FRIENDLY,
PREVIEW_MODE_SOURCE
} from '../../../backend/previewModes';
2016-10-02 22:10:43 +00:00
import {LARGE_RESPONSE_MB} from '../../../backend/constants';
let alwaysShowLargeResponses = false;
2016-04-30 05:01:57 +00:00
class ResponseViewer extends Component {
2016-10-02 22:10:43 +00:00
constructor (props) {
super(props);
this.state = {
blockingBecauseTooLarge: false
};
};
_handleDismissBlocker () {
this.setState({blockingBecauseTooLarge: false});
}
_handleDisableBlocker () {
alwaysShowLargeResponses = true;
this._handleDismissBlocker();
}
_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,
2016-09-09 00:32:36 +00:00
updateFilter,
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) {
2016-10-26 23:22:15 +00:00
return <ResponseError url={url} error={bodyBuffer.toString('utf8')}/>
2016-10-02 22:10:43 +00:00
}
const {blockingBecauseTooLarge} = this.state;
if (blockingBecauseTooLarge) {
2016-08-25 17:06:01 +00:00
return (
2016-10-02 22:10:43 +00:00
<div className="response-pane__overlay response-pane__overlay--under">
<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={e => this._handleDismissBlocker()}
className="inline-block btn btn--super-compact btn--outlined">
Show Response
</button>
{" "}
<button className="faint inline-block btn btn--super-compact"
onClick={e => this._handleDisableBlocker()}>
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',
}}/>
</div>
</div>
)
} else {
return (
<ResponseWebview
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
2016-10-26 23:22:15 +00:00
value={body}
2016-09-09 00:32:36 +00:00
updateFilter={updateFilter}
filter={filter}
2016-09-13 17:29:09 +00:00
autoPrettify={true}
mode={mode}
2016-05-01 19:56:30 +00:00
readOnly={true}
2016-07-19 22:28:29 +00:00
lineWrapping={editorLineWrapping}
fontSize={editorFontSize}
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,
editorLineWrapping: PropTypes.bool.isRequired,
url: PropTypes.string.isRequired,
2016-10-02 22:10:43 +00:00
bytes: PropTypes.number.isRequired,
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;