From 871a85d9343cf49646d13dc455c062efadcb959a Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sat, 12 Feb 2022 11:33:52 +0100 Subject: Beatified code --- src/components/Content/Content.tsx | 167 +++++++++++++++++++++---------------- 1 file changed, 94 insertions(+), 73 deletions(-) (limited to 'src/components/Content/Content.tsx') diff --git a/src/components/Content/Content.tsx b/src/components/Content/Content.tsx index c7a5f58..56a802f 100644 --- a/src/components/Content/Content.tsx +++ b/src/components/Content/Content.tsx @@ -1,120 +1,141 @@ import * as React from "react"; -import {Dispatch, SetStateAction, useContext, useMemo} from "react"; -import {RequestPayload, ResponsePayload, Headers} from "~hooks/useRequests"; -import ReactJson from 'react-json-view'; -import styles from './Content.module.scss'; -import {Button, Col, Container, Row} from "react-bootstrap"; -import {DarkModeContext} from "../../contexts/DarkMode"; +import { Dispatch, SetStateAction, useContext, useMemo } from "react"; +import { RequestPayload, ResponsePayload, Headers } from "~hooks/useRequests"; +import ReactJson from "react-json-view"; +import styles from "./Content.module.scss"; +import { Button, Col, Container, Row } from "react-bootstrap"; +import { DarkModeContext } from "../../contexts/DarkMode"; -function getHeader(headers: Headers, key: string, unit?: string): string | null { +function getHeader( + headers: Headers, + key: string, + unit?: string +): string | null { try { - const [_, value] = headers.find(([headerKey]) => headerKey.toLowerCase() === key.toLowerCase()) - return unit !== undefined - ? `${value}${unit}` - : value + const [_, value] = headers.find( + ([headerKey]) => headerKey.toLowerCase() === key.toLowerCase() + ); + return unit !== undefined ? `${value}${unit}` : value; } catch { return null; } } interface ContentProps { - data: RequestPayload | ResponsePayload - setRaw: Dispatch> - raw?: boolean + data: RequestPayload | ResponsePayload; + setRaw: Dispatch>; + raw?: boolean; } -export default function Content({ raw, setRaw, data }: ContentProps): JSX.Element { +export default function Content({ + raw, + setRaw, + data, +}: ContentProps): JSX.Element { return (
- - setRaw(!raw)}/> - - + + setRaw(!raw)} + /> + + - { - [ - getHeader(data.headers, 'content-length', 'bytes'), - getHeader(data.headers, 'content-type'), - ].filter(x => x !== null).join('; ') - } + {[ + getHeader(data.headers, "content-length", "bytes"), + getHeader(data.headers, "content-type"), + ] + .filter((x) => x !== null) + .join("; ")} {(() => { try { - return ContentBody({ data, raw }) + return ContentBody({ data, raw }); } catch { return (

Body could not be rendered

- +
- ) + ); } })()}
-
- ) -}; - -function ContentBody({ data, raw = false }: Omit) { - const { darkMode } = useContext(DarkModeContext); - const contentType = useMemo(() => { - if (raw) { - return ''; - } - - const type = getHeader(data.headers, 'content-type'); - return type.toLowerCase().split(';')[0]; - }, [data, raw]); + + ); +} +function ContentBody({ data, raw = false }: Omit) { + const { darkMode } = useContext(DarkModeContext); + const contentType = useMemo(() => { if (raw) { - return ( -
-          
-            {atob(data.body)}
-          
-        
- ) + return ""; } - if (['application/pdf', 'text/html'].includes(contentType)) { - return