Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add approximate size to titlebar of responses and redirects #269

Open
wants to merge 4 commits into
base: add-status-chips-to-response-titlebars
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/components/Response/Redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import responsePropTypes, { redirectShape } from 'propTypes/redirect';
import { StyledResponse, StyledHeader } from './StyledComponents';
import Headers from './Headers';
import StatusChip from './StatusChip';
import SizeBytes from './SizeBytes';

function Titlebar({ url, time, statusCode, onClick }) {
function Titlebar({ url, time, size, statusCode, onClick }) {
return (
<StyledHeader expandable onClick={onClick}>
<h3>
<StatusChip statusCode={statusCode} />
Redirect ({(time / 1000).toFixed(3)}s)
<span>Redirect</span>
<SizeBytes size={size} />
<span>({(time / 1000).toFixed(3)}s)</span>
<a href={url} className="text-muted">{url}</a>
</h3>
</StyledHeader>
Expand All @@ -20,6 +23,7 @@ function Titlebar({ url, time, statusCode, onClick }) {
Titlebar.propTypes = {
url: redirectShape.url,
time: redirectShape.time,
size: PropTypes.number.isRequired,
statusCode: PropTypes.number.isRequired,
onClick: PropTypes.func.isRequired,
};
Expand All @@ -36,6 +40,9 @@ function Redirect(props) {

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

const contentLength = headers.find(header => (header.name.toLowerCase() === 'content-length'));
const contentSize = contentLength ? Number(contentLength.value) : 0;

return (
<StyledResponse
collapsible
Expand All @@ -45,6 +52,7 @@ function Redirect(props) {
method={method}
statusCode={statusCode}
url={url}
size={contentSize}
time={time}
onClick={setExpanded}
/>
Expand Down
18 changes: 15 additions & 3 deletions src/components/Response/Response.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ import { StyledResponse, StyledHeader } from './StyledComponents';
import Headers from './Headers';
import RenderedResponse from './RenderedResponse';
import StatusChip from './StatusChip';
import SizeBytes from './SizeBytes';

function Titlebar({ url, time, statusCode }) {
function Titlebar({ url, time, size, statusCode }) {
return (
<StyledHeader>
<h3>
<StatusChip statusCode={statusCode} />
Response ({time})
<span>Response</span>
<SizeBytes size={size} />
<span>({time})</span>
<a href={url} className="text-muted">{url}</a>
</h3>
</StyledHeader>
Expand All @@ -30,6 +33,7 @@ function Titlebar({ url, time, statusCode }) {
Titlebar.propTypes = {
url: responseShape.url,
time: PropTypes.node.isRequired,
size: PropTypes.number.isRequired,
statusCode: PropTypes.number.isRequired,
};

Expand Down Expand Up @@ -82,7 +86,15 @@ export function Response(props) {
return (
<StyledResponse
wrapResponse={wrapResponse}
header={<Titlebar statusCode={status} method={method} url={url} time={time} />}
header={(
<Titlebar
statusCode={status}
method={method}
url={url}
size={contentSize}
time={time}
/>
)}
>
<Headers headers={interceptedResponse.responseHeaders} />
{type.html && <RenderedResponse html={body} />}
Expand Down
17 changes: 17 additions & 0 deletions src/components/Response/SizeBytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, { PropTypes } from 'react';

import byteFormatter from 'utils/byteFormatter';

function SizeBytes({ size }) {
return (
<span title={`${size} bytes`}>
{byteFormatter(size)}
</span>
);
}

SizeBytes.propTypes = {
size: PropTypes.number.isRequired,
};

export default SizeBytes;
14 changes: 14 additions & 0 deletions src/utils/byteFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const KB = 1024;
const MB = 1024 * KB;
const GB = 1024 * MB;

/*
* Formats the provided number in bytes as a string of bytes,
* KB, MB, or GB, with one decimal point and the unit suffix.
*/
export default function byteFormatter(bytes) {
if (bytes < 100) return `${bytes} bytes`;
if (bytes > GB) return `${(bytes / GB).toFixed(1)} GB`;
if (bytes > MB) return `${(bytes / MB).toFixed(1)} MB`;
return `${(bytes / KB).toFixed(1)} KB`;
}
16 changes: 13 additions & 3 deletions test/components/response/__snapshots__/response.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@ exports[`response component renders a result when given one 1`] = `
>
200
</span>
Response (
0.123s
)
<span>
Response
</span>
<span
title="14 bytes"
>
14 bytes
</span>
<span>
(
0.123s
)
</span>
<a
className="text-muted"
href="http://example.com"
Expand Down
Loading