Skip to content

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

refactor

AykutSaracb1e7361aa4be
diff --git a/src/services/widget.ts b/src/services/widget.ts
deleted file mode 100644
index 3e36e4af..00000000
--- a/src/services/widget.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-import { altogic, AltogicResponse } from "src/api/altogic";
-
-const getPartnerStatus = async (
- parentURL: string
-): Promise<AltogicResponse<{ premium: boolean }>> => {
- return await altogic.endpoint.get("/partner", {}, { parentURL });
-};
-
-export { getPartnerStatus };
diff --git a/src/utils/core/addEdgeToGraph.ts b/src/utils/core/addEdgeToGraph.ts
index edbb9ff6..b0379d25 100644
--- a/src/utils/core/addEdgeToGraph.ts
+++ b/src/utils/core/addEdgeToGraph.ts
@@ -1,11 +1,11 @@
import { Graph } from "../json/jsonParser";
export const addEdgeToGraph = (graph: Graph, from: string, to: string) => {
- graph.edges = graph.edges.concat([
- {
- id: `e${from}-${to}`,
- from: from,
- to: to,
- },
- ]);
+ const newEdge = {
+ id: `e${from}-${to}`,
+ from: from,
+ to: to,
+ };
+
+ graph.edges.push(newEdge);
};
diff --git a/src/utils/core/addNodeToGraph.ts b/src/utils/core/addNodeToGraph.ts
index f9ab73ff..8b611e99 100644
--- a/src/utils/core/addNodeToGraph.ts
+++ b/src/utils/core/addNodeToGraph.ts
@@ -9,7 +9,7 @@ type Props = {
};
export const addNodeToGraph = ({ graph, text, type = "null", isEmpty = false }: Props) => {
- let id = String(graph.nodes.length + 1);
+ const id = String(graph.nodes.length + 1);
const isParent = type === "array" || type === "object";
const { width, height } = calculateNodeSize(text, isParent);
@@ -26,7 +26,7 @@ export const addNodeToGraph = ({ graph, text, type = "null", isEmpty = false }:
},
};
- graph.nodes = graph.nodes.concat([node]);
+ graph.nodes.push(node);
return id;
};
diff --git a/src/utils/core/calculateNodeSize.ts b/src/utils/core/calculateNodeSize.ts
index cb39c3f3..d5a64299 100644
--- a/src/utils/core/calculateNodeSize.ts
+++ b/src/utils/core/calculateNodeSize.ts
@@ -17,7 +17,7 @@ export const isContentImage = (value: string | [string, string][]) => {
const sizeCache = new Map<string | [string, string][], { width: number; height: number }>();
-function calculateWidthAndHeight(str: string, single = false) {
+const calculateWidthAndHeight = (str: string, single = false) => {
if (!str) return { width: 45, height: 45 };
const dummyElement = document.createElement("div");
@@ -37,14 +37,14 @@ function calculateWidthAndHeight(str: string, single = false) {
document.body.removeChild(dummyElement);
return { width, height };
-}
+};
export const calculateNodeSize = (text: string | [string, string][], isParent = false) => {
let lines = "";
- const isFolded = useGraph.getState().foldNodes;
- const isImagePreview = useStored.getState().imagePreview;
- const isImage = isContentImage(text) && isImagePreview;
- const cacheKey = [text, isParent, isFolded].toString();
+ const { foldNodes } = useGraph.getState();
+ const { imagePreview } = useStored.getState();
+ const isImage = isContentImage(text) && imagePreview;
+ const cacheKey = [text, isParent, foldNodes].toString();
// check cache
if (sizeCache.has(cacheKey)) {
@@ -60,8 +60,8 @@ export const calculateNodeSize = (text: string | [string, string][], isParent =
let sizes = calculateWidthAndHeight(lines, typeof text === "string");
if (isImage) sizes = { width: 80, height: 80 };
- if (isFolded) sizes.width = 300;
- if (isParent && isFolded) sizes.width = 170;
+ if (foldNodes) sizes.width = 300;
+ if (isParent && foldNodes) sizes.width = 170;
if (isParent) sizes.width += 100;
if (sizes.width > 700) sizes.width = 700;
diff --git a/src/utils/graph/getNextDirection.ts b/src/utils/graph/getNextDirection.ts
index 89968ad2..c714e098 100644
--- a/src/utils/graph/getNextDirection.ts
+++ b/src/utils/graph/getNextDirection.ts
@@ -1,15 +1,6 @@
export function getNextDirection(direction: "LEFT" | "RIGHT" | "DOWN" | "UP") {
- switch (direction) {
- case "RIGHT":
- return "DOWN";
-
- case "DOWN":
- return "LEFT";
-
- case "LEFT":
- return "UP";
-
- default:
- return "RIGHT";
- }
+ if (direction === "RIGHT") return "DOWN";
+ if (direction === "DOWN") return "LEFT";
+ if (direction === "LEFT") return "UP";
+ return "RIGHT";
}
diff --git a/src/utils/graph/getOutgoers.ts b/src/utils/graph/getOutgoers.ts
index 56f79b10..42b2cc78 100644
--- a/src/utils/graph/getOutgoers.ts
+++ b/src/utils/graph/getOutgoers.ts
@@ -12,17 +12,17 @@ export const getOutgoers = (
if (initialParentNode) outgoerNodes.push(initialParentNode);
}
- const runner = (nodeId: string) => {
- const outgoerIds = edges.filter(e => e.from === nodeId).map(e => e.to);
+ const findOutgoers = (currentNodeId: string) => {
+ const outgoerIds = edges.filter(e => e.from === currentNodeId).map(e => e.to);
const nodeList = nodes.filter(n => {
if (parent.includes(n.id) && !matchingNodes.includes(n.id)) matchingNodes.push(n.id);
return outgoerIds.includes(n.id) && !parent.includes(n.id);
});
outgoerNodes.push(...nodeList);
- nodeList.forEach(node => runner(node.id));
+ nodeList.forEach(node => findOutgoers(node.id));
};
- runner(nodeId);
+ findOutgoers(nodeId);
return [outgoerNodes, matchingNodes];
};
diff --git a/src/utils/graph/search.ts b/src/utils/graph/search.ts
index 9ef11492..fc2508d8 100644
--- a/src/utils/graph/search.ts
+++ b/src/utils/graph/search.ts
@@ -1,19 +1,21 @@
export const searchQuery = (param: string) => {
- return document.querySelectorAll(`${param}`);
+ return document.querySelectorAll(param);
};
export const cleanupHighlight = () => {
const nodes = document.querySelectorAll("foreignObject.searched, .highlight");
- nodes?.forEach(node => {
- node.classList.remove("highlight");
- node.classList.remove("searched");
+ nodes.forEach(node => {
+ node.classList.remove("highlight", "searched");
});
};
export const highlightMatchedNodes = (nodes: NodeListOf<Element>, selectedNode: number) => {
- nodes?.forEach(node => {
- node.parentElement?.closest("foreignObject")?.classList.add("searched");
+ nodes.forEach(node => {
+ const foreignObject = node.parentElement?.closest("foreignObject");
+ if (foreignObject) {
+ foreignObject.classList.add("searched");
+ }
});
nodes[selectedNode].classList.add("highlight");
diff --git a/src/utils/json/getAllPaths.ts b/src/utils/json/getAllPaths.ts
deleted file mode 100644
index b49b6281..00000000
--- a/src/utils/json/getAllPaths.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-export function getAllPaths(jsonObj: string) {
- if (!jsonObj) return [];
-
- jsonObj = JSON.parse(jsonObj);
- let paths = [] as string[];
-
- function traverse(obj: string | object, path: string[]) {
- if (typeof obj !== "object") {
- paths.push(path.join("."));
- return;
- }
-
- for (let key in obj) {
- traverse(obj[key], path.concat(key));
- }
- }
-
- traverse(jsonObj, []);
-
- return paths;
-}
diff --git a/src/utils/json/getObjectValues.ts b/src/utils/json/getObjectValues.ts
deleted file mode 100644
index eb222437..00000000
--- a/src/utils/json/getObjectValues.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-export function getObjectValues(obj: object) {
- if (!obj) return [];
- let values = [] as string[];
-
- for (const key in obj) {
- if (typeof obj[key] === "object") {
- values = values.concat(getObjectValues(obj[key]));
- } else {
- values.push(obj[key]);
- }
- }
-
- return [...new Set(values)];
-}