From ba77ed4981e7c9f5fe26ee810ee1566f424e80bd Mon Sep 17 00:00:00 2001 From: afonso Date: Fri, 19 Jul 2024 14:56:45 +0100 Subject: [PATCH] CELE-32 style: Lint graphRendering code --- .../src/helpers/twoD/graphRendering.ts | 98 ++++++++++--------- .../frontend/src/helpers/twoD/twoDHelpers.ts | 17 ++-- .../frontend/src/models/workspace.ts | 16 ++- 3 files changed, 65 insertions(+), 66 deletions(-) diff --git a/applications/visualizer/frontend/src/helpers/twoD/graphRendering.ts b/applications/visualizer/frontend/src/helpers/twoD/graphRendering.ts index 1a4ee8a3..1fddb412 100644 --- a/applications/visualizer/frontend/src/helpers/twoD/graphRendering.ts +++ b/applications/visualizer/frontend/src/helpers/twoD/graphRendering.ts @@ -1,58 +1,60 @@ // src/helpers/twoD/graphDiffUtils.ts -import type { Core } from "cytoscape"; -import { createEdge, createNode } from "./twoDHelpers"; - -export const computeGraphDifferences = (cy: Core, connections: any[], workspace: any) => { - const currentNodes = new Set(cy.nodes().map((node) => node.id())); - const currentEdges = new Set(cy.edges().map((edge) => edge.id())); - - const newNodes = new Set(); - const newEdges = new Set(); - - const nodesToAdd: any[] = []; - const nodesToRemove: any[] = []; - const edgesToAdd: any[] = []; - const edgesToRemove: any[] = []; - - // Compute new nodes and edges based on the current connections and workspace state - const filteredActiveNeurons = Array.from(workspace.activeNeurons).filter((neuronId: string) => { - const neuron = workspace.availableNeurons[neuronId]; - if (!neuron) { - return false; +import type { Core, ElementDefinition, CollectionReturnValue } from 'cytoscape'; +import { createEdge, createNode } from './twoDHelpers'; +import {Connection} from "../../rest"; +import {Workspace} from "../../models"; + +export const computeGraphDifferences = (cy: Core, connections: Connection[], workspace: Workspace) => { + const currentNodes = new Set(cy.nodes().map(node => node.id())); + const currentEdges = new Set(cy.edges().map(edge => edge.id())); + + const newNodes = new Set(); + const newEdges = new Set(); + + const nodesToAdd: ElementDefinition[] = []; + const nodesToRemove: CollectionReturnValue = cy.collection(); + const edgesToAdd: ElementDefinition[] = []; + const edgesToRemove: CollectionReturnValue = cy.collection(); + + // Compute new nodes and edges based on the current connections and workspace state + const filteredActiveNeurons = Array.from(workspace.activeNeurons).filter((neuronId: string) => { + const neuron = workspace.availableNeurons[neuronId]; + if (!neuron) { + return false; + } + const nclass = neuron.nclass; + if (neuronId === nclass) { + return true; + } + return !(workspace.activeNeurons.has(neuronId) && workspace.activeNeurons.has(nclass)); + }); + + for (const nodeId of filteredActiveNeurons) { + newNodes.add(nodeId); + if (!currentNodes.has(nodeId)) { + nodesToAdd.push(createNode(nodeId, workspace.selectedNeurons.has(nodeId))); + } } - const nclass = neuron.nclass; - if (neuronId === nclass) { - return true; - } - return !(workspace.activeNeurons.has(neuronId) && workspace.activeNeurons.has(nclass)); - }); - - filteredActiveNeurons.forEach((nodeId: string) => { - newNodes.add(nodeId); - if (!currentNodes.has(nodeId)) { - nodesToAdd.push(createNode(nodeId, workspace.selectedNeurons.has(nodeId))); - } - }); - currentNodes.forEach((nodeId) => { - if (!newNodes.has(nodeId)) { - nodesToRemove.push(cy.getElementById(nodeId)); + for (const nodeId of currentNodes) { + if (!newNodes.has(nodeId)) { + nodesToRemove.merge(cy.getElementById(nodeId)); + } } - }); - connections.forEach((conn) => { - const edgeId = `${conn.pre}-${conn.post}`; - newEdges.add(edgeId); - if (!currentEdges.has(edgeId)) { - edgesToAdd.push(createEdge(conn)); + for (const conn of connections) { + const edgeId = `${conn.pre}-${conn.post}`; + newEdges.add(edgeId); + if (!currentEdges.has(edgeId)) { + edgesToAdd.push(createEdge(conn)); + } } - }); - currentEdges.forEach((edgeId) => { - if (!newEdges.has(edgeId)) { - edgesToRemove.push(cy.getElementById(edgeId)); + for (const edgeId of currentEdges) { + if (!newEdges.has(edgeId)) { + edgesToRemove.merge(cy.getElementById(edgeId)); + } } - }); - return { nodesToAdd, nodesToRemove: cy.collection(nodesToRemove), edgesToAdd, edgesToRemove: cy.collection(edgesToRemove) }; + return { nodesToAdd, nodesToRemove, edgesToAdd, edgesToRemove }; }; diff --git a/applications/visualizer/frontend/src/helpers/twoD/twoDHelpers.ts b/applications/visualizer/frontend/src/helpers/twoD/twoDHelpers.ts index c168d2a6..6adca832 100644 --- a/applications/visualizer/frontend/src/helpers/twoD/twoDHelpers.ts +++ b/applications/visualizer/frontend/src/helpers/twoD/twoDHelpers.ts @@ -1,8 +1,8 @@ -import type { Core } from "cytoscape"; +import type {Core, ElementDefinition} from "cytoscape"; import type { Connection } from "../../rest"; import type { Workspace } from "../../models/workspace.ts"; -export const createEdge = (conn: Connection) => { +export const createEdge = (conn: Connection): ElementDefinition => { return { group: "edges", data: { @@ -15,14 +15,15 @@ export const createEdge = (conn: Connection) => { }; }; -export const createNode = (nodeId: string, selected: boolean) => { - return { - group: "nodes", - data: { id: nodeId, label: nodeId }, - classes: selected ? "selected" : "", - }; +export const createNode = (nodeId: string, selected: boolean): ElementDefinition => { + return { + group: 'nodes', + data: { id: nodeId, label: nodeId }, + classes: selected ? 'selected' : '' + }; }; + export function applyLayout(cyRef: React.MutableRefObject, layout: string) { if (cyRef.current) { cyRef.current diff --git a/applications/visualizer/frontend/src/models/workspace.ts b/applications/visualizer/frontend/src/models/workspace.ts index 8b2f2c72..974ed8a0 100644 --- a/applications/visualizer/frontend/src/models/workspace.ts +++ b/applications/visualizer/frontend/src/models/workspace.ts @@ -161,21 +161,17 @@ export class Workspace { const uniqueNeurons = new Set(); // Flatten and deduplicate neurons - neuronArrays.flat().forEach((neuron) => { - uniqueNeurons.add(neuron); - // Add class neuron as well - const classNeuron = { ...neuron, name: neuron.nclass }; + for (const neuronArray of neuronArrays.flat()) { + uniqueNeurons.add(neuronArray); + const classNeuron = { ...neuronArray, name: neuronArray.nclass }; uniqueNeurons.add(classNeuron); - }); + } return produce(updatedWorkspace, (draft: Workspace) => { - // Reset the availableNeurons map draft.availableNeurons = {}; - - // Populate availableNeurons with unique neurons - uniqueNeurons.forEach((neuron) => { + for (const neuron of uniqueNeurons) { draft.availableNeurons[neuron.name] = neuron; - }); + } }); } catch (error) { console.error("Failed to fetch neurons:", error);