Skip to content

Commit 244dc0b

Browse files
committed
Add approximate size to titlebar of responses and redirects
1 parent f0c81f0 commit 244dc0b

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

src/components/Response/Redirect.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import responsePropTypes, { redirectShape } from 'propTypes/redirect';
44
import { StyledResponse, StyledHeader } from './StyledComponents';
55
import Headers from './Headers';
66
import StatusChip from './StatusChip';
7+
import SizeBytes from './SizeBytes';
78

8-
function Titlebar({ url, time, statusCode, onClick }) {
9+
function Titlebar({ url, time, size, statusCode, onClick }) {
910
return (
1011
<StyledHeader expandable onClick={onClick}>
1112
<h3>
1213
<StatusChip statusCode={statusCode} />
13-
Redirect ({(time / 1000).toFixed(3)}s)
14+
<span>Redirect</span>
15+
<SizeBytes size={size} />
16+
<span>({(time / 1000).toFixed(3)}s)</span>
1417
<a href={url} className="text-muted">{url}</a>
1518
</h3>
1619
</StyledHeader>
@@ -36,6 +39,9 @@ function Redirect(props) {
3639

3740
const { method, url, time, statusCode } = response;
3841

42+
const contentLength = headers.find((header) => header.name.toLowerCase() === 'content-length')
43+
const contentSize = contentLength ? Number(contentLength.value) : 0
44+
3945
return (
4046
<StyledResponse
4147
collapsible
@@ -45,6 +51,7 @@ function Redirect(props) {
4551
method={method}
4652
statusCode={statusCode}
4753
url={url}
54+
size={contentSize}
4855
time={time}
4956
onClick={setExpanded}
5057
/>

src/components/Response/Response.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ import { StyledResponse, StyledHeader } from './StyledComponents';
1414
import Headers from './Headers';
1515
import RenderedResponse from './RenderedResponse';
1616
import StatusChip from './StatusChip';
17+
import SizeBytes from './SizeBytes';
1718

18-
function Titlebar({ url, time, statusCode }) {
19+
function Titlebar({ url, time, size, statusCode }) {
1920
return (
2021
<StyledHeader>
2122
<h3>
2223
<StatusChip statusCode={statusCode} />
23-
Response ({time})
24+
<span>Response</span>
25+
<SizeBytes size={size} />
26+
<span>({time})</span>
2427
<a href={url} className="text-muted">{url}</a>
2528
</h3>
2629
</StyledHeader>
@@ -82,7 +85,7 @@ export function Response(props) {
8285
return (
8386
<StyledResponse
8487
wrapResponse={wrapResponse}
85-
header={<Titlebar statusCode={status} method={method} url={url} time={time} />}
88+
header={<Titlebar statusCode={status} method={method} url={url} size={contentSize} time={time} />}
8689
>
8790
<Headers headers={interceptedResponse.responseHeaders} />
8891
{type.html && <RenderedResponse html={body} />}

src/components/Response/SizeBytes.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React, { PropTypes } from 'react';
2+
3+
import byteFormatter from 'utils/byteFormatter';
4+
5+
function SizeBytes({ size }) {
6+
return (
7+
<span title={`${size} bytes`}>
8+
{byteFormatter(size)}
9+
</span>
10+
);
11+
}
12+
13+
SizeBytes.propTypes = {
14+
size: PropTypes.number.isRequired
15+
};
16+
17+
export default SizeBytes;

src/utils/byteFormatter.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const KB = 1024
2+
const MB = 1024 * KB
3+
const GB = 1024 * MB
4+
5+
/*
6+
* Formats the provided number in bytes as a string of bytes,
7+
* KB, MB, or GB, with one decimal point and the unit suffix.
8+
*/
9+
export default function byteFormatter(bytes) {
10+
if (bytes < 100) return `${bytes} bytes`
11+
if (bytes > GB) return (bytes / GB).toFixed(1) + ' GB'
12+
if (bytes > MB) return (bytes / MB).toFixed(1) + ' MB'
13+
return (bytes / KB).toFixed(1) + ' KB'
14+
}

0 commit comments

Comments
 (0)