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"; 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; } catch { return null; } } interface ContentProps { data: RequestPayload | ResponsePayload; setRaw: Dispatch>; raw?: boolean; } export default function Content({ raw, setRaw, data, }: ContentProps): JSX.Element { return (
setRaw(!raw)} /> {[ getHeader(data.headers, "content-length", "bytes"), getHeader(data.headers, "content-type"), ] .filter((x) => x !== null) .join("; ")} {(() => { try { 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]); if (raw) { return (
        {atob(data.body)}
      
); } if (["application/pdf", "text/html"].includes(contentType)) { return (