|
| 1 | +import type {NextPage} from 'next' |
| 2 | +import Head from 'next/head' |
| 3 | +import tw, {css} from 'twin.macro' |
| 4 | +import {TextBox} from "../../components/TextBox"; |
| 5 | +import {useEffect, useState} from "react"; |
| 6 | +import {parse} from 'toml' |
| 7 | +import dynamic from "next/dynamic"; |
| 8 | +import duotoneDark from 'prism-react-renderer/themes/duotoneDark'; |
| 9 | +import {useRouter} from "next/router"; |
| 10 | +import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"; |
| 11 | +import {faLink} from "@fortawesome/free-solid-svg-icons"; |
| 12 | + |
| 13 | +const ReactJson = dynamic(import('react-json-view'), {ssr: false}); |
| 14 | + |
| 15 | +const Home: NextPage = () => { |
| 16 | + |
| 17 | + const router = useRouter(); |
| 18 | + |
| 19 | + const [config, setConfig] = useState(""); |
| 20 | + const [parsedConfig, setParsedConfig] = useState<any>({}); |
| 21 | + const [error, setError] = useState<false | string>(false); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + if (!router.query.data) return; |
| 25 | + if (!(typeof router.query.data === 'string')) return; |
| 26 | + try { |
| 27 | + const toml = Buffer.from(router.query.data, "base64").toString("utf8"); |
| 28 | + setConfig(toml.toString()) |
| 29 | + } catch (e) { |
| 30 | + return; |
| 31 | + } |
| 32 | + }, [router.query.data]); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + let configObject; |
| 36 | + try { |
| 37 | + configObject = parse(config); |
| 38 | + if (!configObject || !(typeof configObject === 'object')) { |
| 39 | + setError("must be object"); |
| 40 | + setParsedConfig({}) |
| 41 | + } else { |
| 42 | + setError(false); |
| 43 | + setParsedConfig(JSON.parse(JSON.stringify(configObject))) |
| 44 | + } |
| 45 | + } catch (e: any) { |
| 46 | + if (e.line && e.column) { |
| 47 | + setError("Parsing error on line " + e.line + ", column " + e.column + |
| 48 | + ": " + e.message); |
| 49 | + } else { |
| 50 | + setError(e.message); |
| 51 | + } |
| 52 | + setParsedConfig({}) |
| 53 | + } |
| 54 | + }, [config]) |
| 55 | + |
| 56 | + return ( |
| 57 | + <div> |
| 58 | + <Head> |
| 59 | + <title>Toml Validator</title> |
| 60 | + <meta name="description" content="Validate Toml Files"/> |
| 61 | + </Head> |
| 62 | + |
| 63 | + <main css={css`${tw`flex flex-col`} height: calc(100vh - 3.5rem)`}> |
| 64 | + <div css={tw`text-white bg-blue-500 w-full md:px-8 p-16 h-48 text-center`}> |
| 65 | + <p css={tw`text-3xl font-bold`}>HelpChat</p> |
| 66 | + <p css={tw`text-lg`}>Toml Validator</p> |
| 67 | + </div> |
| 68 | + <div css={tw`flex flex-col md:flex-row flex-grow flex-shrink h-full`}> |
| 69 | + <div css={css` |
| 70 | + height: calc(100vh - 15.5em); |
| 71 | + ${tw`md:w-1/2 p-4 pt-1 pr-2 md:max-width[50vw]`} |
| 72 | + `}> |
| 73 | + <TextBox title={"Toml Config"} code={config} editor={setConfig} language={"toml"}/> |
| 74 | + </div> |
| 75 | + <div css={css` |
| 76 | + height: calc(100vh - 15.5em); |
| 77 | + ${tw`w-full md:w-1/2 p-4 pl-2 pt-1 flex flex-col md:max-width[50vw]`} |
| 78 | + `}> |
| 79 | + <div css={tw`flex flex-col h-full w-full pt-1`}> |
| 80 | + <div css={tw`flex flex-row pl-2`}> |
| 81 | + <p css={tw`text-xl font-semibold mx-auto mb-2`}>JSON Object Dumb</p> |
| 82 | + <div css={tw`flex flex-row h-8`}> |
| 83 | + <div css={tw`py-1 px-2 bg-green-400 rounded-md hover:cursor-pointer`} |
| 84 | + onClick={() => { |
| 85 | + router.query.data = Buffer.from(config).toString("base64"); |
| 86 | + router.push(router); |
| 87 | + }}> |
| 88 | + <FontAwesomeIcon icon={faLink} size="1x"/> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + <div css={css`${tw`rounded-md overflow-auto h-full`} background-color: #2a2734`}> |
| 93 | + <div css={tw`py-2 px-4 text-base`}> |
| 94 | + {!(error || !parsedConfig) ? <ReactJson src={parsedConfig} theme={{ |
| 95 | + base00: duotoneDark.plain.backgroundColor ?? "", |
| 96 | + base01: duotoneDark.styles[2].style.color ?? "", |
| 97 | + base02: duotoneDark.styles[1].style.color ?? "", |
| 98 | + base03: duotoneDark.styles[2].style.color ?? "", |
| 99 | + base04: duotoneDark.styles[4].style.color ?? "", |
| 100 | + base05: duotoneDark.styles[4].style.color ?? "", |
| 101 | + base06: duotoneDark.styles[5].style.color ?? "", |
| 102 | + base07: duotoneDark.styles[3].style.color ?? "", |
| 103 | + base08: duotoneDark.styles[7].style.color ?? "", |
| 104 | + base09: duotoneDark.styles[6].style.color ?? "", |
| 105 | + base0A: duotoneDark.styles[9].style.color ?? "", |
| 106 | + base0B: duotoneDark.styles[10].style.color ?? "", |
| 107 | + base0C: duotoneDark.styles[4].style.color ?? "", |
| 108 | + base0D: duotoneDark.styles[2].style.color ?? "", |
| 109 | + base0E: duotoneDark.styles[2].style.color ?? "", |
| 110 | + base0F: duotoneDark.styles[2].style.color ?? "", |
| 111 | + }}/> : |
| 112 | + <span css={ |
| 113 | + css` |
| 114 | + ${tw`text-base text-white whitespace-pre`} |
| 115 | + font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; |
| 116 | + `}>{error}</span> |
| 117 | + } |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + </main> |
| 124 | + </div> |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +export default Home |
0 commit comments