import React, {PropTypes, PureComponent} from 'react';
import iconv from 'iconv-lite';
import autobind from 'autobind-decorator';
import {shell} from 'electron';
import {SimplePDF} from 'simple-react-pdf';
import CodeEditor from '../codemirror/code-editor';
import ResponseWebView from './response-webview';
import ResponseRaw from './response-raw';
import ResponseError from './response-error';
import {LARGE_RESPONSE_MB, PREVIEW_MODE_FRIENDLY, PREVIEW_MODE_RAW} from '../../../common/constants';
let alwaysShowLargeResponses = false;
@autobind
class ResponseViewer extends PureComponent {
constructor (props) {
super(props);
this.state = {
blockingBecauseTooLarge: false,
bodyBuffer: null
};
}
_handleOpenLink (link) {
shell.openExternal(link);
}
_handleDismissBlocker () {
this.setState({blockingBecauseTooLarge: false});
}
_handleDisableBlocker () {
alwaysShowLargeResponses = true;
this._handleDismissBlocker();
}
_maybeLoadResponseBody (props) {
// Block the response if it's too large
const responseIsTooLarge = props.bytes > LARGE_RESPONSE_MB * 1024 * 1024;
if (!alwaysShowLargeResponses && responseIsTooLarge) {
this.setState({blockingBecauseTooLarge: true});
} else {
this.setState({
blockingBecauseTooLarge: false,
bodyBuffer: props.getBody()
});
}
}
componentWillMount () {
this._maybeLoadResponseBody(this.props);
}
componentWillReceiveProps (nextProps) {
this._maybeLoadResponseBody(nextProps);
}
shouldComponentUpdate (nextProps, nextState) {
for (let k of Object.keys(nextProps)) {
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;
}
render () {
const {
previewMode,
filter,
filterHistory,
contentType,
editorLineWrapping,
editorFontSize,
editorIndentSize,
editorKeyMap,
updateFilter,
url,
error
} = this.props;
const {bodyBuffer} = this.state;
if (error) {
return (
Response body over {LARGE_RESPONSE_MB}MB hidden to prevent unresponsiveness
{' '}