1
+ import { useMemo } from 'react' ;
2
+ import { ResizePanelDetails } from './ResizePanel' ;
3
+ import { BasicNode , BasicRelationship , GraphPropertiesPanelProps } from '../../types' ;
4
+ import { LegendsChip } from './LegendsChip' ;
5
+ import GraphPropertiesTable from './GraphPropertiesTable' ;
6
+
7
+
8
+ const sortAlphabetically = ( a : string , b : string ) => a . toLowerCase ( ) . localeCompare ( b . toLowerCase ( ) ) ;
9
+
10
+ const isNode = ( item : BasicNode | BasicRelationship ) : item is BasicNode => {
11
+ return 'labels' in item && ! ( 'from' in item ) && ! ( 'to' in item ) ;
12
+ } ;
13
+
14
+ const GraphPropertiesPanel = ( { inspectedItem, newScheme } : GraphPropertiesPanelProps ) => {
15
+ const inspectedItemType = isNode ( inspectedItem ) ? 'node' : 'relationship' ;
16
+ const properties = inspectedItemType === 'node'
17
+ ? [
18
+ {
19
+ key : '<id>' ,
20
+ value : `${ ( inspectedItem as BasicNode ) . id } ` ,
21
+ type : 'String' ,
22
+ } ,
23
+ ...Object . keys ( ( inspectedItem as BasicNode ) . properties ) . map ( ( key ) => {
24
+ const value = ( inspectedItem as BasicNode ) . properties [ key ] ;
25
+ return { key : key , value : value ?? '' } ;
26
+ } ) ,
27
+ ]
28
+ : [
29
+ {
30
+ key : '<element_id>' ,
31
+ value : `${ ( inspectedItem as BasicRelationship ) . id } ` ,
32
+ type : 'String' ,
33
+ } ,
34
+ {
35
+ key : '<from>' ,
36
+ value : `${ ( inspectedItem as BasicRelationship ) . from } ` ,
37
+ type : 'String' ,
38
+ } ,
39
+ {
40
+ key : '<to>' ,
41
+ value : `${ ( inspectedItem as BasicRelationship ) . to } ` ,
42
+ type : 'String' ,
43
+ } ,
44
+ {
45
+ key : '<caption>' ,
46
+ value : `${ ( inspectedItem as BasicRelationship ) . caption ?? '' } ` ,
47
+ type : 'String' ,
48
+ } ,
49
+ ] ;
50
+ const labelsSorted = useMemo ( ( ) => {
51
+ if ( isNode ( inspectedItem ) ) {
52
+ return [ ...inspectedItem . labels ] . sort ( sortAlphabetically ) ;
53
+ }
54
+ return [ ] ;
55
+ } , [ inspectedItem ] ) ;
56
+
57
+ return (
58
+ < >
59
+ < ResizePanelDetails . Title >
60
+ < h6 className = "mr-auto" >
61
+ { inspectedItemType === 'node' ? 'Node details' : 'Relationship details' }
62
+ </ h6 >
63
+ </ ResizePanelDetails . Title >
64
+ < ResizePanelDetails . Content >
65
+ < div className = "mx-4 flex flex-row flex-wrap gap-2" >
66
+ { isNode ( inspectedItem ) ? (
67
+ labelsSorted . map ( ( label ) => (
68
+ < LegendsChip
69
+ type = "node"
70
+ key = { `node ${ label } ` }
71
+ label = { label }
72
+ scheme = { newScheme }
73
+ />
74
+ ) )
75
+ ) : (
76
+ < LegendsChip
77
+ type = "relationship"
78
+ label = { ( inspectedItem as BasicRelationship ) . caption ?? '' }
79
+ key = { `relationship ${ ( inspectedItem as BasicRelationship ) . id } ` }
80
+ scheme = { { } }
81
+ />
82
+ ) }
83
+ </ div >
84
+ < div className = "bg-palette-neutral-border-weak my-3 h-px w-full" />
85
+ < GraphPropertiesTable propertiesWithTypes = { properties } />
86
+ </ ResizePanelDetails . Content >
87
+ </ >
88
+ ) ;
89
+ }
90
+
91
+ export default GraphPropertiesPanel ;
0 commit comments