-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathSecurityMapGraph.tsx
More file actions
186 lines (169 loc) · 6.22 KB
/
Copy pathSecurityMapGraph.tsx
File metadata and controls
186 lines (169 loc) · 6.22 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"use client";
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import type { SecurityMapNode } from "./types";
import {
MAP_COLUMNS,
columnIdForType,
countRelated,
mapEdges,
type GraphIndex,
} from "./graphIndex";
type Point = { x: number; y: number };
function fieldClass(node: SecurityMapNode, focusId: string | null, dist: Record<string, number>): string {
const bits = ["sm-field", `sm-field-${node.type}`];
if (!focusId) return bits.join(" ");
if (node.id === focusId) bits.push("is-focus");
else if (dist[node.id] === 1) bits.push("is-near");
else if (dist[node.id] === 2) bits.push("is-far");
else bits.push("is-dim");
return bits.join(" ");
}
function bezier(a: Point, b: Point): string {
const dx = Math.max(40, Math.abs(b.x - a.x) * 0.45);
const c1x = a.x + (b.x >= a.x ? dx : -dx);
const c2x = b.x + (b.x >= a.x ? -dx : dx);
return `M ${a.x} ${a.y} C ${c1x} ${a.y}, ${c2x} ${b.y}, ${b.x} ${b.y}`;
}
export function SecurityMapGraph({
index,
focusId,
dist,
visibleIds,
onSelect,
}: {
index: GraphIndex;
focusId: string | null;
dist: Record<string, number>;
visibleIds: Set<string> | null;
onSelect: (id: string) => void;
}) {
const boardRef = useRef<HTMLDivElement>(null);
const [size, setSize] = useState({ w: 0, h: 0 });
const [centers, setCenters] = useState<Record<string, Point>>({});
useLayoutEffect(() => {
const board = boardRef.current;
if (!board) return;
const measure = () => {
const rect = board.getBoundingClientRect();
const next: Record<string, Point> = {};
for (const el of board.querySelectorAll<HTMLElement>("[data-node-id]")) {
const id = el.dataset.nodeId;
if (!id) continue;
const r = el.getBoundingClientRect();
next[id] = {
x: r.left - rect.left + board.scrollLeft + r.width / 2,
y: r.top - rect.top + board.scrollTop + r.height / 2,
};
}
setCenters(next);
setSize({ w: Math.max(board.scrollWidth, 1), h: Math.max(board.scrollHeight, 1) });
};
measure();
const frame = window.requestAnimationFrame(measure);
const ro = new ResizeObserver(measure);
ro.observe(board);
board.addEventListener("scroll", measure);
window.addEventListener("resize", measure);
return () => {
window.cancelAnimationFrame(frame);
ro.disconnect();
board.removeEventListener("scroll", measure);
window.removeEventListener("resize", measure);
};
}, [index, focusId, visibleIds]);
useEffect(() => {
const board = boardRef.current;
if (!board) return;
const t = window.setTimeout(() => {
const rect = board.getBoundingClientRect();
const next: Record<string, Point> = {};
for (const el of board.querySelectorAll<HTMLElement>("[data-node-id]")) {
const id = el.dataset.nodeId;
if (!id) continue;
const r = el.getBoundingClientRect();
next[id] = {
x: r.left - rect.left + board.scrollLeft + r.width / 2,
y: r.top - rect.top + board.scrollTop + r.height / 2,
};
}
setCenters(next);
setSize({ w: Math.max(board.scrollWidth, 1), h: Math.max(board.scrollHeight, 1) });
}, 50);
return () => window.clearTimeout(t);
}, [index, focusId, visibleIds]);
const edges = useMemo(() => {
const all = mapEdges(index);
if (!visibleIds) return all;
return all.filter((edge) => visibleIds.has(edge.source) && visibleIds.has(edge.target));
}, [index, visibleIds]);
return (
<div className="sm-board" ref={boardRef}>
<svg
className="sm-wires"
width={size.w}
height={size.h}
viewBox={`0 0 ${Math.max(size.w, 1)} ${Math.max(size.h, 1)}`}
aria-hidden="true"
>
{edges.map((edge) => {
const a = centers[edge.source];
const b = centers[edge.target];
if (!a || !b) return null;
const srcCol = columnIdForType(index.nodesById[edge.source]?.type);
const tgtCol = columnIdForType(index.nodesById[edge.target]?.type);
if (!srcCol || !tgtCol || srcCol === tgtCol) return null;
let cls = "sm-wire";
if (focusId) {
const hit = edge.source === focusId || edge.target === focusId;
const near = dist[edge.source] !== undefined && dist[edge.target] !== undefined;
if (hit) cls += " is-hit";
else if (near) cls += " is-near";
else cls += " is-dim";
}
return <path key={edge.id} className={cls} d={bezier(a, b)} fill="none" />;
})}
</svg>
<div className="sm-columns">
{MAP_COLUMNS.map((col) => {
const nodes = col.types
.flatMap((type) => index.nodesByType[type] || [])
.filter((node) => !visibleIds || visibleIds.has(node.id));
return (
<section key={col.id} className={`sm-col sm-col-${col.id}`}>
<header className="sm-col-head">
<h2>{col.title}</h2>
<p>{col.blurb}</p>
</header>
<ul className="sm-col-list">
{nodes.map((node) => {
const threats = countRelated(index, node.id, "threat");
const incidents = countRelated(index, node.id, "incident");
const badge = incidents || threats;
return (
<li key={node.id}>
<button
type="button"
className={fieldClass(node, focusId, dist)}
data-node-id={node.id}
aria-pressed={focusId === node.id}
onClick={() => onSelect(node.id)}
>
<span className="sm-field-title">{node.title}</span>
{node.type !== "threat" && badge ? (
<span className="sm-field-count" title={`${threats} related threats`}>
{badge}
</span>
) : null}
</button>
</li>
);
})}
</ul>
</section>
);
})}
</div>
</div>
);
}
export type { NodeType };