From 213d2bc1836b366ed4be78216b9d8ff5ce5d0bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=B0=D1=81=D1=83=D0=BB?= Date: Wed, 25 Sep 2024 20:19:59 +0300 Subject: [PATCH] feat: add try catch, additional check for incorrect links --- src/stores/useGraphStore/index.ts | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/stores/useGraphStore/index.ts b/src/stores/useGraphStore/index.ts index 2deec1cf2..0f8c40706 100644 --- a/src/stores/useGraphStore/index.ts +++ b/src/stores/useGraphStore/index.ts @@ -182,9 +182,31 @@ export const useGraphStore = create()((set, get) => ({ nodes.push(...structuredNodes) links.push(...structuredLinks) - simulation.nodes(nodes).force('link').links(links) + try { + simulation.nodes(nodes) + + const filteredLinks = links.filter((link: Link) => { + const { target, source } = link + const simulationNodes = simulation.nodes() + + // Log the target and source ref_id for debugging + const targetRefId = (target as NodeExtended)?.ref_id || target + const sourceRefId = (source as NodeExtended)?.ref_id || source + + return ( + simulationNodes.some((n: NodeExtended) => n.ref_id === targetRefId) && + simulationNodes.some((n: NodeExtended) => n.ref_id === sourceRefId) + ) + }) + + simulation.force('link').links([]).links(filteredLinks) + + simulationHelpers.simulationRestart() + } catch (error) { + console.log(error) + // eslint-disable-next-line no-debugger + } - simulationHelpers.simulationRestart() // Add simulation node to reference (so that we can access reference on tick to update position) }, @@ -280,6 +302,8 @@ export const useGraphStore = create()((set, get) => ({ }, }, simulationCreate: (nodes, links) => { + console.log('created') + const structuredNodes = structuredClone(nodes) const structuredLinks = structuredClone(links)