|
| 1 | +import { Alert, Box, Link, Sheet, TabPanel, Typography } from "@mui/joy"; |
| 2 | +import CodeMirror from "@uiw/react-codemirror"; |
| 3 | +import { javascript } from "@codemirror/lang-javascript"; |
| 4 | +import { useCallback, useEffect, useState } from "react"; |
| 5 | +import { diff } from "@opentf/obj-diff"; |
| 6 | +import Tabs from "@mui/joy/Tabs"; |
| 7 | +import TabList from "@mui/joy/TabList"; |
| 8 | +import Tab from "@mui/joy/Tab"; |
| 9 | +import Launch from "@mui/icons-material/Launch"; |
| 10 | +import prettier from "prettier/standalone"; |
| 11 | +import * as babelPlugin from "prettier/parser-babel"; |
| 12 | +import * as estreeParser from "prettier/plugins/estree"; |
| 13 | + |
| 14 | +async function format(code) { |
| 15 | + const formatted = await prettier.format(`const a = ${code}`, { |
| 16 | + parser: "babel", |
| 17 | + plugins: [babelPlugin, estreeParser], |
| 18 | + semi: false, |
| 19 | + trailingComma: "none", |
| 20 | + }); |
| 21 | + |
| 22 | + return formatted.replace("const a = ", ""); |
| 23 | +} |
| 24 | + |
| 25 | +function App() { |
| 26 | + const [obj1Val, setObj1Val] = useState(`{ |
| 27 | + a: 1, b: 2 |
| 28 | + }`); |
| 29 | + const [obj2Val, setObj2Val] = useState(`{ |
| 30 | + a: 2, c: 5 |
| 31 | + }`); |
| 32 | + const [raw, setRaw] = useState([]); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + async function runFormat() { |
| 36 | + setObj1Val(await format(obj1Val)); |
| 37 | + setObj2Val(await format(obj2Val)); |
| 38 | + } |
| 39 | + |
| 40 | + runFormat(); |
| 41 | + }, []); |
| 42 | + |
| 43 | + const onChange1 = useCallback((val: string) => { |
| 44 | + setObj1Val(val); |
| 45 | + }, []); |
| 46 | + |
| 47 | + const onChange2 = useCallback((val: string) => { |
| 48 | + setObj2Val(val); |
| 49 | + }, []); |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + try { |
| 53 | + const a = eval(`const a = ${obj1Val}; a`); |
| 54 | + const b = eval(`const a = ${obj2Val}; a`); |
| 55 | + setRaw(diff(a, b)); |
| 56 | + } catch (error) { |
| 57 | + console.log("error"); |
| 58 | + } |
| 59 | + }, [obj1Val, obj2Val]); |
| 60 | + |
| 61 | + return ( |
| 62 | + <Box sx={{}}> |
| 63 | + <Box |
| 64 | + sx={{ |
| 65 | + borderBottom: "1px solid grey", |
| 66 | + p: 1, |
| 67 | + px: 3, |
| 68 | + display: "flex", |
| 69 | + justifyContent: "space-between", |
| 70 | + }} |
| 71 | + > |
| 72 | + <Box sx={{ display: "flex" }}> |
| 73 | + <img src="/Logo.svg" alt="Logo" height="35" /> |
| 74 | + <Typography level="h4" sx={{ ml: 2, letterSpacing: "2px" }}> |
| 75 | + obj-diff |
| 76 | + </Typography> |
| 77 | + </Box> |
| 78 | + <Link |
| 79 | + href="https://github.com/Open-Tech-Foundation/obj-diff" |
| 80 | + endDecorator={<Launch fontSize="inherit" />} |
| 81 | + target="_blank" |
| 82 | + rel="noopener" |
| 83 | + fontSize="sm" |
| 84 | + > |
| 85 | + Github |
| 86 | + </Link> |
| 87 | + </Box> |
| 88 | + <Box sx={{ display: "flex", justifyContent: "center", mt: 2 }}> |
| 89 | + <Alert variant="soft" color="success"> |
| 90 | + <Typography level="body-md"> |
| 91 | + 🚀 The Fast, Accurate, JavaScript Objects Diffing Library. |
| 92 | + </Typography> |
| 93 | + </Alert> |
| 94 | + </Box> |
| 95 | + <Box |
| 96 | + sx={{ |
| 97 | + display: "grid", |
| 98 | + gridTemplateColumns: "1fr 10px 1fr 10px 1fr", |
| 99 | + p: "50px", |
| 100 | + height: "600px", |
| 101 | + boxSizing: "border-box", |
| 102 | + }} |
| 103 | + > |
| 104 | + <Box sx={{ height: "100%", overflow: "hidden" }}> |
| 105 | + <Sheet |
| 106 | + variant="outlined" |
| 107 | + sx={{ |
| 108 | + height: "100%", |
| 109 | + overflow: "hidden", |
| 110 | + display: "flex", |
| 111 | + flexDirection: "column", |
| 112 | + boxSizing: "border-box", |
| 113 | + }} |
| 114 | + > |
| 115 | + <Typography level="body-sm" sx={{ textAlign: "center" }}> |
| 116 | + Object 1 |
| 117 | + </Typography> |
| 118 | + <Box sx={{ height: "calc(100% - 25px)", overflow: "auto" }}> |
| 119 | + <CodeMirror |
| 120 | + value={obj1Val} |
| 121 | + extensions={[javascript()]} |
| 122 | + onChange={onChange1} |
| 123 | + height="100%" |
| 124 | + /> |
| 125 | + </Box> |
| 126 | + </Sheet> |
| 127 | + </Box> |
| 128 | + <Box /> |
| 129 | + <Box sx={{ height: "100%", overflow: "hidden" }}> |
| 130 | + <Sheet |
| 131 | + variant="outlined" |
| 132 | + sx={{ |
| 133 | + height: "100%", |
| 134 | + overflow: "hidden", |
| 135 | + display: "flex", |
| 136 | + flexDirection: "column", |
| 137 | + boxSizing: "border-box", |
| 138 | + }} |
| 139 | + > |
| 140 | + <Typography level="body-sm" sx={{ textAlign: "center" }}> |
| 141 | + Object 2 |
| 142 | + </Typography> |
| 143 | + <Box sx={{ height: "calc(100% - 25px)", overflow: "auto" }}> |
| 144 | + <CodeMirror |
| 145 | + value={obj2Val} |
| 146 | + extensions={[javascript()]} |
| 147 | + onChange={onChange2} |
| 148 | + /> |
| 149 | + </Box> |
| 150 | + </Sheet> |
| 151 | + </Box> |
| 152 | + <Box /> |
| 153 | + <Box sx={{ height: "100%", overflow: "hidden" }}> |
| 154 | + <Sheet |
| 155 | + variant="outlined" |
| 156 | + sx={{ height: "100%", boxSizing: "border-box" }} |
| 157 | + > |
| 158 | + <Tabs |
| 159 | + aria-label="Basic tabs" |
| 160 | + defaultValue={0} |
| 161 | + sx={{ height: "100%" }} |
| 162 | + > |
| 163 | + <TabList sx={{ display: "flex", justifyContent: "center" }}> |
| 164 | + <Tab>Visualize</Tab> |
| 165 | + <Tab>Raw</Tab> |
| 166 | + </TabList> |
| 167 | + <TabPanel |
| 168 | + value={0} |
| 169 | + sx={{ height: "calc(100% - 35px)", overflow: "auto" }} |
| 170 | + ></TabPanel> |
| 171 | + <TabPanel |
| 172 | + value={1} |
| 173 | + sx={{ height: "calc(100% - 35px)", overflow: "auto" }} |
| 174 | + > |
| 175 | + <Box component="pre">{JSON.stringify(raw, null, 4)}</Box> |
| 176 | + </TabPanel> |
| 177 | + </Tabs> |
| 178 | + </Sheet> |
| 179 | + </Box> |
| 180 | + </Box> |
| 181 | + |
| 182 | + <Box sx={{ display: "flex", justifyContent: "center", mt: 2 }}> |
| 183 | + <Box sx={{ mr: 1 }} component="span"> |
| 184 | + {" "} |
| 185 | + © 2024 |
| 186 | + </Box> |
| 187 | + <Link href="https://open-tech-foundation.pages.dev/"> |
| 188 | + Open Tech Foundation |
| 189 | + </Link> |
| 190 | + . |
| 191 | + </Box> |
| 192 | + </Box> |
| 193 | + ); |
| 194 | +} |
| 195 | + |
| 196 | +export default App; |
0 commit comments