Skip to content

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

perf improvements

AykutSarac1f2ec0a4a652
diff --git a/src/components/CustomNode/ObjectNode.tsx b/src/components/CustomNode/ObjectNode.tsx
deleted file mode 100644
index 93af542b..00000000
--- a/src/components/CustomNode/ObjectNode.tsx
+++ /dev/null
@@ -1,37 +0,0 @@
-import React from "react";
-import { CustomNodeProps } from "src/components/CustomNode";
-import { TextRenderer } from "./TextRenderer";
-import * as Styled from "./styles";
-
-const Node: React.FC<CustomNodeProps> = ({ node, x, y }) => {
- const { text, width, height, data } = node;
-
- if (data?.isEmpty) return null;
-
- return (
- <Styled.StyledForeignObject width={width} height={height} x={0} y={0} $isObject>
- {(text as [string, unknown][]).map((val, idx) => {
- return (
- <Styled.StyledRow
- data-key={JSON.stringify(val)}
- data-type={JSON.stringify(val[1])}
- data-x={x}
- data-y={y + idx * 17.8}
- key={idx}
- >
- <Styled.StyledKey $objectKey>
- {JSON.stringify(val[0]).replaceAll('"', "")}:{" "}
- </Styled.StyledKey>
- <TextRenderer>{JSON.stringify(val[1])}</TextRenderer>
- </Styled.StyledRow>
- );
- })}
- </Styled.StyledForeignObject>
- );
-};
-
-function propsAreEqual(prev: CustomNodeProps, next: CustomNodeProps) {
- return String(prev.node.text) === String(next.node.text) && prev.node.width === next.node.width;
-}
-
-export const ObjectNode = React.memo(Node, propsAreEqual);
diff --git a/src/components/Graph/CustomNode/ObjectNode.tsx b/src/components/Graph/CustomNode/ObjectNode.tsx
new file mode 100644
index 00000000..d5311c65
--- /dev/null
+++ b/src/components/Graph/CustomNode/ObjectNode.tsx
@@ -0,0 +1,41 @@
+import React from "react";
+import { CustomNodeProps } from "src/components/Graph/CustomNode";
+import { TextRenderer } from "./TextRenderer";
+import * as Styled from "./styles";
+
+type Value = [string, string];
+
+type RowProps = {
+ val: Value;
+ x: number;
+ y: number;
+ index: number;
+};
+
+const Row = ({ val, x, y, index }: RowProps) => {
+ return (
+ <Styled.StyledRow
+ data-key={JSON.stringify(val)}
+ data-x={x}
+ data-y={y + index * 17.8}
+ $type={JSON.stringify(val[1])}
+ >
+ <Styled.StyledKey $objectKey>{JSON.stringify(val[0]).replaceAll('"', "")}: </Styled.StyledKey>
+ <TextRenderer>{JSON.stringify(val[1])}</TextRenderer>
+ </Styled.StyledRow>
+ );
+};
+
+const Node: React.FC<CustomNodeProps> = ({ node, x, y }) => (
+ <Styled.StyledForeignObject width={node.width} height={node.height} x={0} y={0} $isObject>
+ {(node.text as Value[]).map((val, idx) => (
+ <Row val={val} index={idx} x={x} y={y} key={idx} />
+ ))}
+ </Styled.StyledForeignObject>
+);
+
+function propsAreEqual(prev: CustomNodeProps, next: CustomNodeProps) {
+ return String(prev.node.text) === String(next.node.text) && prev.node.width === next.node.width;
+}
+
+export const ObjectNode = React.memo(Node, propsAreEqual);
diff --git a/src/components/CustomNode/TextNode.tsx b/src/components/Graph/CustomNode/TextNode.tsx
similarity index 98%
rename from src/components/CustomNode/TextNode.tsx
rename to src/components/Graph/CustomNode/TextNode.tsx
index fa1c3420..2e9aa601 100644
--- a/src/components/CustomNode/TextNode.tsx
+++ b/src/components/Graph/CustomNode/TextNode.tsx
@@ -1,7 +1,7 @@
import React from "react";
import styled from "styled-components";
import { MdLink, MdLinkOff } from "react-icons/md";
-import { CustomNodeProps } from "src/components/CustomNode";
+import { CustomNodeProps } from "src/components/Graph/CustomNode";
import { isContentImage } from "src/lib/utils/graph/calculateNodeSize";
import useGraph from "src/store/useGraph";
import useStored from "src/store/useStored";
diff --git a/src/components/CustomNode/TextRenderer.tsx b/src/components/Graph/CustomNode/TextRenderer.tsx
similarity index 100%
rename from src/components/CustomNode/TextRenderer.tsx
rename to src/components/Graph/CustomNode/TextRenderer.tsx
diff --git a/src/components/CustomNode/index.tsx b/src/components/Graph/CustomNode/index.tsx
similarity index 72%
rename from src/components/CustomNode/index.tsx
rename to src/components/Graph/CustomNode/index.tsx
index 65f1b41e..eebbe57b 100644
--- a/src/components/CustomNode/index.tsx
+++ b/src/components/Graph/CustomNode/index.tsx
@@ -16,17 +16,18 @@ const rootProps = {
ry: 50,
};
-export const CustomNode = (nodeProps: NodeProps) => {
+export const CustomNode = (nodeProps: NodeProps<NodeData["data"]>) => {
const { text, data } = nodeProps.properties;
return (
- <Node {...nodeProps} {...(data.isEmpty && rootProps)} label={<React.Fragment />}>
+ <Node {...nodeProps} {...(data?.isEmpty && rootProps)} animated={false} label={null as any}>
{({ node, x, y }) => {
if (Array.isArray(text)) {
+ if (data?.isEmpty) return null;
return <ObjectNode node={node as NodeData} x={x} y={y} />;
}
- return <TextNode node={node as NodeData} hasCollapse={!!data.childrenCount} x={x} y={y} />;
+ return <TextNode node={node as NodeData} hasCollapse={!!data?.childrenCount} x={x} y={y} />;
}}
</Node>
);
diff --git a/src/components/CustomNode/styles.tsx b/src/components/Graph/CustomNode/styles.tsx
similarity index 91%
rename from src/components/CustomNode/styles.tsx
rename to src/components/Graph/CustomNode/styles.tsx
index f2c8b6ca..2410a69b 100644
--- a/src/components/CustomNode/styles.tsx
+++ b/src/components/Graph/CustomNode/styles.tsx
@@ -74,14 +74,8 @@ export const StyledKey = styled.span<{
white-space: nowrap;
`;
-export const StyledRow = styled.span.attrs<{
- "data-type": string;
- theme: DefaultTheme;
-}>(props => ({
- style: {
- color: getTypeColor(props["data-type"], props.theme),
- },
-}))<{ "data-type": string; theme: DefaultTheme }>`
+export const StyledRow = styled.span<{ $type: string }>`
+ color: ${({ theme, $type }) => getTypeColor($type, theme)};
display: block;
overflow: hidden;
text-overflow: ellipsis;
diff --git a/src/components/Graph/index.tsx b/src/components/Graph/index.tsx
index 555a8f7b..18dc34a3 100644
--- a/src/components/Graph/index.tsx
+++ b/src/components/Graph/index.tsx
@@ -3,13 +3,13 @@ import dynamic from "next/dynamic";
import styled from "styled-components";
import { ReactZoomPanPinchRef, TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";
import { Edge, EdgeProps, ElkRoot, NodeProps } from "reaflow";
-import { CustomNode } from "src/components/CustomNode";
+import { CustomNode } from "src/components/Graph/CustomNode";
import useToggleHide from "src/hooks/useToggleHide";
+import { Loading } from "src/layout/Loading";
import useGraph from "src/store/useGraph";
import useModal from "src/store/useModal";
import useUser from "src/store/useUser";
import { NodeData } from "src/types/models";
-import { Loading } from "../../layout/Loading";
import { ErrorView } from "./ErrorView";
import { PremiumView } from "./PremiumView";
@@ -68,16 +68,13 @@ const StyledEditorWrapper = styled.div<{ $widget: boolean }>`
}
`;
-export const Graph = ({ isWidget = false }: GraphProps) => {
+const GraphCanvas = ({ isWidget }: { isWidget: boolean }) => {
const { validateHiddenNodes } = useToggleHide();
- const isPremium = useUser(state => state.premium);
const setLoading = useGraph(state => state.setLoading);
- const setZoomPanPinch = useGraph(state => state.setZoomPanPinch);
const centerView = useGraph(state => state.centerView);
const setSelectedNode = useGraph(state => state.setSelectedNode);
const setVisible = useModal(state => state.setVisible);
-
- const loading = useGraph(state => state.loading);
+ const isPremium = useUser(state => state.premium);
const direction = useGraph(state => state.direction);
const nodes = useGraph(state => state.nodes);
const edges = useGraph(state => state.edges);
@@ -93,13 +90,6 @@ export const Graph = ({ isWidget = false }: GraphProps) => {
[setSelectedNode, setVisible]
);
- const onInit = React.useCallback(
- (ref: ReactZoomPanPinchRef) => {
- setZoomPanPinch(ref);
- },
- [setZoomPanPinch]
- );
-
const onLayoutChange = React.useCallback(
(layout: ElkRoot) => {
if (layout.width && layout.height) {
@@ -128,7 +118,7 @@ export const Graph = ({ isWidget = false }: GraphProps) => {
const memoizedNode = React.useCallback(
(props: JSX.IntrinsicAttributes & NodeProps<any>) => (
// @ts-ignore
- <CustomNode {...props} onClick={handleNodeClick} animated={false} />
+ <CustomNode {...props} onClick={handleNodeClick} />
),
[handleNodeClick]
);
@@ -146,9 +136,49 @@ export const Graph = ({ isWidget = false }: GraphProps) => {
if (!isPremium) return <PremiumView />;
}
+ return (
+ <Canvas
+ className="jsoncrack-canvas"
+ nodes={nodes}
+ edges={edges}
+ maxHeight={paneHeight}
+ maxWidth={paneWidth}
+ height={paneHeight}
+ width={paneWidth}
+ direction={direction}
+ layoutOptions={{
+ "elk.layered.compaction.postCompaction.strategy": "EDGE_LENGTH",
+ "elk.layered.nodePlacement.strategy": "NETWORK_SIMPLEX",
+ }}
+ onLayoutChange={onLayoutChange}
+ onCanvasClick={onCanvasClick}
+ node={memoizedNode}
+ edge={memoizedEdge}
+ key={direction}
+ pannable={false}
+ zoomable={false}
+ animated={false}
+ readonly={true}
+ dragEdge={null}
+ dragNode={null}
+ fit={true}
+ />
+ );
+};
+
+export const Graph = ({ isWidget = false }: GraphProps) => {
+ const setZoomPanPinch = useGraph(state => state.setZoomPanPinch);
+
+ const onInit = React.useCallback(
+ (ref: ReactZoomPanPinchRef) => {
+ setZoomPanPinch(ref);
+ },
+ [setZoomPanPinch]
+ );
+
return (
<>
- <Loading message="Painting graph..." loading={loading} />
+ <Loading message="Painting graph..." />
<StyledEditorWrapper
onClick={() => {
if ("activeElement" in document) (document.activeElement as HTMLElement)?.blur();
@@ -161,7 +191,7 @@ export const Graph = ({ isWidget = false }: GraphProps) => {
minScale={0.05}
initialScale={0.4}
wheel={{ step: 0.04 }}
- zoomAnimation={{ animationType: "linear" }}
+ zoomAnimation={{ disabled: true }}
doubleClick={{ disabled: true }}
onInit={onInit}
onPanning={ref => ref.instance.wrapperComponent?.classList.add("dragging")}
@@ -172,35 +202,9 @@ export const Graph = ({ isWidget = false }: GraphProps) => {
width: "100%",
height: "100%",
overflow: "hidden",
- display: loading ? "none" : "block",
}}
>
- <Canvas
- className="jsoncrack-canvas"
- nodes={nodes}
- edges={edges}
- maxHeight={paneHeight}
- maxWidth={paneWidth}
- height={paneHeight}
- width={paneWidth}
- direction={direction}
- layoutOptions={{
- "elk.layered.compaction.postCompaction.strategy": "EDGE_LENGTH",
- "elk.layered.nodePlacement.strategy": "NETWORK_SIMPLEX",
- }}
- onLayoutChange={onLayoutChange}
- onCanvasClick={onCanvasClick}
- node={memoizedNode}
- edge={memoizedEdge}
- key={direction}
- pannable={false}
- zoomable={false}
- animated={false}
- readonly={true}
- dragEdge={null}
- dragNode={null}
- fit={true}
- />
+ <GraphCanvas isWidget={isWidget} />
</TransformComponent>
</TransformWrapper>
</StyledEditorWrapper>
diff --git a/src/layout/Loading/index.tsx b/src/layout/Loading/index.tsx
index 16d8c41e..2b243bfa 100644
--- a/src/layout/Loading/index.tsx
+++ b/src/layout/Loading/index.tsx
@@ -1,6 +1,7 @@
import React from "react";
import styled, { keyframes } from "styled-components";
import { Center, Image, Stack } from "@mantine/core";
+import useGraph from "src/store/useGraph";
import { JSONCrackLogo } from "../JsonCrackLogo";
interface LoadingProps {
@@ -46,14 +47,18 @@ const StyledMessage = styled.div`
font-weight: 600;
`;
-export const Loading: React.FC<LoadingProps> = ({ loading = true, message }) => (
- <Center mx="auto">
- <StyledLoading $visible={loading}>
- <Stack>
- <Image mx="auto" width="6.5em" src="./assets/rocket_ship.webp" alt="loading image" />
- <JSONCrackLogo fontSize="2rem" />
- <StyledMessage>{message ?? "Preparing the environment for you..."}</StyledMessage>
- </Stack>
- </StyledLoading>
- </Center>
-);
+export const Loading: React.FC<LoadingProps> = ({ loading: innerLoading = false, message }) => {
+ const loading = useGraph(state => state.loading || innerLoading);
+
+ return (
+ <Center mx="auto">
+ <StyledLoading $visible={loading}>
+ <Stack>
+ <Image mx="auto" width="6.5em" src="./assets/rocket_ship.webp" alt="loading image" />
+ <JSONCrackLogo fontSize="2rem" />
+ <StyledMessage>{message ?? "Preparing the environment for you..."}</StyledMessage>
+ </Stack>
+ </StyledLoading>
+ </Center>
+ );
+};
diff --git a/src/store/useFile.ts b/src/store/useFile.ts
index 46b38116..3f4bd85e 100644
--- a/src/store/useFile.ts
+++ b/src/store/useFile.ts
@@ -130,6 +130,7 @@ const useFile = create<FileStates & JsonActions>()((set, get) => ({
if (contents && contents.length < 80_000 && !isIframe() && !isFetchURL) {
sessionStorage.setItem("content", contents);
sessionStorage.setItem("format", get().format);
+ set({ hasChanges: true });
}
debouncedUpdateJson(json);