2017-02-28 21:32:23 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2017-02-08 00:31:48 +00:00
|
|
|
import {shell} from 'electron';
|
2017-03-08 05:52:17 +00:00
|
|
|
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
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class ResponseViewer extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
blockingBecauseTooLarge: false
|
|
|
|
};
|
|
|
|
}
|
2016-10-02 22:10:43 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleOpenLink (link) {
|
2017-02-08 00:31:48 +00:00
|
|
|
shell.openExternal(link);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-02-08 00:31:48 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleDismissBlocker () {
|
2016-10-02 22:10:43 +00:00
|
|
|
this.setState({blockingBecauseTooLarge: false});
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2016-10-02 22:10:43 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleDisableBlocker () {
|
2016-10-02 22:10:43 +00:00
|
|
|
alwaysShowLargeResponses = true;
|
|
|
|
this._handleDismissBlocker();
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
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) {
|
2016-08-04 04:12:45 +00:00
|
|
|
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) {
|
2016-08-04 04:12:45 +00:00
|
|
|
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,
|
2016-11-29 20:55:31 +00:00
|
|
|
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) {
|
2016-11-29 20:55:31 +00:00
|
|
|
return (
|
|
|
|
<ResponseError
|
|
|
|
url={url}
|
|
|
|
error={bodyBuffer.toString('utf8')}
|
2017-02-09 00:46:12 +00:00
|
|
|
fontSize={editorFontSize}
|
2016-11-29 20:55:31 +00:00
|
|
|
statusCode={statusCode}
|
|
|
|
/>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-10-02 22:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const {blockingBecauseTooLarge} = this.state;
|
|
|
|
if (blockingBecauseTooLarge) {
|
2016-08-25 17:06:01 +00:00
|
|
|
return (
|
2017-01-27 01:00:27 +00:00
|
|
|
<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>
|
2017-03-01 00:10:23 +00:00
|
|
|
<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>
|
2017-03-03 20:09:08 +00:00
|
|
|
{' '}
|
2016-10-02 22:10:43 +00:00
|
|
|
<button className="faint inline-block btn btn--super-compact"
|
2017-03-01 00:10:23 +00:00
|
|
|
onClick={this._handleDisableBlocker}>
|
2016-10-02 22:10:43 +00:00
|
|
|
Always Show
|
|
|
|
</button>
|
|
|
|
</p>
|
|
|
|
</div>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
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"
|
2017-02-09 00:46:12 +00:00
|
|
|
style={{maxWidth: '100%', maxHeight: '100%', margin: 'auto'}}/>
|
2016-10-26 23:22:15 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
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:
|
2016-09-28 20:37:53 +00:00
|
|
|
let mode = contentType;
|
2016-10-26 23:22:15 +00:00
|
|
|
const body = bodyBuffer.toString('utf8');
|
2016-09-28 20:37:53 +00:00
|
|
|
|
|
|
|
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
|
2017-02-08 00:31:48 +00:00
|
|
|
onClickLink={this._handleOpenLink}
|
2017-03-01 21:15:56 +00:00
|
|
|
defaultValue={body}
|
2016-09-09 00:32:36 +00:00
|
|
|
updateFilter={updateFilter}
|
|
|
|
filter={filter}
|
2017-03-01 21:15:56 +00:00
|
|
|
autoPrettify
|
|
|
|
noMatchBrackets
|
|
|
|
readOnly
|
2016-09-28 20:37:53 +00:00
|
|
|
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}
|
|
|
|
/>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-04-30 05:01:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:04:36 +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,
|
2016-07-28 20:10:26 +00:00
|
|
|
url: PropTypes.string.isRequired,
|
2016-10-02 22:10:43 +00:00
|
|
|
bytes: PropTypes.number.isRequired,
|
2016-11-29 20:55:31 +00:00
|
|
|
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,
|
2016-07-28 20:10:26 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
};
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
export default ResponseViewer;
|