import React, {PropTypes, Component} from 'react'; import fs from 'fs'; import mime from 'mime-types'; import {remote} from 'electron'; import {Tab, Tabs, TabList, TabPanel} from 'react-tabs'; import SizeTag from './tags/SizeTag'; import StatusTag from './tags/StatusTag'; import TimeTag from './tags/TimeTag'; import PreviewModeDropdown from './dropdowns/PreviewModeDropdown'; import ResponseViewer from './viewers/ResponseViewer'; import ResponseHeadersViewer from './viewers/ResponseHeadersViewer'; import ResponseCookiesViewer from './viewers/ResponseCookiesViewer'; import * as models from '../../models'; import {REQUEST_TIME_TO_SHOW_COUNTER, MOD_SYM, PREVIEW_MODE_SOURCE, getPreviewModeName} from '../../common/constants'; import {getSetCookieHeaders} from '../../common/misc'; import {cancelCurrentRequest} from '../../common/network'; import {trackEvent} from '../../analytics'; class ResponsePane extends Component { constructor (props) { super(props); this.state = { response: null } } async _getResponse (request) { if (!request) { this.setState({response: null}); } else { const response = await models.response.getLatestByParentId(request._id); this.setState({response}); } } async _handleDownloadResponseBody () { if (!this.state.response) { // Should never happen console.warn('No response to download'); return; } const {body, encoding, contentType} = this.state.response; const bodyBuffer = new Buffer(body, encoding); const extension = mime.extension(contentType) || ''; const options = { title: 'Save Response', buttonLabel: 'Save', filters: [{ name: 'Download', extensions: [extension], }] }; remote.dialog.showSaveDialog(options, filename => { if (!filename) { trackEvent('Response', 'Save', 'Cancel'); return; } fs.writeFile(filename, bodyBuffer, {}, err => { if (err) { console.warn('Failed to save response body', err); trackEvent('Response', 'Save', 'Failure'); } else { trackEvent('Response', 'Save', 'Success'); } }); }); } componentWillReceiveProps (nextProps) { this._getResponse(nextProps.request); } componentDidMount () { this._getResponse(this.props.request); } render () { const { request, previewMode, updatePreviewMode, updateResponseFilter, loadingRequests, editorLineWrapping, editorFontSize, responseFilter, showCookiesModal } = this.props; const {response} = this.state; const loadStartTime = loadingRequests[request ? request._id : '__NONE__']; let timer = null; if (loadStartTime) { // Set a timer to update the UI again soon // TODO: Move this into a child component so we don't rerender too much setTimeout(() => { this.forceUpdate(); }, 100); // NOTE: subtract 200ms because the request has some time on either end const millis = Date.now() - loadStartTime - 200; const elapsedTime = Math.round(millis / 100) / 10; timer = (
{elapsedTime > REQUEST_TIME_TO_SHOW_COUNTER ? (

{elapsedTime} seconds...

) : (

Loading...

)}

) } if (!request) { return (
) } if (!response) { return (
{timer}
Send Request {MOD_SYM}Enter
Focus Url Bar {MOD_SYM}L
Manage Cookies {MOD_SYM}K
Edit Environments {MOD_SYM}E
) } const cookieHeaders = getSetCookieHeaders(response.headers); return (
{timer} {!response ? null : (
)}
) } } ResponsePane.propTypes = { // Functions updatePreviewMode: PropTypes.func.isRequired, updateResponseFilter: PropTypes.func.isRequired, showCookiesModal: PropTypes.func.isRequired, // Required previewMode: PropTypes.string.isRequired, responseFilter: PropTypes.string.isRequired, loadingRequests: PropTypes.object.isRequired, editorFontSize: PropTypes.number.isRequired, editorLineWrapping: PropTypes.bool.isRequired, // Other request: PropTypes.object }; export default ResponsePane;