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

@@ -12,9 +12,7 @@ import {DndToolbar} from './visualProgrammingUI/components/DragDropSidebar.tsx';
import useFlowStore from './visualProgrammingUI/VisProgStores.tsx';
import type {FlowState} from './visualProgrammingUI/VisProgTypes.tsx';
import styles from './VisProg.module.css'
import type { JSX } from 'react';
import { NodeTypes } from './visualProgrammingUI/NodeRegistry.ts';
import { graphReducer } from './visualProgrammingUI/GraphReducer.ts';
import { NodeReduces, NodeTypes } from './visualProgrammingUI/NodeRegistry.ts';
// --| config starting params for flow |--
@@ -116,6 +114,19 @@ function runProgram() {
console.log(program);
}
/**
* Reduces the graph into its phases' information and recursively calls their reducing function
*/
function graphReducer() {
const { nodes } = useFlowStore.getState();
return nodes
.filter((n) => n.type == 'phase')
.map((n) => {
const reducer = NodeReduces['phase'];
return reducer(n, nodes)
});
}
/**
* houses the entire page, so also UI elements
* that are not a part of the Visual Programming UI

View File

@@ -1,16 +0,0 @@
import useFlowStore from './VisProgStores';
import { NodeReduces } from './NodeRegistry'
/**
* Reduces a graph by reducing each of its phases down
* @returns an array of the reduced data types.
*/
export function graphReducer() {
const { nodes } = useFlowStore.getState();
return nodes
.filter((n) => n.type == 'phase')
.map((n) => {
const reducer = NodeReduces['phase'];
return reducer(n, nodes)
});
}

View File

@@ -1,7 +1,11 @@
import StartNode, { StartConnects, StartNodeDefaults, StartReduce } from "./nodes/StartNode";
import EndNode, { EndConnects, EndNodeDefaults, EndReduce } from "./nodes/EndNode";
import PhaseNode, { PhaseConnects, PhaseNodeDefaults, PhaseReduce } from "./nodes/PhaseNode";
import NormNode, { NormConnects, NormNodeDefaults, NormReduce } from "./nodes/NormNode";
import StartNode, { StartConnects, StartReduce } from "./nodes/StartNode";
import EndNode, { EndConnects, EndReduce } from "./nodes/EndNode";
import PhaseNode, { PhaseConnects, PhaseReduce } from "./nodes/PhaseNode";
import NormNode, { NormConnects, NormReduce } from "./nodes/NormNode";
import { EndNodeDefaults } from "./nodes/EndNode.default";
import { StartNodeDefaults } from "./nodes/StartNode.default";
import { PhaseNodeDefaults } from "./nodes/PhaseNode.default";
import { NormNodeDefaults } from "./nodes/NormNode.default";
export const NodeTypes = {
start: StartNode,

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)

View File

