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

166 lines
4.1 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-07-19 22:28:29 +00:00
body,
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-08-25 17:06:01 +00:00
if (error) {
2016-10-02 22:10:43 +00:00
return <ResponseError url={url} error={body}/>
}
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">
Warning! Previewing responses over {LARGE_RESPONSE_MB}MB may cause
slowdowns on some computers.
</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:
return (
<ResponseWebview
2016-05-01 19:56:30 +00:00
body={body}
contentType={contentType}
2016-06-19 07:21:43 +00:00
url={url}
2016-05-01 19:56:30 +00:00
/>
2016-04-30 05:01:57 +00:00
);
case PREVIEW_MODE_SOURCE:
let mode = contentType;
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
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-09-21 01:25:46 +00:00
<ResponseRaw value={body} 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-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,
// Optional
2016-09-09 00:32:36 +00:00
updateFilter: PropTypes.func,
2016-08-25 17:06:01 +00:00
contentType: PropTypes.string,
error: PropTypes.bool
2016-04-30 05:01:57 +00:00
};
export default ResponseViewer;