refactor: defaults should be in their own file, respecting eslint/ react standards. all tests fail, obviously.

ref: N25B-294
This commit is contained in:
Björn Otgaar
2025-11-17 16:00:36 +01:00
parent c5dc825ca3
commit 35ff58eca8
16 changed files with 1134 additions and 1201 deletions

View File

@@ -6,35 +6,32 @@ import {
reconnectEdge,
type Node,
type Edge,
type NodeChange,
type XYPosition,
} from '@xyflow/react';
import type { FlowState, AppNode } from './VisProgTypes';
import type { FlowState } from './VisProgTypes';
import { NodeDefaults, NodeConnects } from './NodeRegistry';
/**
* Create a node given the correct data
* @param type
* @param id
* @param position
* @param data
* @param type the type of the node to create
* @param id the id of the node to create
* @param position the position of the node to create
* @param data the data in the node to create
* @constructor
*/
function createNode(id: string, type: string, position: XYPosition, data: any) {
const defaultData = Object.entries(NodeDefaults).find(([t, _]) => t == type)?.[1]
function createNode(id: string, type: string, position: XYPosition, data: Record<string, unknown>) {
const defaultData = NodeDefaults[type as keyof typeof NodeDefaults]
const newData = {
id: id,
type: type,
position: position,
data: data,
}
return (defaultData == undefined) ? newData : ({...defaultData, ...newData})
return {...defaultData, ...newData}
}
//* Initial nodes, created by using createNodeInstance. */
//* Initial nodes, created by using createNode. */
const initialNodes : Node[] = [
createNode('start', 'start', {x: 100, y: 100}, {label: "Start"}),
createNode('end', 'end', {x: 370, y: 100}, {label: "End"}),
@@ -63,8 +60,8 @@ const useFlowStore = create<FlowState>((set, get) => ({
const nodes = get().nodes;
// connection has: { source, sourceHandle, target, targetHandle }
// Let's find the source and target ID's.
let sourceNode = nodes.find((n) => n.id == connection.source);
let targetNode = nodes.find((n) => n.id == connection.target);
const sourceNode = nodes.find((n) => n.id == connection.source);
const targetNode = nodes.find((n) => n.id == connection.target);
// In case the nodes weren't found, return basic functionality.
if (sourceNode == undefined || targetNode == undefined || sourceNode.type == undefined || targetNode.type == undefined) {
@@ -73,13 +70,9 @@ const useFlowStore = create<FlowState>((set, get) => ({
}
// We should find out how their data changes by calling their respective functions.
let sourceConnectFunction = Object.entries(NodeConnects).find(([t, _]) => t == sourceNode.type)?.[1]
let targetConnectFunction = Object.entries(NodeConnects).find(([t, _]) => t == targetNode.type)?.[1]
if (sourceConnectFunction == undefined || targetConnectFunction == undefined) {
set({ nodes, edges });
return;
}
const sourceConnectFunction = NodeConnects[sourceNode.type as keyof typeof NodeConnects]
const targetConnectFunction = NodeConnects[targetNode.type as keyof typeof NodeConnects]
// We're going to have to update their data based on how they want to update it.
sourceConnectFunction(sourceNode, targetNode, true)
targetConnectFunction(targetNode, sourceNode, false)