insomnia/app/ui/components/response-pane.js

358 lines
11 KiB
JavaScript
Raw Normal View History

import React, {PropTypes, PureComponent} from 'react';
import autobind from 'autobind-decorator';
2016-10-28 17:51:08 +00:00
import fs from 'fs';
import mime from 'mime-types';
import {remote} from 'electron';
import {Tab, TabList, TabPanel, Tabs} from 'react-tabs';
import SizeTag from './tags/size-tag';
import StatusTag from './tags/status-tag';
import TimeTag from './tags/time-tag';
import Button from './base/button';
import PreviewModeDropdown from './dropdowns/preview-mode-dropdown';
import ResponseViewer from './viewers/response-viewer';
import ResponseHistoryDropdown from './dropdowns/response-history-dropdown';
import ResponseTimer from './response-timer';
import ResponseTimelineViewer from './viewers/response-timeline-viewer';
import ResponseHeadersViewer from './viewers/response-headers-viewer';
import ResponseCookiesViewer from './viewers/response-cookies-viewer';
2016-11-10 05:56:23 +00:00
import * as models from '../../models';
2017-06-16 22:21:41 +00:00
import {PREVIEW_MODE_SOURCE} from '../../common/constants';
import {getSetCookieHeaders, nullFn} from '../../common/misc';
import {cancelCurrentRequest} from '../../network/network';
import {trackEvent} from '../../analytics';
import Hotkey from './hotkey';
2016-09-17 05:46:44 +00:00
@autobind
class ResponsePane extends PureComponent {
constructor (props) {
super(props);
this.state = {
response: null
};
}
2016-09-17 05:46:44 +00:00
_trackTab (name) {
trackEvent('Response Pane', 'View', name);
}
async _getResponse (requestId, responseId) {
let response = responseId ? await models.response.getById(responseId) : null;
if (!response) {
response = await models.response.getLatestByParentId(requestId);
}
this.setState({response});
2016-09-17 05:46:44 +00:00
}
async _handleDownloadResponseBody () {
2016-10-28 17:51:08 +00:00
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 Body',
2016-10-28 17:51:08 +00:00
buttonLabel: 'Save',
filters: [{
name: 'Download', extensions: [extension]
2016-10-28 17:51:08 +00:00
}]
};
remote.dialog.showSaveDialog(options, filename => {
if (!filename) {
trackEvent('Response', 'Save Cancel');
2016-10-28 17:51:08 +00:00
return;
}
fs.writeFile(filename, bodyBuffer, {}, err => {
if (err) {
console.warn('Failed to save response body', err);
trackEvent('Response', 'Save Failure');
2016-10-28 17:51:08 +00:00
} else {
trackEvent('Response', 'Save Success');
2016-10-28 17:51:08 +00:00
}
});
});
}
2016-10-28 17:51:08 +00:00
async _handleDownloadFullResponseBody () {
if (!this.state.response) {
// Should never happen
console.warn('No response to download');
return;
}
const {body, timeline, encoding} = this.state.response;
const headers = timeline
.filter(v => v.name === 'HEADER_IN')
.map(v => v.value)
.join('');
const bodyBuffer = new Buffer(body, encoding);
const fullResponse = `${headers}${bodyBuffer}`;
const options = {
title: 'Save Full Response',
buttonLabel: 'Save',
filters: [{
name: 'Download'
}]
};
remote.dialog.showSaveDialog(options, filename => {
if (!filename) {
trackEvent('Response', 'Save Full Cancel');
return;
}
fs.writeFile(filename, fullResponse, {}, err => {
if (err) {
console.warn('Failed to save full response', err);
trackEvent('Response', 'Save Full Failure');
} else {
trackEvent('Response', 'Save Full Success');
}
});
});
}
2016-09-17 05:46:44 +00:00
componentWillReceiveProps (nextProps) {
const activeRequestId = nextProps.request ? nextProps.request._id : null;
const activeResponseId = nextProps.activeResponseId;
this._getResponse(activeRequestId, activeResponseId);
2016-09-17 05:46:44 +00:00
}
componentDidMount () {
const activeRequestId = this.props.request ? this.props.request._id : null;
const activeResponseId = this.props.activeResponseId;
this._getResponse(activeRequestId, activeResponseId);
2016-09-17 05:46:44 +00:00
}
render () {
const {
request,
previewMode,
handleShowRequestSettings,
handleSetPreviewMode,
handleSetActiveResponse,
handleDeleteResponses,
2017-06-12 21:48:17 +00:00
handleDeleteResponse,
handleSetFilter,
loadStartTime,
2016-09-17 05:46:44 +00:00
editorLineWrapping,
editorFontSize,
editorIndentSize,
2017-01-24 22:18:11 +00:00
editorKeyMap,
filter,
filterHistory,
activeResponseId,
2016-09-17 05:46:44 +00:00
showCookiesModal
} = this.props;
const {response} = this.state;
if (!request) {
return (
2016-09-20 21:17:01 +00:00
<section className="response-pane pane">
<header className="pane__header"></header>
<div className="pane__body pane__body--placeholder"></div>
</section>
);
2016-09-17 05:46:44 +00:00
}
if (!response) {
return (
2016-09-20 21:17:01 +00:00
<section className="response-pane pane">
<header className="pane__header"></header>
<div className="pane__body pane__body--placeholder">
<div>
<table className="table--fancy">
2016-09-20 21:17:01 +00:00
<tbody>
<tr>
<td>Send Request</td>
<td className="text-right">
<code><Hotkey char="Enter"/></code>
2016-09-20 21:17:01 +00:00
</td>
</tr>
<tr>
<td>Focus Url Bar</td>
<td className="text-right">
<code><Hotkey char="L"/></code>
2016-09-20 21:17:01 +00:00
</td>
</tr>
<tr>
<td>Manage Cookies</td>
<td className="text-right">
<code><Hotkey char="K"/></code>
2016-09-20 21:17:01 +00:00
</td>
</tr>
<tr>
<td>Edit Environments</td>
<td className="text-right">
<code><Hotkey char="E"/></code>
2016-09-20 21:17:01 +00:00
</td>
</tr>
</tbody>
</table>
2016-09-17 05:46:44 +00:00
</div>
2016-09-20 21:17:01 +00:00
</div>
<ResponseTimer
handleCancel={cancelCurrentRequest}
loadStartTime={loadStartTime}
/>
2016-09-20 21:17:01 +00:00
</section>
);
2016-09-17 05:46:44 +00:00
}
const cookieHeaders = getSetCookieHeaders(response.headers);
return (
2016-09-20 21:17:01 +00:00
<section className="response-pane pane">
{!response ? null : (
<header className="pane__header row-spaced">
<div className="no-wrap scrollable scrollable--no-bars pad-left">
<StatusTag
statusCode={response.statusCode}
statusMessage={response.statusMessage || null}
/>
<TimeTag milliseconds={response.elapsedTime}/>
<SizeTag bytes={response.bytesRead}/>
</div>
<ResponseHistoryDropdown
requestId={request._id}
isLatestResponseActive={!activeResponseId}
activeResponseId={response._id}
handleSetActiveResponse={handleSetActiveResponse}
handleDeleteResponses={handleDeleteResponses}
2017-06-12 21:48:17 +00:00
handleDeleteResponse={handleDeleteResponse}
onChange={nullFn}
className="tall pane__header__right"
right
/>
</header>
)}
<Tabs className="pane__body" forceRenderTabPanel>
2016-09-20 21:17:01 +00:00
<TabList>
<Tab>
2016-09-20 21:17:01 +00:00
<PreviewModeDropdown
download={this._handleDownloadResponseBody}
fullDownload={this._handleDownloadFullResponseBody}
2016-09-20 21:17:01 +00:00
previewMode={previewMode}
updatePreviewMode={handleSetPreviewMode}
2016-09-20 21:17:01 +00:00
/>
</Tab>
<Tab>
<Button onClick={this._trackTab} value="Headers">
Header
{' '}
{response.headers.length > 0 && (
<span className="bubble">{response.headers.length}</span>
)}
</Button>
2016-09-20 21:17:01 +00:00
</Tab>
<Tab>
<Button onClick={this._trackTab} value="Cookies">
Cookie {cookieHeaders.length ? (
<span className="bubble">{cookieHeaders.length}</span>) : null}
</Button>
2016-09-20 21:17:01 +00:00
</Tab>
{(response.timeline && response.timeline.length > 0) && (
<Tab>
<Button onClick={this._trackTab} value="Timeline">Timeline</Button>
</Tab>
)}
2016-09-20 21:17:01 +00:00
</TabList>
<TabPanel>
<ResponseViewer
key={response._id}
2016-10-02 22:10:43 +00:00
bytes={response.bytesRead}
2016-10-27 16:45:44 +00:00
contentType={response.contentType || ''}
2016-09-20 21:17:01 +00:00
previewMode={response.error ? PREVIEW_MODE_SOURCE : previewMode}
filter={filter}
filterHistory={filterHistory}
updateFilter={response.error ? null : handleSetFilter}
2016-09-20 21:17:01 +00:00
body={response.error ? response.error : response.body}
2016-10-26 23:22:15 +00:00
encoding={response.encoding}
2016-09-20 21:17:01 +00:00
error={!!response.error}
2016-10-02 22:10:43 +00:00
responseId={response._id}
2016-09-20 21:17:01 +00:00
editorLineWrapping={editorLineWrapping}
editorFontSize={editorFontSize}
editorIndentSize={editorIndentSize}
2017-01-24 22:18:11 +00:00
editorKeyMap={editorKeyMap}
2016-09-20 21:17:01 +00:00
url={response.url}
/>
</TabPanel>
2016-09-21 00:03:26 +00:00
<TabPanel className="scrollable-container">
<div className="scrollable pad">
<ResponseHeadersViewer
2016-09-21 00:03:26 +00:00
key={response._id}
headers={response.headers}
2016-09-21 00:03:26 +00:00
/>
</div>
2016-09-20 21:17:01 +00:00
</TabPanel>
2016-09-21 00:03:26 +00:00
<TabPanel className="scrollable-container">
<div className="scrollable pad">
<ResponseCookiesViewer
handleShowRequestSettings={handleShowRequestSettings}
cookiesSent={response.settingSendCookies}
cookiesStored={response.settingStoreCookies}
showCookiesModal={showCookiesModal}
2016-09-21 00:03:26 +00:00
key={response._id}
headers={cookieHeaders}
2016-09-21 00:03:26 +00:00
/>
</div>
2016-09-20 21:17:01 +00:00
</TabPanel>
{(response.timeline && response.timeline.length > 0) && (
<TabPanel>
<ResponseTimelineViewer
key={response._id}
timeline={response.timeline || []}
editorLineWrapping={editorLineWrapping}
editorFontSize={editorFontSize}
editorIndentSize={editorIndentSize}
/>
</TabPanel>
)}
2016-09-20 21:17:01 +00:00
</Tabs>
<ResponseTimer
handleCancel={cancelCurrentRequest}
loadStartTime={loadStartTime}
/>
2016-09-20 21:17:01 +00:00
</section>
);
2016-09-17 05:46:44 +00:00
}
}
ResponsePane.propTypes = {
// Functions
handleSetFilter: PropTypes.func.isRequired,
2016-09-17 05:46:44 +00:00
showCookiesModal: PropTypes.func.isRequired,
handleSetPreviewMode: PropTypes.func.isRequired,
handleSetActiveResponse: PropTypes.func.isRequired,
handleDeleteResponses: PropTypes.func.isRequired,
2017-06-12 21:48:17 +00:00
handleDeleteResponse: PropTypes.func.isRequired,
handleShowRequestSettings: PropTypes.func.isRequired,
2016-09-17 05:46:44 +00:00
// Required
previewMode: PropTypes.string.isRequired,
filter: PropTypes.string.isRequired,
filterHistory: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
2016-09-17 05:46:44 +00:00
editorFontSize: PropTypes.number.isRequired,
editorIndentSize: PropTypes.number.isRequired,
2017-01-24 22:18:11 +00:00
editorKeyMap: PropTypes.string.isRequired,
2016-09-17 05:46:44 +00:00
editorLineWrapping: PropTypes.bool.isRequired,
loadStartTime: PropTypes.number.isRequired,
activeResponseId: PropTypes.string.isRequired,
2016-09-17 05:46:44 +00:00
// Other
request: PropTypes.object
2016-09-17 05:46:44 +00:00
};
export default ResponsePane;