Skip to content

Commit

Permalink
Add approximate size to titlebar of responses and redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiefOfGxBxL committed Nov 3, 2024
1 parent f0c81f0 commit 244dc0b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
11 changes: 9 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 @@ -36,6 +39,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 +51,7 @@ function Redirect(props) {
method={method}
statusCode={statusCode}
url={url}
size={contentSize}
time={time}
onClick={setExpanded}
/>
Expand Down
9 changes: 6 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 Down Expand Up @@ -82,7 +85,7 @@ 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'
}

0 comments on commit 244dc0b

Please sign in to comment.