1- import { v4 as uuidv4 } from 'uuid' ;
2- import { ReactElement } from 'react' ;
3- import { MenuItem } from '@mui/material' ;
4- import { IdType } from '../../../models/IdType'
5- import { Column } from '../../../models/TableModel'
6- import { useTableStore } from '../../../store/TableStore'
7- import { useLayoutStore } from '../../../store/LayoutStore' ;
8- import { useNetworkStore } from '../../../store/NetworkStore' ;
9- import { ValueType } from '../../../models/TableModel/ValueType' ;
10- import { putNetworkSummaryToDb } from '../../../store/persist/db' ;
11- import { useWorkspaceStore } from '../../../store/WorkspaceStore' ;
12- import { useViewModelStore } from '../../../store/ViewModelStore' ;
13- import { useVisualStyleStore } from '../../../store/VisualStyleStore' ;
14- import { ValueTypeName } from '../../../models/TableModel/ValueTypeName' ;
15- import { AttributeName } from '../../../models/TableModel/AttributeName' ;
16- import { BaseMenuProps } from '../../../components/ToolBar/BaseMenuProps' ;
17- import { LayoutAlgorithm , LayoutEngine } from '../../../models/LayoutModel'
18- import {
19- createEmptyNetworkWithView , DEFAULT_ATTRIBUTE ,
20- createNodeView , createEdgeView
21- } from '../utils/createNewNetwork' ;
22- import { VisualPropertyGroup } from '../../../models/VisualStyleModel/VisualPropertyGroup' ;
23-
241// Define default columns for demo edge and node tables
252const DEMO_EDGE_TABLE_COLUMN : Column = {
263 name : DEFAULT_ATTRIBUTE ,
27- type : ValueTypeName . String
28- } ;
4+ type : ValueTypeName . String ,
5+ }
296const DEMO_NODE_TABLE_COLUMN : Column = {
307 name : DEFAULT_ATTRIBUTE ,
31- type : ValueTypeName . String
32- } ;
8+ type : ValueTypeName . String ,
9+ }
3310
3411/**
3512 * TutorialMenuItemTwo component:
@@ -39,21 +16,24 @@ const DEMO_NODE_TABLE_COLUMN: Column = {
3916 * - handleClose: A function from the parent component that will be called to close the menu.
4017 */
4118
42- export const TutorialMenuItemTwo = ( { handleClose } : BaseMenuProps ) : ReactElement => {
19+ export const TutorialMenuItemTwo = ( {
20+ handleClose,
21+ } : BaseMenuProps ) : ReactElement => {
4322 // Define constants and hooks for network and node IDs
44- const nodeOneId = '1' ;
45- const nodeTwoId = '2' ;
46- const edgeId = 'e1' ;
23+ const nodeOneId = '1'
24+ const nodeTwoId = '2'
25+ const edgeId = 'e1'
4726 const NODE_TYPE = VisualPropertyGroup . Node
4827 const EDGE_TYPE = VisualPropertyGroup . Edge
49- const newNetworkUuid = uuidv4 ( ) ;
28+ const newNetworkUuid = uuidv4 ( )
5029
5130 // Use custom hooks for state management across different stores
52- const { addNewNetwork, addNodesToNetwork, addEdgeToNetwork } = useNetworkStore ( ( state ) => ( {
53- addNewNetwork : state . add ,
54- addNodesToNetwork : state . addNodes ,
55- addEdgeToNetwork : state . addEdge ,
56- } ) ) ;
31+ const { addNewNetwork, addNodesToNetwork, addEdgeToNetwork } =
32+ useNetworkStore ( ( state ) => ( {
33+ addNewNetwork : state . add ,
34+ addNodesToNetwork : state . addNodes ,
35+ addEdgeToNetwork : state . addEdge ,
36+ } ) )
5737 const addNodeViews = useViewModelStore ( ( state ) => state . addNodeViews )
5838 const addEdgeView = useViewModelStore ( ( state ) => state . addEdgeView )
5939 const setVisualStyle = useVisualStyleStore ( ( state ) => state . add )
@@ -82,7 +62,8 @@ export const TutorialMenuItemTwo = ({ handleClose }: BaseMenuProps): ReactElemen
8262 layoutEngines . find ( ( engine ) => engine . name === defaultLayout . engineName ) ??
8363 layoutEngines [ 0 ]
8464
85- const updateNodePositions : ( // Function to update node positions after layout is applied
65+ const updateNodePositions : (
66+ // Function to update node positions after layout is applied
8667 networkId : IdType ,
8768 positions : Map < IdType , [ number , number , number ?] > ,
8869 ) => void = useViewModelStore ( ( state ) => state . updateNodePositions )
@@ -96,21 +77,29 @@ export const TutorialMenuItemTwo = ({ handleClose }: BaseMenuProps): ReactElemen
9677 /**
9778 * handleClick function:
9879 * This function is called when the menu item is clicked.
99- * It firstly creates an empty networkWithView,
80+ * It firstly creates an empty networkWithView,
10081 * then it adds two nodes and one edge to the network and applies the defualt layout.
10182 * Finally, it updates network views, tables, and the summary.
10283 */
10384 try {
104- // Create a new network with specified node and edge attributes
105- const [ newNetworkWithView , newNetworkSummary ] = await createEmptyNetworkWithView ( [ DEMO_NODE_TABLE_COLUMN ] ,
106- [ DEMO_EDGE_TABLE_COLUMN ] , newNetworkUuid ) ;
107-
108- // Update state stores with the new network and its components
109- addNetworkToWorkspace ( newNetworkUuid ) ;
110- addNewNetwork ( newNetworkWithView . network ) ;
111- setVisualStyle ( newNetworkUuid , newNetworkWithView . visualStyle ) ;
112- setTables ( newNetworkUuid , newNetworkWithView . nodeTable , newNetworkWithView . edgeTable ) ;
113- setViewModel ( newNetworkUuid , newNetworkWithView . networkViews [ 0 ] ) ;
85+ // Create a new network with specified node and edge attributes
86+ const [ newNetworkWithView , newNetworkSummary ] =
87+ await createEmptyNetworkWithView (
88+ [ DEMO_NODE_TABLE_COLUMN ] ,
89+ [ DEMO_EDGE_TABLE_COLUMN ] ,
90+ newNetworkUuid ,
91+ )
92+
93+ // Update state stores with the new network and its components
94+ addNetworkToWorkspace ( newNetworkUuid )
95+ addNewNetwork ( newNetworkWithView . network )
96+ setVisualStyle ( newNetworkUuid , newNetworkWithView . visualStyle )
97+ setTables (
98+ newNetworkUuid ,
99+ newNetworkWithView . nodeTable ,
100+ newNetworkWithView . edgeTable ,
101+ )
102+ setViewModel ( newNetworkUuid , newNetworkWithView . networkViews [ 0 ] )
114103 setCurrentNetworkId ( newNetworkUuid )
115104
116105 // Add nodes and an edge to the network
@@ -119,15 +108,22 @@ export const TutorialMenuItemTwo = ({ handleClose }: BaseMenuProps): ReactElemen
119108
120109 // Apply layout to the network
121110 setIsRunning ( true )
122- engine . apply ( newNetworkWithView . network . nodes ,
123- newNetworkWithView . network . edges , afterLayout , defaultLayout )
111+ engine . apply (
112+ newNetworkWithView . network . nodes ,
113+ newNetworkWithView . network . edges ,
114+ afterLayout ,
115+ defaultLayout ,
116+ )
124117
125118 // Update tables with node and edge attributes
126- const nodeAttr : Array < [ IdType , Record < AttributeName , ValueType > ] >
127- = [ [ nodeOneId , { [ DEFAULT_ATTRIBUTE ] : NODE_TYPE + nodeOneId } ] ,
128- [ nodeTwoId , { [ DEFAULT_ATTRIBUTE ] : NODE_TYPE + nodeTwoId } ] ] ;
129- const edgeAttr : [ IdType , Record < AttributeName , ValueType > ]
130- = [ edgeId , { [ DEFAULT_ATTRIBUTE ] : edgeId } ] ;
119+ const nodeAttr : Array < [ IdType , Record < AttributeName , ValueType > ] > = [
120+ [ nodeOneId , { [ DEFAULT_ATTRIBUTE ] : NODE_TYPE + nodeOneId } ] ,
121+ [ nodeTwoId , { [ DEFAULT_ATTRIBUTE ] : NODE_TYPE + nodeTwoId } ] ,
122+ ]
123+ const edgeAttr : [ IdType , Record < AttributeName , ValueType > ] = [
124+ edgeId ,
125+ { [ DEFAULT_ATTRIBUTE ] : edgeId } ,
126+ ]
131127 addRowsToTable ( newNetworkUuid , NODE_TYPE , nodeAttr )
132128 addRowToTable ( newNetworkUuid , EDGE_TYPE , edgeAttr )
133129
@@ -141,20 +137,19 @@ export const TutorialMenuItemTwo = ({ handleClose }: BaseMenuProps): ReactElemen
141137 // Update network summary and persist to the database
142138 await putNetworkSummaryToDb ( {
143139 ...newNetworkSummary ,
144- nodeCount : 2 , edgeCount : 1 , hasLayout : true , modificationTime : new Date ( Date . now ( ) ) ,
140+ nodeCount : 2 ,
141+ edgeCount : 1 ,
142+ hasLayout : true ,
143+ modificationTime : new Date ( Date . now ( ) ) ,
145144 } )
146145
147146 // Close the menu item after operations are complete
148- handleClose ( ) ;
147+ handleClose ( )
149148 } catch ( error ) {
150149 console . error ( error )
151150 }
152- } ;
151+ }
153152
154153 // Render a MenuItem that triggers network creation on click
155- return (
156- < MenuItem onClick = { handleClick } >
157- Create Example Network
158- </ MenuItem >
159- ) ;
160- } ;
154+ return < MenuItem onClick = { handleClick } > Create Example Network</ MenuItem >
155+ }
0 commit comments