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 6f1ce10 commit a21c6d2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
12 changes: 9 additions & 3 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 @@ -35,11 +38,14 @@ 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
expanded={isExpanded}
header={<Titlebar method={method} statusCode={statusCode} url={url} time={time} onClick={setExpanded} />}
header={<Titlebar method={method} statusCode={statusCode} url={url} size={contentSize} time={time} onClick={setExpanded} />}
>
<Headers expanded headers={headers} />
</StyledResponse>
Expand Down
11 changes: 7 additions & 4 deletions src/components/Response/Response.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { Alert, Label } from 'react-bootstrap';
import { Alert } from 'react-bootstrap';
import Highlight from 'react-highlight';
import formatXml from 'xml-formatter';

Expand All @@ -14,8 +14,9 @@ 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 }) {
let labelStyle = 'default'
if (statusCode >= 200 && statusCode < 300) labelStyle = 'success'
else if (statusCode >= 400 && statusCode < 600) labelStyle = 'danger'
Expand All @@ -24,7 +25,9 @@ function Titlebar({ url, time, statusCode }) {
<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 @@ -86,7 +89,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 a21c6d2

Please sign in to comment.