-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectVisualizer.tsx
More file actions
166 lines (146 loc) · 5.03 KB
/
ProjectVisualizer.tsx
File metadata and controls
166 lines (146 loc) · 5.03 KB
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
import { useEffect, useRef, useState } from "react";
import { useNavigate, useSearchParams } from "react-router";
import Controls from "../components/controls/Controls.tsx";
import MetricsExtension from "../components/controls/ControlExtensions/MetricsExtension.tsx";
import FileContextMenu from "../components/contextMenu/FileContextMenu.tsx";
import type { VisualizerContext } from "../DependencyVisualizer.tsx";
import FileDetailsPane from "../components/detailsPanes/FileDetailsPane.tsx";
import { ProjectDependencyVisualizer } from "../cytoscape/projectDependencyVisualizer/index.ts";
import type {
AuditManifest,
DependencyManifest,
Metric,
} from "@stackcore/manifests";
import { useTheme } from "../../../contexts/ThemeProvider.tsx";
export default function ProjectVisualizer(props: VisualizerContext) {
const navigate = useNavigate();
const { theme } = useTheme();
const [searchParams, setSearchParams] = useSearchParams();
const containerRef = useRef<HTMLDivElement | null>(null);
const [busy, setBusy] = useState<boolean>(true);
const [projectVisualizer, setProjectVisualizer] = useState<
ProjectDependencyVisualizer | undefined
>(undefined);
const metricFromUrl = (searchParams.get("metric") || undefined) as
| Metric
| undefined;
const [metric, setMetric] = useState<Metric | undefined>(metricFromUrl);
function handleMetricChange(metric: Metric | undefined) {
if (metric) {
searchParams.set("metric", metric);
setSearchParams(searchParams);
} else {
searchParams.delete("metric");
setSearchParams(searchParams);
}
setMetric(metric);
}
const [contextMenu, setContextMenu] = useState<
{
position: { x: number; y: number };
fileDependencyManifest: DependencyManifest[string];
} | undefined
>(undefined);
const [detailsPane, setDetailsPane] = useState<
{
manifestId: number;
fileDependencyManifest: DependencyManifest[string];
fileAuditManifest: AuditManifest[string];
} | undefined
>(undefined);
// On mount useEffect
useEffect(() => {
setBusy(true);
const projectDependencyVisualizer = new ProjectDependencyVisualizer(
containerRef.current as HTMLElement,
props.dependencyManifest,
props.auditManifest,
{
theme,
defaultMetric: metric,
onAfterNodeRightClick: (value: {
position: { x: number; y: number };
filePath: string;
}) => {
setContextMenu({
position: value.position,
fileDependencyManifest: props.dependencyManifest[value.filePath],
});
},
onAfterNodeDblClick: (filePath: string) => {
const newSearchParams = new URLSearchParams(searchParams);
newSearchParams.set("fileId", filePath);
newSearchParams.delete("instanceId");
navigate(`?${newSearchParams.toString()}`);
},
},
);
setProjectVisualizer(projectDependencyVisualizer);
setBusy(false);
// Cleanup on unmount
return () => {
projectDependencyVisualizer?.cy.destroy();
setProjectVisualizer(undefined);
};
}, [props.dependencyManifest, props.auditManifest]);
// Hook to update the target metric in the graph
useEffect(() => {
if (projectVisualizer) {
projectVisualizer.setTargetMetric(metric);
}
}, [metric]);
// Hook to update highlight node in the graph
useEffect(() => {
if (projectVisualizer) {
if (props.highlightedCytoscapeRef) {
projectVisualizer.highlightNode(props.highlightedCytoscapeRef);
} else {
projectVisualizer.unhighlightNodes();
}
}
}, [props.highlightedCytoscapeRef]);
// Hook to update the theme in the graph
useEffect(() => {
if (projectVisualizer) {
projectVisualizer.updateTheme(theme);
}
}, [theme]);
return (
<div className="relative w-full h-full">
{/* This is the container for Cytoscape */}
{/* It is important to set the width and height to 100% */}
{/* Otherwise, Cytoscape will not render correctly */}
<div ref={containerRef} className="absolute w-full h-full z-10" />
<div className="absolute bottom-10 left-1/2 transform -translate-x-1/2 z-20">
<Controls
busy={busy}
cy={projectVisualizer?.cy}
onLayout={() => projectVisualizer?.layoutGraph(projectVisualizer.cy)}
>
<MetricsExtension
busy={busy}
metricState={{
metric,
setMetric: handleMetricChange,
}}
/>
</Controls>
</div>
<FileDetailsPane
context={detailsPane}
onClose={() => setDetailsPane(undefined)}
/>
<FileContextMenu
context={contextMenu}
onClose={() => setContextMenu(undefined)}
onOpenDetails={(filePath) => {
setDetailsPane({
manifestId: props.manifestId,
fileDependencyManifest: props.dependencyManifest[filePath],
fileAuditManifest: props.auditManifest[filePath],
});
}}
/>
</div>
);
}