1- import { iterations } from './iterations ' ;
1+ import { states } from './states ' ;
22import { createRoot } from 'react-dom/client' ;
33import ELK from 'elkjs/lib/elk.bundled.js' ;
44import React , { useCallback , useState , useEffect , useMemo } from 'react' ;
@@ -14,7 +14,9 @@ import ReactFlow, {
1414
1515import 'reactflow/dist/style.css' ;
1616
17- const data = { nodes : iterations [ 0 ] . nodes , edges : iterations [ 0 ] . edges , combos : iterations [ 0 ] . combos } ;
17+ // First is initial state
18+ const totalIterations = states . length - 1 ;
19+ const data = { nodes : states [ 0 ] . nodes , edges : states [ 0 ] . edges , combos : states [ 0 ] . combos } ;
1820const sizeByNode = ( n ) => [ 60 + n . label . length * 5 , 30 ] ;
1921const toGroupNode = ( n ) => ( { ...n , type : 'group' , data : { label : n . label } , position : { x : 0 , y : 0 } , width : 200 , height : 200 } ) ;
2022const toRegularNode = ( n ) => ( {
@@ -183,20 +185,20 @@ const LayoutFlow = () => {
183185 const [ { preNodes, preEdges } , setPreNodesEdges ] = useState ( { preNodes : initialNodes , preEdges : initialEdges } ) ;
184186 const [ nodes , setNodes , onNodesChange ] = useNodesState ( JSON . parse ( JSON . stringify ( initialNodes ) ) ) ;
185187 const [ edges , setEdges , onEdgesChange ] = useEdgesState ( JSON . parse ( JSON . stringify ( initialEdges ) ) ) ;
186- const [ iteration , setIteration ] = useState ( 0 ) ;
188+ const [ stateIdx , setStateIdx ] = useState ( 0 ) ;
187189 const { fitView } = useReactFlow ( ) ;
188190
189191 const [ navigateTo , setNavigateTo ] = useState ( '' ) ;
190192 const [ navHistory , setNavHistory ] = useState ( [ ] ) ;
191193 const [ showOnlySelected , setShowOnlySelected ] = useState ( false ) ;
192194
193- const prevIter = ( ) => {
194- if ( iteration === 0 ) {
195+ const prevState = ( ) => {
196+ if ( stateIdx === 0 ) {
195197 return ;
196198 }
197199 let newNodes = preNodes ;
198200 let newEdges = preEdges ;
199- const toRemove = iterations [ iteration ] ;
201+ const toRemove = states [ stateIdx ] ;
200202 let toRemoveNodeIds = toRemove . nodes . concat ( toRemove . combos ) . map ( ( n ) => n . id ) ;
201203 let toRemoveEdgeIds = toRemove . edges . map ( ( n ) => toEdge ( n ) . id ) ;
202204 newNodes = newNodes . filter ( ( n ) => ! toRemoveNodeIds . includes ( n . id ) ) ;
@@ -205,21 +207,21 @@ const LayoutFlow = () => {
205207 newNodes = newNodes . concat ( ( toRemove . removedNodes || [ ] ) . map ( toRegularNode ) ) ;
206208 const edgeMap = ( toRemove . removedEdges || [ ] ) . map ( toEdge ) . reduce ( ( acc , val ) => ( { ...acc , [ val . id ] : val } ) , { } ) ;
207209 newEdges = newEdges . concat ( Object . keys ( edgeMap ) . map ( key => edgeMap [ key ] ) ) ;
208- const toHighlight = iterations [ iteration - 1 ] ;
210+ const toHighlight = states [ stateIdx - 1 ] ;
209211 const toHighlightNodeIds = toHighlight . nodes . concat ( toHighlight . combos ) . map ( ( n ) => n . id ) ;
210212 newNodes = newNodes . map ( n => ( { ...n , style : { ...n . style , backgroundColor : toHighlightNodeIds . includes ( n . id ) ? highlightColor : undefined } } ) ) ;
211- setIteration ( iteration - 1 ) ;
213+ setStateIdx ( stateIdx - 1 ) ;
212214 setPreNodesEdges ( { preNodes : newNodes , preEdges : newEdges } ) ;
213215 }
214216
215- const nextIter = ( ) => {
216- if ( iteration === iterations . length - 1 ) {
217+ const nextState = ( ) => {
218+ if ( stateIdx === states . length - 1 ) {
217219 return ;
218220 }
219221 let newNodes = preNodes ;
220222 let newEdges = preEdges ;
221- setIteration ( iteration + 1 ) ;
222- const toAdd = iterations [ iteration + 1 ] ;
223+ setStateIdx ( stateIdx + 1 ) ;
224+ const toAdd = states [ stateIdx + 1 ] ;
223225 let toRemoveNodeIds = toAdd . removedNodes . concat ( toAdd . removedCombos ) . map ( ( n ) => n . id ) ;
224226 let toRemoveEdgeIds = toAdd . removedEdges . map ( ( n ) => toEdge ( n ) . id ) ;
225227 newNodes = newNodes . filter ( ( n ) => ! toRemoveNodeIds . includes ( n . id ) ) ;
@@ -248,7 +250,9 @@ const LayoutFlow = () => {
248250
249251 useEffect ( ( ) => {
250252 layout ( { } , JSON . parse ( JSON . stringify ( preNodes ) ) , JSON . parse ( JSON . stringify ( preEdges ) ) , setNodes , setEdges , fitView , navHistory , showOnlySelected ) ;
251- } , [ preNodes , setNodes , setEdges , iteration , showOnlySelected , navHistory ] ) ;
253+ } , [ preNodes , setNodes , setEdges , stateIdx , showOnlySelected , navHistory ] ) ;
254+
255+ const stateLabel = stateIdx === 0 ? "Initial state" : `After iteration ${ stateIdx } / ${ totalIterations } ` ;
252256
253257 return (
254258 < ReactFlow
@@ -265,9 +269,9 @@ const LayoutFlow = () => {
265269 < div >
266270 < input type = "checkbox" checked = { showOnlySelected } onChange = { ( ) => setShowOnlySelected ( ! showOnlySelected ) } />
267271 < span style = { { paddingLeft : 4 , paddingRight : 4 } } > Show only selected</ span >
268- < button onClick = { ( ) => prevIter ( ) } > Prev Iter </ button >
269- < button onClick = { ( ) => nextIter ( ) } > Next Iter </ button >
270- < span style = { { paddingLeft : 4 , paddingRight : 4 } } > Iteration # { iteration + 1 } / { iterations . length } </ span >
272+ < button onClick = { ( ) => prevState ( ) } > Prev State </ button >
273+ < button onClick = { ( ) => nextState ( ) } > Next State </ button >
274+ < span style = { { paddingLeft : 4 , paddingRight : 4 } } > { stateLabel } </ span >
271275 < input placeholder = "Search Class ID" onChange = { ( e ) => setNavigateTo ( e . target . value ) } value = { navigateTo } > </ input >
272276 < button onClick = { ( ) => {
273277 navigate ( navigateTo ) ;
@@ -284,7 +288,7 @@ const LayoutFlow = () => {
284288 }
285289 </ div >
286290 < div >
287- < span > { iterations [ iteration ] . appliedRules . join ( ', ' ) } </ span >
291+ < span > { states [ stateIdx ] . appliedRules . join ( ', ' ) } </ span >
288292 </ div >
289293 </ Panel >
290294 </ ReactFlow >
0 commit comments