Skip to content

Commit

Permalink
refactor(20321): refactor transition node
Browse files Browse the repository at this point in the history
  • Loading branch information
vanch3d committed Apr 23, 2024
1 parent 4015bd6 commit 2132a6b
Showing 1 changed file with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getOutgoers, Node, NodeAddChange, XYPosition } from 'reactflow'
import { Connection, getOutgoers, Node, NodeAddChange, XYPosition } from 'reactflow'

import {
BehaviorPolicy,
Expand All @@ -20,7 +20,6 @@ import {
StateType,
TransitionData,
TransitionType,
WorkspaceAction,
WorkspaceState,
} from '@datahub/types.ts'
import { PolicyCheckErrors } from '@datahub/designer/validation.errors.ts'
Expand Down Expand Up @@ -118,11 +117,9 @@ export const loadTransitions = (
behaviorPolicy: BehaviorPolicy,
schemas: Schema[],
scripts: Script[],
store: WorkspaceState & WorkspaceAction
behaviorPolicyNode: Node<BehaviorPolicyData>
) => {
const { onNodesChange, onConnect } = store
const BehaviorPolicyNode = store.nodes.find((node) => node.id === behaviorPolicy.id)
if (!BehaviorPolicyNode)
if (behaviorPolicyNode.id !== behaviorPolicy.id)
throw new Error(
i18n.t('datahub:error.loading.connection.notFound', { type: DataHubNodeType.BEHAVIOR_POLICY }) as string
)
Expand All @@ -131,25 +128,36 @@ export const loadTransitions = (

const delta = ((Math.max(behaviorPolicy.onTransitions?.length || 0, 1) - 1) * CANVAS_POSITION.Transition.y) / 2
const position: XYPosition = {
x: BehaviorPolicyNode.position.x + CANVAS_POSITION.Transition.x,
y: BehaviorPolicyNode.position.y - CANVAS_POSITION.Transition.y - delta,
x: behaviorPolicyNode.position.x + CANVAS_POSITION.Transition.x,
y: behaviorPolicyNode.position.y - CANVAS_POSITION.Transition.y - delta,
}

const shiftBottom = () => {
position.y += CANVAS_POSITION.Transition.y
return position
}

const newNodes: (NodeAddChange | Connection)[] = []
for (const behaviorPolicyTransition of behaviorPolicy.onTransitions || []) {
const transitionNode: Node<TransitionData> = {
id: getNodeId(),
type: DataHubNodeType.TRANSITION,
position: { ...shiftBottom() },
data: extractEventStates(model, behaviorPolicyTransition),
}

onNodesChange([{ item: transitionNode, type: 'add' } as NodeAddChange])
onConnect({ source: behaviorPolicy.id, target: transitionNode.id, sourceHandle: null, targetHandle: null })
loadBehaviorPolicyPipelines(behaviorPolicy, transitionNode, schemas, scripts, store)
const pipelines = loadBehaviorPolicyPipelines(behaviorPolicy, transitionNode, schemas, scripts)

newNodes.push(
{ item: transitionNode, type: 'add' } as NodeAddChange,
{
source: behaviorPolicy.id,
target: transitionNode.id,
sourceHandle: null,
targetHandle: null,
} as Connection,
...pipelines
)
}

return newNodes
}

0 comments on commit 2132a6b

Please sign in to comment.