diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/components/App/App.tsx | 5 | ||||
| -rw-r--r-- | src/components/Content/Content.tsx | 7 | ||||
| -rw-r--r-- | src/contexts/DarkMode.tsx | 41 | ||||
| -rw-r--r-- | src/hooks/useDarkMode.tsx | 6 | ||||
| -rw-r--r-- | src/index.tsx | 7 |
5 files changed, 56 insertions, 10 deletions
diff --git a/src/components/App/App.tsx b/src/components/App/App.tsx index bbab612..3f50f22 100644 --- a/src/components/App/App.tsx +++ b/src/components/App/App.tsx | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | import * as React from "react"; | 1 | import * as React from "react"; |
| 2 | import {ReactElement, useEffect, useMemo, useState} from "react"; | 2 | import {ReactElement, useContext, useEffect, useMemo, useState} from "react"; |
| 3 | import useRequests, { | 3 | import useRequests, { |
| 4 | ReadyState, | 4 | ReadyState, |
| 5 | RequestResponse | 5 | RequestResponse |
| @@ -16,6 +16,7 @@ import {Sliders} from "../Icons/Sliders"; | |||
| 16 | import {Sun} from "../Icons/Sun"; | 16 | import {Sun} from "../Icons/Sun"; |
| 17 | import {Moon} from "../Icons/Moon"; | 17 | import {Moon} from "../Icons/Moon"; |
| 18 | import Trash from "../Icons/Trash"; | 18 | import Trash from "../Icons/Trash"; |
| 19 | import {DarkModeContext} from "../../contexts/DarkMode"; | ||
| 19 | 20 | ||
| 20 | interface Config { | 21 | interface Config { |
| 21 | url: string | 22 | url: string |
| @@ -49,7 +50,7 @@ const statusTextMap: ReadyStateMap = { | |||
| 49 | } | 50 | } |
| 50 | 51 | ||
| 51 | export default function App() { | 52 | export default function App() { |
| 52 | const { darkMode, toggle } = useDarkMode(); | 53 | const { darkMode, toggle } = useContext(DarkModeContext); |
| 53 | const [config, setConfig]= useState<Config | null>(null) | 54 | const [config, setConfig]= useState<Config | null>(null) |
| 54 | 55 | ||
| 55 | const { calls, readyState, clear } = useRequests({ | 56 | const { calls, readyState, clear } = useRequests({ |
diff --git a/src/components/Content/Content.tsx b/src/components/Content/Content.tsx index 95ee444..1341e4f 100644 --- a/src/components/Content/Content.tsx +++ b/src/components/Content/Content.tsx | |||
| @@ -1,9 +1,10 @@ | |||
| 1 | import * as React from "react"; | 1 | import * as React from "react"; |
| 2 | import {Dispatch, SetStateAction, useMemo} from "react"; | 2 | import {Dispatch, SetStateAction, useContext, useMemo} from "react"; |
| 3 | import {RequestPayload, ResponsePayload} from "~hooks/useRequests"; | 3 | import {RequestPayload, ResponsePayload} from "~hooks/useRequests"; |
| 4 | import ReactJson from 'react-json-view'; | 4 | import ReactJson from 'react-json-view'; |
| 5 | import styles from './Content.module.scss'; | 5 | import styles from './Content.module.scss'; |
| 6 | import {Button, Col, Container, Row} from "react-bootstrap"; | 6 | import {Button, Col, Container, Row} from "react-bootstrap"; |
| 7 | import {DarkModeContext} from "../../contexts/DarkMode"; | ||
| 7 | 8 | ||
| 8 | function getHeader(headers: { [key: string]: string }, key: string, unit?: string): string | null { | 9 | function getHeader(headers: { [key: string]: string }, key: string, unit?: string): string | null { |
| 9 | console.log(headers, key) | 10 | console.log(headers, key) |
| @@ -63,6 +64,7 @@ export default function Content({ raw, setRaw, data }: ContentProps): JSX.Elemen | |||
| 63 | }; | 64 | }; |
| 64 | 65 | ||
| 65 | function ContentBody({ data, raw = false }: Omit<ContentProps, 'setRaw'>) { | 66 | function ContentBody({ data, raw = false }: Omit<ContentProps, 'setRaw'>) { |
| 67 | const { darkMode } = useContext(DarkModeContext); | ||
| 66 | const contentType = useMemo(() => { | 68 | const contentType = useMemo(() => { |
| 67 | if (raw) { | 69 | if (raw) { |
| 68 | return ''; | 70 | return ''; |
| @@ -84,6 +86,7 @@ function ContentBody({ data, raw = false }: Omit<ContentProps, 'setRaw'>) { | |||
| 84 | 86 | ||
| 85 | if (['application/pdf', 'text/html'].includes(contentType)) { | 87 | if (['application/pdf', 'text/html'].includes(contentType)) { |
| 86 | return <iframe | 88 | return <iframe |
| 89 | className="bg-white" | ||
| 87 | src={`data:${contentType};base64,${data.body}`} | 90 | src={`data:${contentType};base64,${data.body}`} |
| 88 | srcDoc={contentType === 'text/html' ? atob(data.body) : undefined} | 91 | srcDoc={contentType === 'text/html' ? atob(data.body) : undefined} |
| 89 | loading='lazy' | 92 | loading='lazy' |
| @@ -94,10 +97,12 @@ function ContentBody({ data, raw = false }: Omit<ContentProps, 'setRaw'>) { | |||
| 94 | if (contentType.startsWith('application/json')) { | 97 | if (contentType.startsWith('application/json')) { |
| 95 | return <ReactJson | 98 | return <ReactJson |
| 96 | src={JSON.parse(atob(data.body))} | 99 | src={JSON.parse(atob(data.body))} |
| 100 | theme={darkMode ? "monokai" : undefined} | ||
| 97 | style={{ | 101 | style={{ |
| 98 | padding: '1em', | 102 | padding: '1em', |
| 99 | width: '100%', | 103 | width: '100%', |
| 100 | height: '100%', | 104 | height: '100%', |
| 105 | overflowY: 'auto', | ||
| 101 | }} | 106 | }} |
| 102 | /> | 107 | /> |
| 103 | } | 108 | } |
diff --git a/src/contexts/DarkMode.tsx b/src/contexts/DarkMode.tsx new file mode 100644 index 0000000..6dc1c31 --- /dev/null +++ b/src/contexts/DarkMode.tsx | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | import * as React from "react"; | ||
| 2 | import { | ||
| 3 | Context, | ||
| 4 | createContext, | ||
| 5 | ReactElement, | ||
| 6 | ReactNode, useEffect, | ||
| 7 | useMemo, | ||
| 8 | useState | ||
| 9 | } from "react"; | ||
| 10 | import {ThemeConfig} from "bootstrap-darkmode"; | ||
| 11 | |||
| 12 | interface DarkModeApi { | ||
| 13 | darkMode: boolean | ||
| 14 | toggle: () => void | ||
| 15 | } | ||
| 16 | |||
| 17 | export const DarkModeContext = createContext<Partial<DarkModeApi>>({}) as Context<DarkModeApi>; | ||
| 18 | interface DarkModeProviderProps { | ||
| 19 | children: ReactNode | ||
| 20 | } | ||
| 21 | |||
| 22 | export default function DarkModeProvider({ children }: DarkModeProviderProps) { | ||
| 23 | const themeConfig = useMemo(() => new ThemeConfig(), []); | ||
| 24 | const [darkMode, setDarkMode] = useState(() => themeConfig.getTheme() === 'dark') | ||
| 25 | |||
| 26 | useEffect(() => { | ||
| 27 | themeConfig.setTheme(darkMode ? 'dark' : 'light'); | ||
| 28 | }, [darkMode]) | ||
| 29 | |||
| 30 | return ( | ||
| 31 | <DarkModeContext.Provider | ||
| 32 | value={{ | ||
| 33 | darkMode, | ||
| 34 | toggle: () => setDarkMode(dm => !dm), | ||
| 35 | }} | ||
| 36 | > | ||
| 37 | |||
| 38 | {children} | ||
| 39 | </DarkModeContext.Provider> | ||
| 40 | ) | ||
| 41 | } | ||
diff --git a/src/hooks/useDarkMode.tsx b/src/hooks/useDarkMode.tsx index fe4ce95..18aea68 100644 --- a/src/hooks/useDarkMode.tsx +++ b/src/hooks/useDarkMode.tsx | |||
| @@ -7,12 +7,6 @@ export interface UseDarkMode { | |||
| 7 | } | 7 | } |
| 8 | 8 | ||
| 9 | export default function useDarkMode() { | 9 | export default function useDarkMode() { |
| 10 | const themeConfig = useMemo(() => new ThemeConfig(), []); | ||
| 11 | const [darkMode, setDarkMode] = useState(() => themeConfig.getTheme() === 'dark') | ||
| 12 | |||
| 13 | useEffect(() => { | ||
| 14 | themeConfig.setTheme(darkMode ? 'dark' : 'light'); | ||
| 15 | }, [darkMode]) | ||
| 16 | 10 | ||
| 17 | return { | 11 | return { |
| 18 | darkMode, | 12 | darkMode, |
diff --git a/src/index.tsx b/src/index.tsx index a6c4952..037092d 100644 --- a/src/index.tsx +++ b/src/index.tsx | |||
| @@ -3,5 +3,10 @@ import * as ReactDOM from "react-dom"; | |||
| 3 | import App from "./components/App/App"; | 3 | import App from "./components/App/App"; |
| 4 | 4 | ||
| 5 | import './index.scss'; | 5 | import './index.scss'; |
| 6 | import DarkModeProvider from "./contexts/DarkMode"; | ||
| 6 | 7 | ||
| 7 | ReactDOM.render(<App />, document.getElementById("root")); | 8 | ReactDOM.render(( |
| 9 | <DarkModeProvider> | ||
| 10 | <App /> | ||
| 11 | </DarkModeProvider> | ||
| 12 | ), document.getElementById("root")); | ||
