Skip to content

✨ Innovative and open-source visualization application that transforms various data formats, such as JSON, YAML, XML and CSV into interactive graphs.

improve context api

AykutSaraca6db361a1a18
diff --git a/src/components/CustomNode/index.tsx b/src/components/CustomNode/index.tsx
index f69c8140..7c6a16f0 100644
--- a/src/components/CustomNode/index.tsx
+++ b/src/components/CustomNode/index.tsx
@@ -18,7 +18,7 @@ const baseLabelStyle = {
strokeWidth: 0,
};
-const CustomNode = (nodeProps: NodeProps) => {
+export const CustomNode = (nodeProps: NodeProps) => {
const { properties: data } = nodeProps;
return (
@@ -53,7 +53,3 @@ const CustomNode = (nodeProps: NodeProps) => {
</Node>
);
};
-
-const MemoizedCustomNode = React.memo(CustomNode);
-
-export { MemoizedCustomNode as CustomNode };
diff --git a/src/components/Graph/index.tsx b/src/components/Graph/index.tsx
index 15fb8e02..0e61eb54 100644
--- a/src/components/Graph/index.tsx
+++ b/src/components/Graph/index.tsx
@@ -1,5 +1,5 @@
import React from "react";
-import { Canvas, EdgeData, NodeData } from "reaflow";
+import { Canvas, EdgeData, ElkRoot, NodeData } from "reaflow";
import { CustomNode } from "src/components/CustomNode";
import { useConfig } from "src/hocs/config";
import { getEdgeNodes } from "src/containers/LiveEditor/helpers";
@@ -12,9 +12,7 @@ export const Graph = () => {
height: 2000,
});
- const {
- states: { json, settings },
- } = useConfig();
+ const { json, settings } = useConfig();
React.useEffect(() => {
const { nodes, edges } = getEdgeNodes(json, settings.expand);
@@ -28,22 +26,24 @@ export const Graph = () => {
if (input) input.blur();
};
+ const onLayoutChange = (layout: ElkRoot) => {
+ if (layout.width && layout.height)
+ setSize({ width: layout.width, height: layout.height });
+ };
+
return (
<Canvas
nodes={nodes}
- node={(props) => <CustomNode {...props} />}
edges={edges}
maxWidth={size.width}
maxHeight={size.height}
- zoomable={false}
direction={settings.layout}
- readonly
key={settings.layout}
onCanvasClick={onCanvasClick}
- onLayoutChange={(lay) => {
- if (lay.width && lay.height)
- setSize({ width: lay.width, height: lay.height });
- }}
+ onLayoutChange={onLayoutChange}
+ node={CustomNode}
+ zoomable={false}
+ readonly
/>
);
};
diff --git a/src/components/Sidebar/index.tsx b/src/components/Sidebar/index.tsx
index 9ec84299..fc4de5e3 100644
--- a/src/components/Sidebar/index.tsx
+++ b/src/components/Sidebar/index.tsx
@@ -103,16 +103,13 @@ const StyledImportFile = styled.label`
function getLayoutIcon(layout: CanvasDirection) {
if (layout === "LEFT") return <CgArrowLongLeftE />;
- if (layout === "UP") return <CgArrowLongDownE />;
+ if (layout === "UP") return <CgArrowLongUpE />;
if (layout === "RIGHT") return <CgArrowLongRightE />;
- return <CgArrowLongUpE />;
+ return <CgArrowLongDownE />;
}
export const Sidebar: React.FC = () => {
- const {
- states: { settings },
- dispatch,
- } = useConfig();
+ const { settings, dispatch } = useConfig();
const router = useRouter();
const [jsonFile, setJsonFile] = React.useState<File | null>(null);
diff --git a/src/containers/Editor/Tools.tsx b/src/containers/Editor/Tools.tsx
index e8e813c3..a2666fe6 100644
--- a/src/containers/Editor/Tools.tsx
+++ b/src/containers/Editor/Tools.tsx
@@ -41,9 +41,9 @@ const StyledToolElement = styled.button`
`;
export const Tools: React.FC = () => {
- const { states, dispatch } = useConfig();
+ const { json, settings, dispatch } = useConfig();
const handleSave = () => {
- localStorage.setItem("json", states.json);
+ localStorage.setItem("json", json);
toast.success("Saved JSON successfully!");
};
@@ -63,7 +63,7 @@ export const Tools: React.FC = () => {
<AiOutlineFullscreen />
</StyledToolElement>
<StyledToolElement aria-label="switch theme" onClick={toggleTheme}>
- {states.settings.lightmode ? <HiOutlineMoon /> : <HiOutlineSun />}
+ {settings.lightmode ? <HiOutlineMoon /> : <HiOutlineSun />}
</StyledToolElement>
<Input />
<StyledToolElement aria-label="save" onClick={handleSave}>
diff --git a/src/containers/Editor/index.tsx b/src/containers/Editor/index.tsx
index 223883b3..5791b690 100644
--- a/src/containers/Editor/index.tsx
+++ b/src/containers/Editor/index.tsx
@@ -8,9 +8,7 @@ import { useConfig } from "src/hocs/config";
import { Allotment } from "allotment";
const Editor: React.FC = () => {
- const {
- states: { settings },
- } = useConfig();
+ const { settings } = useConfig();
return (
<Styles.StyledPageWrapper>
diff --git a/src/hocs/config.tsx b/src/hocs/config.tsx
index 36494e20..fa32c7d6 100644
--- a/src/hocs/config.tsx
+++ b/src/hocs/config.tsx
@@ -18,12 +18,13 @@ export const initialStates: AppConfig = {
};
interface Config {
- states: AppConfig;
+ json: string;
+ settings: StorageConfig;
dispatch: React.Dispatch<ReducerAction>;
}
const defaultContext: Config = {
- states: initialStates,
+ ...initialStates,
dispatch: () => {},
};
@@ -35,7 +36,11 @@ const useConfig = () => React.useContext(ConfigContext);
const WithConfig: ReactComponent = ({ children }) => {
const [render, setRender] = React.useState(false);
const [states, dispatch] = React.useReducer(useConfigReducer, initialStates);
- const value = { states, dispatch };
+ const value = {
+ dispatch,
+ json: states.json,
+ settings: states.settings,
+ };
React.useEffect(() => {
const jsonStored = localStorage.getItem("json");
diff --git a/src/hooks/useFocusNode.tsx b/src/hooks/useFocusNode.tsx
index c989d1da..1bb00036 100644
--- a/src/hooks/useFocusNode.tsx
+++ b/src/hooks/useFocusNode.tsx
@@ -14,9 +14,7 @@ export const useFocusNode = () => {
debounced: "",
});
- const {
- states: { settings },
- } = useConfig();
+ const { settings } = useConfig();
const skip = () => setSelectedNode((current) => current + 1);
diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx
index ad3bf8b5..22c70bc3 100644
--- a/src/pages/_app.tsx
+++ b/src/pages/_app.tsx
@@ -9,9 +9,7 @@ import { useConfig, withConfig } from "src/hocs/config";
import { GoogleAnalytics } from "src/components/GoogleAnalytics";
function JsonVisio({ Component, pageProps }: AppProps) {
- const {
- states: { settings },
- } = useConfig();
+ const { settings } = useConfig();
return (
<>