diff --git a/src/components/Sidebar/index.tsx b/src/components/Sidebar/index.tsx index 22c2233..2d9ee36 100644 --- a/src/components/Sidebar/index.tsx +++ b/src/components/Sidebar/index.tsx @@ -35,7 +35,7 @@ const StyledElement = styled.div<{ disabled?: boolean }>` color: ${({ theme, disabled }) => disabled ? theme.SILVER_DARK : theme.SILVER}; cursor: pointer; - pointer-events: ${({ disabled }) => disabled && 'none'}; + pointer-events: ${({ disabled }) => disabled && "none"}; a { text-align: center; @@ -90,9 +90,10 @@ const StyledImportFile = styled.label` } `; -export const Sidebar = () => { +export const Sidebar: React.FC<{ + setJson: React.Dispatch>; +}> = ({ setJson }) => { const [jsonFile, setJsonFile] = React.useState(null); - const [json, setJson] = useLocalStorage("json", JSON.stringify(defaultValue)); const [config, setConfig] = useLocalStorage("config", { layout: "LEFT", minimap: true, diff --git a/src/containers/JsonEditor/index.tsx b/src/containers/JsonEditor/index.tsx index e942b2b..ccf5d35 100644 --- a/src/containers/JsonEditor/index.tsx +++ b/src/containers/JsonEditor/index.tsx @@ -53,13 +53,22 @@ export const defaultValue = [ }, ]; -export const JsonEditor: React.FC = () => { - const [clear, setClear] = React.useState(0); - const [json, setJson] = useLocalStorage("json", JSON.stringify(defaultValue)); - const initialJson = React.useMemo(() => JSON.parse(json), [clear]); +export const JsonEditor: React.FC<{ + json: string; + setJson: React.Dispatch>; +}> = ({ json, setJson }) => { + const [initialJson, setInitialJson] = React.useState( + React.useMemo( + () => + typeof localStorage !== "undefined" && localStorage.getItem("json") + ? localStorage.getItem("json") + : json, + [] + ) + ); React.useEffect(() => { - if (json === "[]") setClear((c) => c + 1); + if (json === "[]") setInitialJson(json); }, [json]); React.useEffect(() => { @@ -78,13 +87,17 @@ export const JsonEditor: React.FC = () => { } }; - return ( - - ); + if (typeof window !== "undefined") { + return ( + + ); + } + + return null; }; diff --git a/src/containers/LiveEditor/CustomNode.tsx b/src/containers/LiveEditor/CustomNode.tsx index 3e241fa..1e2ad12 100644 --- a/src/containers/LiveEditor/CustomNode.tsx +++ b/src/containers/LiveEditor/CustomNode.tsx @@ -82,7 +82,10 @@ const CustomNode = ({ nodeProps }) => { {entries.map((val) => ( -
+
{val[0]}: {val[1]}
diff --git a/src/containers/LiveEditor/index.tsx b/src/containers/LiveEditor/index.tsx index 433671a..90af8d8 100644 --- a/src/containers/LiveEditor/index.tsx +++ b/src/containers/LiveEditor/index.tsx @@ -1,4 +1,4 @@ -import React, { ComponentType } from "react"; +import React from "react"; import styled from "styled-components"; import { TransformWrapper, @@ -40,11 +40,12 @@ const StyledControls = styled.div` opacity: 0.8; `; -export const LiveEditor: React.FC = () => { +export const LiveEditor: React.FC<{ + json: string; + setJson: React.Dispatch>; +}> = ({ json }) => { const canvasRef = React.useRef(null); const wrapperRef = React.useRef(null); - - const [json] = useLocalStorage("json", JSON.stringify(defaultValue)); const [config] = useLocalStorage("config", { layout: "LEFT", minimap: true, @@ -52,7 +53,7 @@ export const LiveEditor: React.FC = () => { }); React.useEffect(() => { - wrapperRef.current?.resetTransform(); + if (wrapperRef.current) wrapperRef.current?.resetTransform(); }, [json, wrapperRef]); const { nodes, edges } = getEdgeNodes(json); @@ -127,7 +128,7 @@ export const LiveEditor: React.FC = () => { - diff --git a/src/pages/editor/index.tsx b/src/pages/editor/index.tsx index d57bd35..69d7345 100644 --- a/src/pages/editor/index.tsx +++ b/src/pages/editor/index.tsx @@ -6,7 +6,7 @@ import SplitPane from "react-split-pane"; import { Button } from "src/components/Button"; import { Sidebar } from "src/components/Sidebar"; -import { JsonEditor } from "src/containers/JsonEditor"; +import { defaultValue, JsonEditor } from "src/containers/JsonEditor"; import { LiveEditor } from "src/containers/LiveEditor"; const StyledPageWrapper = styled.div` @@ -117,14 +117,21 @@ const StyledEditor = styled(SplitPane)` `; const Editor: React.FC = () => { + const [json, setJson] = React.useState(JSON.stringify(defaultValue)); const route = useRouter(); + React.useEffect(() => { + const jsonStored = localStorage.getItem("json"); + + if (jsonStored) setJson(jsonStored); + }, []); + return ( Editor | JSON Visio - + { defaultSize={450} split="vertical" > - - + +