-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathsecurityMapState.ts
More file actions
152 lines (132 loc) · 4.33 KB
/
Copy pathsecurityMapState.ts
File metadata and controls
152 lines (132 loc) · 4.33 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
import { useCallback, useEffect, useMemo, useState } from "react";
import type { AssessmentState, SecurityMapGraph } from "./types";
import {
dismissLegacyNotice,
hasLegacyPosture,
isLegacyDismissed,
loadAssessment,
saveAssessment,
setControlState,
type AssessmentDocument,
} from "./assessment";
import {
ALL_VIEW_ID,
DEFAULT_VIEW_ID,
buildIndex,
hopsFrom,
listMapViews,
viewNodeIds,
type GraphIndex,
} from "./graphIndex";
function readQuery(): { focus: string | null; view: string } {
if (typeof window === "undefined") return { focus: null, view: DEFAULT_VIEW_ID };
const params = new URLSearchParams(window.location.search);
const focus = params.get("focus");
const view = params.get("view");
return {
focus: focus && focus.trim() ? focus.trim() : null,
view: view && view.trim() ? view.trim() : DEFAULT_VIEW_ID,
};
}
function writeQuery(focus: string | null, view: string) {
if (typeof window === "undefined") return;
const url = new URL(window.location.href);
if (focus) url.searchParams.set("focus", focus);
else url.searchParams.delete("focus");
if (view && view !== DEFAULT_VIEW_ID) url.searchParams.set("view", view);
else url.searchParams.delete("view");
const qs = url.searchParams.toString();
window.history.replaceState(null, "", `${url.pathname}${qs ? `?${qs}` : ""}${url.hash}`);
}
export function useSecurityMapState(graph: SecurityMapGraph) {
const index = useMemo(() => buildIndex(graph), [graph]);
const views = useMemo(() => listMapViews(graph), [graph]);
const [focusId, setFocusId] = useState<string | null>(null);
const [viewId, setViewId] = useState(DEFAULT_VIEW_ID);
const [hydrated, setHydrated] = useState(false);
const [assessment, setAssessment] = useState<AssessmentDocument>(() => ({
schemaVersion: "1.0.0",
graphSchemaVersion: graph.schemaVersion,
updatedAt: "1970-01-01T00:00:00.000Z",
controls: {},
}));
const [legacyVisible, setLegacyVisible] = useState(false);
useEffect(() => {
const initial = readQuery();
const knownView = views.some((view) => view.id === initial.view) ? initial.view : DEFAULT_VIEW_ID;
const visible = viewNodeIds(index, knownView);
const focus =
initial.focus && index.nodesById[initial.focus] && (!visible || visible.has(initial.focus))
? initial.focus
: null;
setViewId(knownView);
setFocusId(focus);
setAssessment(loadAssessment(graph));
setLegacyVisible(hasLegacyPosture() && !isLegacyDismissed());
setHydrated(true);
}, [graph, index, views]);
useEffect(() => {
if (!hydrated) return;
writeQuery(focusId, viewId);
}, [hydrated, focusId, viewId]);
const visibleIds = useMemo(() => viewNodeIds(index, viewId), [index, viewId]);
const dist = useMemo(
() => (focusId ? hopsFrom(index, focusId, 2) : (Object.create(null) as Record<string, number>)),
[index, focusId],
);
const selectNode = useCallback((id: string) => {
setFocusId((cur) => (cur === id ? null : id));
}, []);
const clearFocus = useCallback(() => {
setFocusId(null);
}, []);
const selectView = useCallback(
(id: string) => {
const next = views.some((view) => view.id === id) ? id : ALL_VIEW_ID;
const visible = viewNodeIds(index, next);
const option = views.find((view) => view.id === next);
setViewId(next);
setFocusId((cur) => {
if (cur && index.nodesById[cur] && (!visible || visible.has(cur))) return cur;
if (option?.focusNodeId && index.nodesById[option.focusNodeId]) return option.focusNodeId;
return null;
});
},
[index, views],
);
const assess = useCallback(
(controlId: string, nextState: AssessmentState) => {
setAssessment((cur) => {
const next = setControlState(cur, controlId, nextState, graph);
saveAssessment(next);
return next;
});
},
[graph],
);
const replaceAssessment = useCallback((next: AssessmentDocument) => {
saveAssessment(next);
setAssessment(next);
}, []);
const dismissLegacy = useCallback(() => {
dismissLegacyNotice();
setLegacyVisible(false);
}, []);
return {
index,
views,
viewId,
visibleIds,
focusId,
dist,
assessment,
legacyVisible,
selectNode,
selectView,
clearFocus,
assess,
replaceAssessment,
dismissLegacy,
};
}
export type { GraphIndex };