insomnia/app/components/ResponsePane.js

172 lines
5.1 KiB
JavaScript
Raw Normal View History

import React, {PropTypes, Component} from 'react';
import {Tab, Tabs, TabList, TabPanel} from 'react-tabs'
import StatusTag from './StatusTag';
import SizeTag from './SizeTag';
import TimeTag from './TimeTag';
import PreviewModeDropdown from './PreviewModeDropdown';
import ResponseBodyViewer from './ResponseBodyViewer';
import ResponseHeadersViewer from './ResponseHeadersViewer';
import {getPreviewModeName} from '../lib/previewModes';
import {PREVIEW_MODE_SOURCE} from '../lib/previewModes';
import {REQUEST_TIME_TO_SHOW_COUNTER} from '../lib/constants';
import {MOD_SYM} from '../lib/constants';
2016-06-19 00:08:14 +00:00
class ResponsePane extends Component {
render () {
2016-07-07 20:10:55 +00:00
const {
response,
request,
previewMode,
updatePreviewMode,
2016-07-19 22:28:29 +00:00
loadingRequests,
editorLineWrapping,
editorFontSize
2016-07-07 20:10:55 +00:00
} = this.props;
2016-07-07 22:06:18 +00:00
const loadStartTime = loadingRequests[request ? request._id : '__NONE__'];
2016-07-07 20:10:55 +00:00
let timer = null;
if (loadStartTime) {
// Set a timer to update the UI again soon
setTimeout(() => {
this.forceUpdate();
}, 30);
2016-07-07 20:39:24 +00:00
// NOTE: We subtract 200ms because the request has some time padding on either end
2016-07-07 20:10:55 +00:00
const elapsedTime = Math.round((Date.now() - loadStartTime - 200) / 100) / 10;
timer = (
<div className="response-pane__overlay">
{elapsedTime > REQUEST_TIME_TO_SHOW_COUNTER ? (
<h2>{elapsedTime} seconds...</h2>
) : (
<h2>Loading...</h2>
)}
<i className="fa fa-refresh fa-spin"></i>
{false && elapsedTime > REQUEST_TIME_TO_SHOW_COUNTER ? (
// TODO: implement cancel requests
<button className="btn btn--compact bg-danger">Cancel Request</button>
) : null}
</div>
)
}
if (!request) {
return (
<section className="response-pane pane">
<header className="pane__header"></header>
<div className="pane__body pane__body--placeholder text-center pad"></div>
</section>
)
}
2016-06-19 00:08:14 +00:00
if (!response) {
return (
<section className="response-pane pane">
2016-07-07 20:10:55 +00:00
{timer}
2016-06-19 00:08:14 +00:00
<header className="pane__header"></header>
<div className="pane__body pane__body--placeholder">
<table>
<tbody>
<tr>
<td>Send Request</td>
<td><code>{MOD_SYM}Enter</code></td>
</tr>
<tr>
<td>Switch Requests</td>
<td><code>{MOD_SYM}P</code></td>
</tr>
<tr>
<td>Focus Url Bar</td>
<td><code>{MOD_SYM}L</code></td>
</tr>
</tbody>
</table>
2016-06-19 00:08:14 +00:00
</div>
</section>
)
}
return (
2016-05-01 19:56:30 +00:00
<section className="response-pane pane">
2016-07-07 20:10:55 +00:00
{timer}
2016-06-19 00:27:21 +00:00
<header className="pane__header pad no-wrap">
2016-06-19 00:08:14 +00:00
{!response ? null : (
<div>
<StatusTag
statusCode={response.statusCode}
statusMessage={response.statusMessage}
/>
<TimeTag milliseconds={response.elapsedTime}/>
<SizeTag bytes={response.bytesRead}/>
2016-06-19 00:08:14 +00:00
</div>
)}
</header>
<Tabs className="pane__body">
<TabList>
<Tab>
<button>{getPreviewModeName(previewMode)}</button>
<PreviewModeDropdown
previewMode={previewMode}
updatePreviewMode={updatePreviewMode}
/>
</Tab>
2016-06-19 00:17:20 +00:00
<Tab>
<button>
Headers {response.headers.length ? (
<span className="txt-sm">({response.headers.length})</span> ) : null}
</button>
</Tab>
2016-06-19 00:08:14 +00:00
</TabList>
<TabPanel>
{response.error ? (
<ResponseBodyViewer
2016-06-19 00:08:14 +00:00
contentType={response.contentType}
previewMode={PREVIEW_MODE_SOURCE}
2016-07-19 22:28:29 +00:00
editorLineWrapping={editorLineWrapping}
editorFontSize={editorFontSize}
2016-06-19 00:08:14 +00:00
body={response.error}
2016-06-19 07:21:43 +00:00
url={response.url}
2016-06-19 00:08:14 +00:00
/>
) : (
<ResponseBodyViewer
2016-06-19 00:08:14 +00:00
contentType={response.contentType}
previewMode={previewMode}
2016-07-19 22:28:29 +00:00
editorLineWrapping={editorLineWrapping}
editorFontSize={editorFontSize}
2016-06-19 00:08:14 +00:00
body={response.body}
2016-06-19 07:21:43 +00:00
url={response.url}
2016-06-19 00:08:14 +00:00
wrap={true} // TODO: Make this a user preference
/>
)}
</TabPanel>
<TabPanel className="scrollable pad">
<ResponseHeadersViewer headers={response.headers}/>
2016-06-19 00:08:14 +00:00
</TabPanel>
</Tabs>
</section>
)
}
2016-06-19 00:08:14 +00:00
}
ResponsePane.propTypes = {
2016-04-30 05:01:57 +00:00
// Functions
updatePreviewMode: PropTypes.func.isRequired,
// Required
previewMode: PropTypes.string.isRequired,
loadingRequests: PropTypes.object.isRequired,
2016-07-19 22:28:29 +00:00
editorFontSize: PropTypes.number.isRequired,
editorLineWrapping: PropTypes.bool.isRequired,
2016-04-30 05:01:57 +00:00
// Other
2016-07-07 20:10:55 +00:00
response: PropTypes.object,
2016-07-19 22:28:29 +00:00
request: PropTypes.object
};
export default ResponsePane;