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

342 lines
10 KiB
JavaScript
Raw Normal View History

2017-07-17 23:32:47 +00:00
// @flow
2017-07-18 22:10:57 +00:00
import type {Request} from '../../models/request';
import type {Response} from '../../models/response';
2017-07-17 23:32:47 +00:00
import React, {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 {
2017-07-17 23:32:47 +00:00
props: {
// Functions
handleSetFilter: Function,
showCookiesModal: Function,
handleSetPreviewMode: Function,
handleSetActiveResponse: Function,
handleDeleteResponses: Function,
handleDeleteResponse: Function,
handleShowRequestSettings: Function,
// Required
previewMode: string,
filter: string,
filterHistory: Array<string>,
editorFontSize: number,
editorIndentSize: number,
editorKeyMap: string,
editorLineWrapping: boolean,
loadStartTime: number,
responses: Array<Object>,
// Other
2017-07-19 04:48:28 +00:00
request: ?Request,
response: ?Response
2017-07-17 23:32:47 +00:00
};
_trackTab (name: string) {
trackEvent('Response Pane', 'View', name);
}
2017-07-18 22:10:57 +00:00
_handleGetResponseBody (): Buffer | null {
if (!this.props.response) {
return null;
}
2017-07-17 18:20:38 +00:00
return models.response.getBodyBuffer(this.props.response);
}
async _handleDownloadResponseBody () {
2017-07-17 18:20:38 +00:00
if (!this.props.response) {
2016-10-28 17:51:08 +00:00
// Should never happen
console.warn('No response to download');
return;
}
2017-07-17 22:58:41 +00:00
const {response} = this.props;
const {contentType} = response;
2016-10-28 17:51:08 +00:00
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, outputPath => {
if (!outputPath) {
trackEvent('Response', 'Save Cancel');
2016-10-28 17:51:08 +00:00
return;
}
const bodyBuffer = models.response.getBodyBuffer(response);
2017-07-18 22:10:57 +00:00
if (bodyBuffer) {
fs.writeFile(outputPath, bodyBuffer, err => {
if (err) {
console.warn('Failed to save response body', err);
trackEvent('Response', 'Save Failure');
} else {
trackEvent('Response', 'Save Success');
}
});
}
2016-10-28 17:51:08 +00:00
});
}
2016-10-28 17:51:08 +00:00
_handleDownloadFullResponseBody () {
2017-07-17 22:58:41 +00:00
const {response} = this.props;
if (!response) {
// Should never happen
console.warn('No response to download');
return;
}
const headers = response.timeline
.filter(v => v.name === 'HEADER_IN')
.map(v => v.value)
.join('');
2017-07-18 22:10:57 +00:00
const bodyBuffer = models.response.getBodyBuffer(response) || Buffer.from('');
const fullResponse = `${headers}${bodyBuffer.toString()}`;
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
render () {
const {
request,
2017-07-17 18:20:38 +00:00
responses,
response,
2016-09-17 05:46:44 +00:00
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,
2016-09-17 05:46:44 +00:00
showCookiesModal
} = this.props;
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
2017-07-17 18:20:38 +00:00
activeResponse={response}
responses={responses}
requestId={request._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>
<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}
bodyPath={response.bodyPath}
getBody={this._handleGetResponseBody}
error={response.error}
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>
<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
}
}
export default ResponsePane;