From bcb77d979d817e1e609adb4d007bbbcc3f61efbd Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Thu, 30 Dec 2021 09:51:00 +0100 Subject: Prepare for github --- src/components/Content/Content.module.scss | 51 +++++++++++++++ src/components/Content/Content.tsx | 100 +++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 src/components/Content/Content.module.scss create mode 100644 src/components/Content/Content.tsx (limited to 'src/components/Content') diff --git a/src/components/Content/Content.module.scss b/src/components/Content/Content.module.scss new file mode 100644 index 0000000..8908516 --- /dev/null +++ b/src/components/Content/Content.module.scss @@ -0,0 +1,51 @@ +.content { + width: 100%; + height: 100%; + display: flex; + flex-flow: column nowrap; + overflow: hidden; +} + +.header { + flex-shrink: 0; + flex-grow: 0; + width: 100%; + display: flex; + padding: 0.5em; + background-color: black; + color: white; +} + +.body { + flex-grow: 1; + flex-shrink: 1; + overflow-y: auto; + + pre { + width: 100%; + height: 100%; + padding: 1em; + font-family: monospace; + overflow: auto; + } + + iframe { + height: 100%; + width: 100%; + } +} + +.renderError { + width: 100%; + height: 100%; + display: flex; + flex-flow: column nowrap; + justify-content: center; + align-items: center; + + a { + margin-top: 1em; + text-decoration: underline; + color: blue; + } +} diff --git a/src/components/Content/Content.tsx b/src/components/Content/Content.tsx new file mode 100644 index 0000000..a7b5949 --- /dev/null +++ b/src/components/Content/Content.tsx @@ -0,0 +1,100 @@ +import styles from "~components/Details/Details.module.scss"; +import * as React from "react"; +import classNames from "classnames"; +import {RequestPayload, ResponsePayload} from "~hooks/useRequests"; +import { + Dispatch, + forwardRef, SetStateAction, + useEffect, + useImperativeHandle, + useMemo, + useRef, + useState +} from "react"; +import ReactJson from 'react-json-view'; +import styles from './Content.module.scss'; + +interface ContentProps { + data: RequestPayload | ResponsePayload + setRaw: Dispatch> + raw?: boolean +} + +export default function Content({ raw, setRaw, ...props }: ContentProps): JSX.Element { + return ( +
+
+ setRaw(!raw)}/> + +
+
+ {(() => { + try { + return ContentBody({ ...props, raw }) + } catch { + return ( +
+

Body could not be rendered

+ setRaw(true)}>View raw +
+ ) + } + })()} +
+
+ ) +}; + +function ContentBody({ data, raw = false }: Omit) { + const contentType = useMemo(() => { + if (raw) { + return ''; + } + + const [_, type] = ( + Object + .entries(data.headers) + .find(([key]) => key.toLowerCase() === 'content-type') + ); + + return type.toLowerCase().split(';')[0]; + }, [data, raw]); + + if (raw) { + return
{atob(data.body)}
+ } + + if (['application/pdf', 'text/html'].includes(contentType)) { + return