Skip to content

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

code refactor

AykutSarac3862f9b87138
diff --git a/src/components/CustomNode/TextNode.tsx b/src/components/CustomNode/TextNode.tsx
index 52650cb0..df3a9a73 100644
--- a/src/components/CustomNode/TextNode.tsx
+++ b/src/components/CustomNode/TextNode.tsx
@@ -2,7 +2,7 @@ import React from "react";
import styled from "styled-components";
import { MdLink, MdLinkOff } from "react-icons/md";
import { CustomNodeProps } from "src/components/CustomNode";
-import { isContentImage } from "src/lib/utils/core/calculateNodeSize";
+import { isContentImage } from "src/lib/utils/graph/calculateNodeSize";
import useGraph from "src/store/useGraph";
import useStored from "src/store/useStored";
import { TextRenderer } from "./TextRenderer";
diff --git a/src/components/CustomNode/styles.tsx b/src/components/CustomNode/styles.tsx
index 5d2c2eed..4cdc0372 100644
--- a/src/components/CustomNode/styles.tsx
+++ b/src/components/CustomNode/styles.tsx
@@ -1,12 +1,6 @@
-import { Fira_Mono } from "next/font/google";
import styled, { DefaultTheme } from "styled-components";
import { LinkItUrl } from "react-linkify-it";
-
-const robotoMono = Fira_Mono({
- weight: ["500"],
- subsets: ["latin"],
- display: "swap",
-});
+import { firaMono } from "src/constants/fonts";
function getTypeColor(value: string, theme: DefaultTheme) {
if (!Number.isNaN(+value)) return theme.NODE_COLORS.INTEGER;
@@ -31,7 +25,7 @@ export const StyledForeignObject = styled.foreignObject<{
color: ${({ theme }) => theme.NODE_COLORS.TEXT};
pointer-events: none;
padding: ${({ isObject }) => isObject && "10px"};
- font-family: ${robotoMono.style.fontFamily};
+ font-family: ${firaMono.style.fontFamily};
font-weight: 500;
&.searched {
diff --git a/src/constants/customFonts.ts b/src/constants/fonts.ts
similarity index 58%
rename from src/constants/customFonts.ts
rename to src/constants/fonts.ts
index 1e249b16..c4e6afe4 100644
--- a/src/constants/customFonts.ts
+++ b/src/constants/fonts.ts
@@ -1,3 +1,4 @@
+import { Fira_Mono } from "next/font/google";
import localFont from "next/font/local";
const monaSans = localFont({
@@ -7,4 +8,10 @@ const monaSans = localFont({
fallback: ["Arial, Helvetica, sans-serif", "Tahoma, Verdana, sans-serif"],
});
-export { monaSans };
+const firaMono = Fira_Mono({
+ weight: ["500"],
+ subsets: ["latin"],
+ display: "swap",
+});
+
+export { monaSans, firaMono };
diff --git a/src/layout/EditorWrapper/index.tsx b/src/layout/EditorWrapper/index.tsx
index f87f807a..39899572 100644
--- a/src/layout/EditorWrapper/index.tsx
+++ b/src/layout/EditorWrapper/index.tsx
@@ -2,7 +2,7 @@ import React from "react";
import { ThemeProvider } from "styled-components";
import { MantineProvider, MantineThemeOverride } from "@mantine/core";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
-import { monaSans } from "src/constants/customFonts";
+import { monaSans } from "src/constants/fonts";
import { lightTheme, darkTheme } from "src/constants/theme";
import useStored from "src/store/useStored";
diff --git a/src/layout/JsonCrackLogo/index.tsx b/src/layout/JsonCrackLogo/index.tsx
index 69230712..0cfa2ae5 100644
--- a/src/layout/JsonCrackLogo/index.tsx
+++ b/src/layout/JsonCrackLogo/index.tsx
@@ -1,7 +1,7 @@
import React from "react";
import Link from "next/link";
import styled from "styled-components";
-import { monaSans } from "src/constants/customFonts";
+import { monaSans } from "src/constants/fonts";
const StyledTitle = styled.div<{ fontSize: string }>`
font-weight: 900;
diff --git a/src/lib/utils/core/addNodeToGraph.ts b/src/lib/utils/core/addNodeToGraph.ts
index faa1b42f..e2ba01e8 100644
--- a/src/lib/utils/core/addNodeToGraph.ts
+++ b/src/lib/utils/core/addNodeToGraph.ts
@@ -1,6 +1,6 @@
import { NodeType } from "jsonc-parser";
-import { Graph } from "../json/jsonParser";
-import { calculateNodeSize } from "./calculateNodeSize";
+import { calculateNodeSize } from "src/lib/utils/graph/calculateNodeSize";
+import { Graph } from "src/lib/utils/json/jsonParser";
type Props = {
graph: Graph;
diff --git a/src/lib/utils/core/traverse.ts b/src/lib/utils/core/traverse.ts
index 16c7059e..f03695c3 100644
--- a/src/lib/utils/core/traverse.ts
+++ b/src/lib/utils/core/traverse.ts
@@ -1,8 +1,8 @@
import { Node, NodeType } from "jsonc-parser";
-import { Graph, States } from "../json/jsonParser";
+import { calculateNodeSize } from "src/lib/utils/graph/calculateNodeSize";
+import { Graph, States } from "src/lib/utils/json/jsonParser";
import { addEdgeToGraph } from "./addEdgeToGraph";
import { addNodeToGraph } from "./addNodeToGraph";
-import { calculateNodeSize } from "./calculateNodeSize";
type PrimitiveOrNullType = "boolean" | "string" | "number" | "null";
diff --git a/src/lib/utils/core/calculateNodeSize.ts b/src/lib/utils/graph/calculateNodeSize.ts
similarity index 63%
rename from src/lib/utils/core/calculateNodeSize.ts
rename to src/lib/utils/graph/calculateNodeSize.ts
index cac192c9..65343593 100644
--- a/src/lib/utils/core/calculateNodeSize.ts
+++ b/src/lib/utils/graph/calculateNodeSize.ts
@@ -1,13 +1,11 @@
-import { Fira_Mono } from "next/font/google";
+import { firaMono } from "src/constants/fonts";
import useGraph from "src/store/useGraph";
import useStored from "src/store/useStored";
-const firaMono = Fira_Mono({
- weight: ["500"],
- subsets: ["latin"],
-});
+type Text = string | [string, string][];
+type Size = { width: number; height: number };
-export const isContentImage = (value: string | [string, string][]) => {
+export const isContentImage = (value: Text) => {
if (typeof value !== "string") return false;
const isImageURL = /(https?:\/\/.*\.(?:png|jpg|gif))/i.test(value);
@@ -16,7 +14,13 @@ export const isContentImage = (value: string | [string, string][]) => {
return isImageURL || isBase64;
};
-const sizeCache = new Map<string | [string, string][], { width: number; height: number }>();
+const calculateLines = (text: Text): string => {
+ if (typeof text === "string") {
+ return text;
+ } else {
+ return text.map(([k, v]) => `${k}: ${JSON.stringify(v).slice(0, 80)}`).join("\n");
+ }
+};
const calculateWidthAndHeight = (str: string, single = false) => {
if (!str) return { width: 45, height: 45 };
@@ -34,37 +38,42 @@ const calculateWidthAndHeight = (str: string, single = false) => {
dummyElement.style.fontFamily = firaMono.style.fontFamily;
document.body.appendChild(dummyElement);
- const width = dummyElement.offsetWidth + 4;
- const height = dummyElement.offsetHeight;
+ const clientRect = dummyElement.getBoundingClientRect();
+ const width = clientRect.width + 4;
+ const height = clientRect.height;
document.body.removeChild(dummyElement);
return { width, height };
};
-export const calculateNodeSize = (text: string | [string, string][], isParent = false) => {
- let lines = "";
+const sizeCache = new Map<Text, Size>();
+
+// clear cache every 2 mins
+setInterval(() => sizeCache.clear(), 120_000);
+
+export const calculateNodeSize = (text: Text, isParent = false) => {
const { foldNodes } = useGraph.getState();
const { imagePreview } = useStored.getState();
const isImage = isContentImage(text) && imagePreview;
+
const cacheKey = [text, isParent, foldNodes].toString();
- // check cache
+ // check cache if data already exists
if (sizeCache.has(cacheKey)) {
const size = sizeCache.get(cacheKey);
if (size) return size;
}
- if (typeof text !== "string") {
- lines = text.map(([k, v]) => `${k}: ${JSON.stringify(v).slice(0, 80)}`).join("\n");
- } else {
- lines = text;
- }
+ const lines = calculateLines(text);
+ const sizes = calculateWidthAndHeight(lines, typeof text === "string");
- let sizes = calculateWidthAndHeight(lines, typeof text === "string");
+ if (isImage) {
+ sizes.width = 80;
+ sizes.height = 80;
+ }
- if (isImage) sizes = { width: 80, height: 80 };
if (foldNodes) sizes.width = 300;
if (isParent && foldNodes) sizes.width = 170;
if (isParent) sizes.width += 100;
diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx
index f68cfb33..cd8622ba 100644
--- a/src/pages/_app.tsx
+++ b/src/pages/_app.tsx
@@ -5,7 +5,7 @@ import { useRouter } from "next/router";
import { ThemeProvider } from "styled-components";
import { MantineProvider, MantineThemeOverride } from "@mantine/core";
import { pageview } from "react-ga";
-import { monaSans } from "src/constants/customFonts";
+import { monaSans } from "src/constants/fonts";
import GlobalStyle from "src/constants/globalStyle";
import { lightTheme } from "src/constants/theme";