/
Dashboard.tsx
184 lines (167 loc) · 5.59 KB
/
Dashboard.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import React, { useMemo, useState } from "react";
import ReactGraph from "react-graph";
import { PageWrapper, ReactGraphWrapper } from "../common/Organisms";
import { SideBar } from "./sidebar/Sidebar";
import { SearchBar } from "./search-bar/SearchBar";
import { InfoPanel } from "./info-panel/InfoPanel";
const Dashboard = (props: any) => {
const [graphState, setGraphState] = useState({
nodes: [],
relationships: [],
});
const [addedNodes, setAddedNodes] = useState([]);
const [selectedOptions, setSelectedOptions] = useState(null);
const [checkedNodesLabels, setCheckedNodesLabels] = useState([]);
const [checkedRelationshipsLabels, setCheckedRelationshipsLabels] = useState(
[]
);
const [dataOnInspect, setDataOnInspect] = useState({
hoveredItem: {},
selectedItem: {},
});
const [styles, setStyles] = useState({ nodes: {}, relationships: {} });
const selectOptions = useMemo(
() =>
props.nodes.map((node: { properties: { name: any }; id: any }) => {
if (node.properties && node.properties.name) {
return {
label: node.properties.name,
value: node.id,
};
}
}),
// TODO: Optimize deep comparison to big data
[JSON.stringify(props.nodes)]
);
const nodesLabels = useMemo(() => {
const labels = new Set();
props.nodes.map((node: any) => {
if (node.labels) {
node.labels.map((label: string) => {
labels.add(label);
});
}
});
return Array.from(labels);
// TODO: Optimize deep comparison to big data
}, [JSON.stringify(props.nodes)]);
const relationshipsLabels = useMemo(() => {
const labels = new Set();
props.relationships.map((relationship: any) => {
if (relationship.type) {
labels.add(relationship.type);
}
});
return Array.from(labels);
// TODO: Optimize deep comparison to big data
}, [JSON.stringify(props.relationships)]);
const handleSelectChange = (selectedOption: any) => {
let selectedNodes: any;
if (selectedOption) {
selectedNodes = selectedOption.map(
// @ts-ignore
(option: any) => props.nodesIdMap[option.value]
);
}
setSelectedOptions(selectedOption);
setAddedNodes(selectedNodes);
};
const handleNodeLabelsCheckBoxChange = (event: any) => {
const { value, checked } = event.target;
let stateChecked = checkedNodesLabels;
if (checked) {
// @ts-ignore
stateChecked.push(value);
} else {
stateChecked = stateChecked.filter((e: any) => e !== value);
}
// TODO: Improve filter to reduce time complexity
const nodes = props.nodes.filter((node: any) => {
const hasNodeAnyCheckedLabelList = stateChecked.filter(
(checkLabel: any) => node.labels.includes(checkLabel)
);
return hasNodeAnyCheckedLabelList.length;
});
// @ts-ignore
setGraphState({ nodes, relationships: [] });
setCheckedNodesLabels(stateChecked);
setCheckedRelationshipsLabels([]);
setSelectedOptions(null);
setAddedNodes([]);
};
const handleRelationshipsLabelsCheckBoxChange = (event: any) => {
const { value, checked } = event.target;
let stateChecked = checkedRelationshipsLabels;
if (checked) {
// @ts-ignore
stateChecked.push(value);
// @ts-ignore
const nodes: any[] = [];
const relationships: any[] = [];
props.relationships.map((relationship: any) => {
// @ts-ignore
if (checkedRelationshipsLabels.includes(relationship.type)) {
// @ts-ignore
nodes.push(props.nodesIdMap[relationship.startNodeId]);
// @ts-ignore
nodes.push(props.nodesIdMap[relationship.endNodeId]);
relationships.push(relationship);
}
});
// @ts-ignore
setGraphState({ nodes: [...graphState.nodes, ...nodes], relationships });
setSelectedOptions(null);
setAddedNodes([]);
} else {
stateChecked = stateChecked.filter((e: any) => e !== value);
const relationships = graphState.relationships.filter(
(relationship: any) => relationship.type !== value
);
setGraphState({ ...graphState, relationships });
setSelectedOptions(null);
setAddedNodes([]);
}
setCheckedRelationshipsLabels(stateChecked);
setCheckedNodesLabels([]);
};
return (
<PageWrapper>
<div style={{ display: "flex", flex: 1 }}>
<SideBar
nodesLabels={nodesLabels}
relationshipsLabels={relationshipsLabels}
handleNodeLabelsCheckBoxChange={handleNodeLabelsCheckBoxChange}
handleRelationshipsCheckBoxChange={
handleRelationshipsLabelsCheckBoxChange
}
checkedNodesLabels={checkedNodesLabels}
checkedRelationshipsLabels={checkedRelationshipsLabels}
styles={styles}
/>
<div style={{ flex: 1 }}>
<SearchBar
selectOptions={selectOptions}
selectedOptions={selectedOptions}
handleSelectChange={handleSelectChange}
/>
<ReactGraphWrapper>
<ReactGraph
initialState={graphState}
nodes={props.nodes}
relationships={props.relationships}
addedNodes={addedNodes}
onInspect={setDataOnInspect}
onStyleChange={setStyles}
width="100%"
height="100%"
hasLegends
hasTruncatedFields
/>
</ReactGraphWrapper>
</div>
</div>
<InfoPanel dataOnInspect={dataOnInspect} styles={styles} />
</PageWrapper>
);
};
export { Dashboard };