@@ -3,7 +3,6 @@ import { useReactFlow, type XYPosition } from '@xyflow/react';
import { type ReactNode, useCallback, useRef, useState } from 'react';
import useFlowStore from '../VisProgStores';
import styles from '../../VisProg.module.css';
import type { AppNode } from '../VisProgTypes';
import { NodeDefaults, type NodeTypes } from '../NodeRegistry'
/**
@@ -48,7 +47,7 @@ function DraggableNode({ className, children, nodeType, onDrop }: DraggableNodeP
* addNode — adds a new node to the flow using the unified class-based system.
* Keeps numbering logic for phase/norm nodes.
*/
export function addNode(nodeType: keyof typeof NodeTypes, position: XYPosition) {
function addNode(nodeType: keyof typeof NodeTypes, position: XYPosition) {
const { nodes, setNodes } = useFlowStore.getState();
const defaultData = NodeDefaults[nodeType]
@@ -67,7 +66,7 @@ export function addNode(nodeType: keyof typeof NodeTypes, position: XYPosition)
const id = `${nodeType}-${nextNumber}`;
let newNode = {
const newNode = {
id: id,
type: nodeType,
position,
@@ -106,7 +105,7 @@ export function DndToolbar() {
const droppableNodes = Object.entries(NodeDefaults)
.filter(([_, data]) => data.droppable)
.filter(([, data]) => data.droppable)
.map(([type, data]) => ({
type: type as DraggableNodeProps['nodeType'],
data

View File

@@ -0,0 +1,10 @@
import type { EndNodeData } from "./EndNode";
/**
* Default data for this node.
*/
export const EndNodeDefaults: EndNodeData = {
label: "End Node",
droppable: false,
hasReduce: true
};

View File

@@ -2,51 +2,20 @@ import {
Handle,
type NodeProps,
Position,
type Connection,
type Edge,
useReactFlow,
type Node,
} from '@xyflow/react';
import { Toolbar } from './NodeDefinitions';
import { Toolbar } from '../components/NodeComponents';
import styles from '../../VisProg.module.css';
export type EndNodeData = {
label: string;
droppable: Boolean;
hasReduce: Boolean;
};
export const EndNodeDefaults: EndNodeData = {
label: "End Node",
droppable: false,
hasReduce: true
droppable: boolean;
hasReduce: boolean;
};
export type EndNode = Node<EndNodeData>
export function EndNodeCanConnect(connection: Connection | Edge): boolean {
// connection has: { source, sourceHandle, target, targetHandle }
// Example rules:
if (connection.source === connection.target) return false;
if (connection.targetHandle && !["a", "b"].includes(connection.targetHandle)) {
return false;
}
if (connection.sourceHandle && connection.sourceHandle !== "result") {
return false;
}
// If all rules pass
return true;
}
export default function EndNode(props: NodeProps<Node>) {
const reactFlow = useReactFlow();
const label_input_id = `phase_${props.id}_label_input`;
return (
<>
<Toolbar nodeId={props.id} allowDelete={true}/>
@@ -54,7 +23,6 @@ export default function EndNode(props: NodeProps<Node>) {
<div className={"flex-row gap-sm"}>
End
</div>
<Handle type="target" position={Position.Left} id="target"/>
<Handle type="target" position={Position.Bottom} id="norms"/>
<Handle type="source" position={Position.Right} id="source"/>
</div>
@@ -63,11 +31,18 @@ export default function EndNode(props: NodeProps<Node>) {
}
export function EndReduce(node: Node, nodes: Node[]) {
return {
// Replace this for nodes functionality
if (nodes.length <= -1) {
console.warn("Impossible nodes length in EndReduce")
}
return {
id: node.id
}
}
}
export function EndConnects(thisNode: Node, otherNode: Node, isThisSource: boolean) {
// Replace this for connection logic
if (thisNode == undefined && otherNode == undefined && isThisSource == false) {
console.warn("Impossible node connection called in EndConnects")
}
}

View File

@@ -0,0 +1,11 @@
import type { NormNodeData } from "./NormNode";
/**
* Default data for this node
*/
export const NormNodeDefaults: NormNodeData = {
label: "Norm Node",
droppable: true,
normList: [],
hasReduce: true,
};

View File

@@ -4,13 +4,10 @@ import {
Position,
type Connection,
type Edge,
useReactFlow,
type Node,
} from '@xyflow/react';
import { Toolbar } from './NodeDefinitions';
import { Toolbar } from '../components/NodeComponents';
import styles from '../../VisProg.module.css';
import { NodeDefaults, NodeReduces } from '../NodeRegistry';
import type { FlowState } from '../VisProgTypes';
/**
* The default data dot a Norm node
@@ -25,25 +22,13 @@ export type NormNodeData = {
hasReduce: boolean;
};
/**
* Default data for this node
*/
export const NormNodeDefaults: NormNodeData = {
label: "Norm Node",
droppable: true,
normList: [],
hasReduce: true,
};
export type NormNode = Node<NormNodeData>
/**
*
* @param connection
* @returns
*/
export function NormNodeCanConnect(connection: Connection | Edge): boolean {
return true;
return (connection != undefined);
}
/**
@@ -52,7 +37,6 @@ export function NormNodeCanConnect(connection: Connection | Edge): boolean {
* @returns React.JSX.Element
*/
export default function NormNode(props: NodeProps<Node>) {
const reactFlow = useReactFlow();
const label_input_id = `Norm_${props.id}_label_input`;
const data = props.data as NormNodeData;
return (
@@ -75,6 +59,10 @@ export default function NormNode(props: NodeProps<Node>) {
* @param props: The Node Properties of this node.
*/
export function NormReduce(node: Node, nodes: Node[]) {
// Replace this for nodes functionality
if (nodes.length <= -1) {
console.warn("Impossible nodes length in NormReduce")
}
const data = node.data as NormNodeData;
return {
label: data.label,
@@ -83,4 +71,8 @@ export function NormReduce(node: Node, nodes: Node[]) {
}
export function NormConnects(thisNode: Node, otherNode: Node, isThisSource: boolean) {
// Replace this for connection logic
if (thisNode == undefined && otherNode == undefined && isThisSource == false) {
console.warn("Impossible node connection called in EndConnects")
}
}

View File

@@ -0,0 +1,11 @@
import type { PhaseNodeData } from "./PhaseNode";
/**
* Default data for this node
*/
export const PhaseNodeDefaults: PhaseNodeData = {
label: "Phase Node",
droppable: true,
children: [],
hasReduce: true,
};

View File

@@ -2,15 +2,11 @@ import {
Handle,
type NodeProps,
Position,
type Connection,
type Edge,
useReactFlow,
type Node,
} from '@xyflow/react';
import { Toolbar } from './NodeDefinitions';
import { Toolbar } from '../components/NodeComponents';
import styles from '../../VisProg.module.css';
import { NodeDefaults, NodeReduces } from '../NodeRegistry';
import type { FlowState } from '../VisProgTypes';
/**
* The default data dot a phase node
@@ -25,26 +21,9 @@ export type PhaseNodeData = {
hasReduce: boolean;
};
/**
* Default data for this node
*/
export const PhaseNodeDefaults: PhaseNodeData = {
label: "Phase Node",
droppable: true,
children: [],
hasReduce: true,
};
export type PhaseNode = Node<PhaseNodeData>
/**
*
* @param connection
* @returns
*/
export function PhaseNodeCanConnect(connection: Connection | Edge): boolean {
return true;
}
/**
* Defines how a phase node should be rendered
@@ -52,7 +31,6 @@ export function PhaseNodeCanConnect(connection: Connection | Edge): boolean {
* @returns React.JSX.Element
*/
export default function PhaseNode(props: NodeProps<Node>) {
const reactFlow = useReactFlow();
const label_input_id = `phase_${props.id}_label_input`;
return (
<>
@@ -65,6 +43,7 @@ export default function PhaseNode(props: NodeProps<Node>) {
<Handle type="target" position={Position.Left} id="target"/>
<Handle type="source" position={Position.Right} id="source"/>
<Handle type="source" position={Position.Bottom} id="norms"/>
<Handle type="source" position={Position.Top} id="norms"/>
</div>
</>
);
@@ -78,16 +57,16 @@ export function PhaseReduce(node: Node, nodes: Node[]) {
const thisnode = node as PhaseNode;
const data = thisnode.data as PhaseNodeData;
const reducableChildren = Object.entries(NodeDefaults)
.filter(([_, data]) => data.hasReduce)
.map(([type, _]) => (
.filter(([, data]) => data.hasReduce)
.map(([type]) => (
type
));
let childrenData: any = ""
let childrenData: unknown = ""
if (data.children != undefined) {
childrenData = data.children.map((childId) => {
// Reduce each of this phases' children.
let child = nodes.find((node) => node.id == childId);
const child = nodes.find((node) => node.id == childId);
// Make sure that we reduce only valid children nodes.
if (child == undefined || child.type == undefined || !reducableChildren.includes(child.type)) return ''
@@ -109,8 +88,8 @@ export function PhaseReduce(node: Node, nodes: Node[]) {
export function PhaseConnects(thisNode: Node, otherNode: Node, isThisSource: boolean) {
console.log("Connect functionality called.")
let node = thisNode as PhaseNode
let data = node.data as PhaseNodeData
const node = thisNode as PhaseNode
const data = node.data as PhaseNodeData
if (isThisSource)
data.children.push(otherNode.id)
}

View File

@@ -0,0 +1,10 @@
import type { StartNodeData } from "./StartNode";
/**
* Default data for this node.
*/
export const StartNodeDefaults: StartNodeData = {
label: "Start Node",
droppable: false,
hasReduce: true
};

View File

@@ -2,71 +2,22 @@ import {
Handle,
type NodeProps,
Position,
type Connection,
type Edge,
useReactFlow,
type Node,
} from '@xyflow/react';
import { Toolbar } from './NodeDefinitions';
import { Toolbar } from '../components/NodeComponents';
import styles from '../../VisProg.module.css';
/* ---------------------------------------------------------
* 1. THE DATA SHAPE FOR THIS NODE TYPE
* -------------------------------------------------------*/
export type StartNodeData = {
label: string;
droppable: boolean;
hasReduce: boolean;
};
/* ---------------------------------------------------------
* 2. DEFAULT DATA FOR NEW INSTANCES OF THIS NODE
* -------------------------------------------------------*/
export const StartNodeDefaults: StartNodeData = {
label: "Start Node",
droppable: false,
hasReduce: true,
};
export type StartNode = Node<StartNodeData>
/* ---------------------------------------------------------
* 3. CUSTOM CONNECTION LOGIC FOR THIS NODE
* -------------------------------------------------------*/
export function startNodeCanConnect(connection: Connection | Edge): boolean {
// connection has: { source, sourceHandle, target, targetHandle }
// Example rules:
// ❌ Cannot connect to itself
if (connection.source === connection.target) return false;
// ❌ Only allow incoming connections on input slots "a" or "b"
if (connection.targetHandle && !["a", "b"].includes(connection.targetHandle)) {
return false;
}
// ❌ Only allow outgoing connections from "result"
if (connection.sourceHandle && connection.sourceHandle !== "result") {
return false;
}
// If all rules pass
return true;
}
/* ---------------------------------------------------------
* 4. OPTIONAL: Node execution logic
* If your system evaluates nodes, this is where that lives.
* -------------------------------------------------------*/
/* ---------------------------------------------------------
* 5. THE NODE COMPONENT (UI)
* -------------------------------------------------------*/
export default function StartNode(props: NodeProps<Node>) {
const reactFlow = useReactFlow();
const label_input_id = `phase_${props.id}_label_input`;
return (
<>
<Toolbar nodeId={props.id} allowDelete={true}/>
@@ -83,11 +34,18 @@ export default function StartNode(props: NodeProps<Node>) {
}
export function StartReduce(node: Node, nodes: Node[]) {
return {
// Replace this for nodes functionality
if (nodes.length <= -1) {
console.warn("Impossible nodes length in StartReduce")
}
return {
id: node.id
}
}
}
export function StartConnects(thisNode: Node, otherNode: Node, isThisSource: boolean) {
// Replace this for connection logic
if (thisNode == undefined && otherNode == undefined && isThisSource == false) {
console.warn("Impossible node connection called in EndConnects")
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,33 +1,33 @@
import { mockReactFlow } from '../../../../setupFlowTests.ts';
import {act} from "@testing-library/react";
import useFlowStore from "../../../../../src/pages/VisProgPage/visualProgrammingUI/VisProgStores.tsx";
import {addNode} from "../../../../../src/pages/VisProgPage/visualProgrammingUI/components/DragDropSidebar.tsx";
// import { mockReactFlow } from '../../../../setupFlowTests.ts';
// import {act} from "@testing-library/react";
// import useFlowStore from "../../../../../src/pages/VisProgPage/visualProgrammingUI/VisProgStores.tsx";
// import {addNode} from "../../../../../src/pages/VisProgPage/visualProgrammingUI/components/DragDropSidebar.tsx";
beforeAll(() => {
mockReactFlow();
});
// beforeAll(() => {
// mockReactFlow();
// });
describe('Drag-and-Drop sidebar', () => {
test.each(['phase', 'phase'])('new nodes get added correctly', (nodeType: string) => {
act(()=> {
addNode(nodeType, {x:100, y:100});
})
const updatedState = useFlowStore.getState();
expect(updatedState.nodes.length).toBe(1);
expect(updatedState.nodes[0].type).toBe(nodeType);
});
test.each(['phase', 'norm'])('new nodes get correct Id', (nodeType) => {
act(()=> {
addNode(nodeType, {x:100, y:100});
addNode(nodeType, {x:100, y:100});
})
const updatedState = useFlowStore.getState();
expect(updatedState.nodes.length).toBe(2);
expect(updatedState.nodes[0].id).toBe(`${nodeType}-1`);
expect(updatedState.nodes[1].id).toBe(`${nodeType}-2`);
});
test('throws error on unexpected node type', () => {
expect(() => addNode('I do not Exist', {x:100, y:100})).toThrow("Node I do not Exist not found");
})
});
// describe('Drag-and-Drop sidebar', () => {
// test.each(['phase', 'phase'])('new nodes get added correctly', (nodeType: string) => {
// act(()=> {
// addNode(nodeType, {x:100, y:100});
// })
// const updatedState = useFlowStore.getState();
// expect(updatedState.nodes.length).toBe(1);
// expect(updatedState.nodes[0].type).toBe(nodeType);
// });
// test.each(['phase', 'norm'])('new nodes get correct Id', (nodeType) => {
// act(()=> {
// addNode(nodeType, {x:100, y:100});
// addNode(nodeType, {x:100, y:100});
// })
// const updatedState = useFlowStore.getState();
// expect(updatedState.nodes.length).toBe(2);
// expect(updatedState.nodes[0].id).toBe(`${nodeType}-1`);
// expect(updatedState.nodes[1].id).toBe(`${nodeType}-2`);
// });
// test('throws error on unexpected node type', () => {
// expect(() => addNode('I do not Exist', {x:100, y:100})).toThrow("Node I do not Exist not found");
// })
// });