From b1e7361aa4be7fb01040cbfe8e9dbd57c683a089 Mon Sep 17 00:00:00 2001 From: AykutSarac Date: Sun, 14 May 2023 20:03:35 +0300 Subject: [PATCH] refactor --- src/services/widget.ts | 9 --------- src/utils/core/addEdgeToGraph.ts | 14 +++++++------- src/utils/core/addNodeToGraph.ts | 4 ++-- src/utils/core/calculateNodeSize.ts | 16 ++++++++-------- src/utils/graph/getNextDirection.ts | 17 ++++------------- src/utils/graph/getOutgoers.ts | 8 ++++---- src/utils/graph/search.ts | 14 ++++++++------ src/utils/json/getAllPaths.ts | 21 --------------------- src/utils/json/getObjectValues.ts | 14 -------------- 9 files changed, 33 insertions(+), 84 deletions(-) delete mode 100644 src/services/widget.ts delete mode 100644 src/utils/json/getAllPaths.ts delete mode 100644 src/utils/json/getObjectValues.ts diff --git a/src/services/widget.ts b/src/services/widget.ts deleted file mode 100644 index 3e36e4a..0000000 --- a/src/services/widget.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { altogic, AltogicResponse } from "src/api/altogic"; - -const getPartnerStatus = async ( - parentURL: string -): Promise> => { - return await altogic.endpoint.get("/partner", {}, { parentURL }); -}; - -export { getPartnerStatus }; diff --git a/src/utils/core/addEdgeToGraph.ts b/src/utils/core/addEdgeToGraph.ts index edbb9ff..b0379d2 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 f9ab73f..8b611e9 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 cb39c3f..d5a6429 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(); -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 89968ad..c714e09 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 56f79b1..42b2cc7 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 9ef1149..fc2508d 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, 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 b49b628..0000000 --- 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 eb22243..0000000 --- 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)]; -}