insomnia/app/components/ResponsePane.js

156 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-06-19 00:08:14 +00:00
import React, {PropTypes, Component} from 'react'
import {Tab, Tabs, TabList, TabPanel} from 'react-tabs'
2016-06-15 03:20:05 +00:00
import StatusTag from './StatusTag'
import SizeTag from './SizeTag'
import TimeTag from './TimeTag'
2016-04-30 05:01:57 +00:00
import PreviewModeDropdown from '../components/PreviewModeDropdown'
import ResponseViewer from '../components/ResponseViewer'
import {getPreviewModeName} from '../lib/previewModes'
2016-07-07 20:10:55 +00:00
import {PREVIEW_MODE_SOURCE} from '../lib/previewModes'
import {REQUEST_TIME_TO_SHOW_COUNTER} 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,
loadingRequests
} = 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>
)
}
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>
2016-06-19 00:40:14 +00:00
<div className="pane__body pane__body--placeholder text-center pad">
2016-06-19 00:08:14 +00:00
<h1>Nothing Yet...</h1>
<p>Click the <em>Send</em> button to trigger a request</p>
</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.millis}/>
<SizeTag bytes={response.bytes}/>
</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 ? (
<ResponseViewer
contentType={response.contentType}
previewMode={PREVIEW_MODE_SOURCE}
body={response.error}
2016-06-19 07:21:43 +00:00
url={response.url}
2016-06-19 00:08:14 +00:00
wrap={true}
/>
) : (
<ResponseViewer
contentType={response.contentType}
previewMode={previewMode}
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">
<table className="wide">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{response.headers.map((h, i) => (
<tr className="selectable" key={i}>
<td>{h.name}</td>
<td>{h.value}</td>
</tr>
))}
</tbody>
</table>
</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-04-30 05:01:57 +00:00
// Other
2016-07-07 20:10:55 +00:00
response: PropTypes.object,
2016-07-07 22:06:18 +00:00
request: PropTypes.object,
};
export default ResponsePane;