-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathAlertTable.tsx
More file actions
224 lines (211 loc) · 7.03 KB
/
AlertTable.tsx
File metadata and controls
224 lines (211 loc) · 7.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import type { Location, Result, Run } from "sarif";
import type {
PathNode,
Result as ResultKeysResult,
ResultKey,
} from "./result-keys";
import { getPath, getPathNode, getResult, keyToString } from "./result-keys";
import { className, jumpToLocation } from "./result-table-utils";
import { onNavigation } from "./navigation";
import type { NavigateMsg, UserSettings } from "../../common/interface-types";
import { NavigationDirection } from "../../common/interface-types";
import { isNoLocation, parseSarifLocation } from "../../common/sarif-utils";
import { sendTelemetry } from "../common/telemetry";
import { AlertTableTruncatedMessage } from "./AlertTableTruncatedMessage";
import { AlertTableResultRow } from "./AlertTableResultRow";
import type { ReactNode } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { useScrollIntoView } from "./useScrollIntoView";
type Props = {
results: Result[];
databaseUri: string;
sourceLocationPrefix: string;
run?: Run;
userSettings: UserSettings;
numTruncatedResults?: number;
header: ReactNode;
noResults?: ReactNode;
};
export function AlertTable({
results,
databaseUri,
sourceLocationPrefix,
run,
userSettings,
numTruncatedResults,
header,
noResults,
}: Props) {
const [expanded, setExpanded] = useState<Set<string>>(new Set<string>());
const [selectedItem, setSelectedItem] = useState<ResultKey | undefined>(
undefined,
);
const selectedItemRef = useRef<HTMLTableRowElement>(undefined);
useScrollIntoView(selectedItem, selectedItemRef);
/**
* Given a list of `keys`, toggle the first, and if we 'open' the
* first item, open all the rest as well. This mimics vscode's file
* explorer tree view behavior.
*/
const toggle = useCallback((e: React.MouseEvent, keys: ResultKey[]) => {
const keyStrings = keys.map(keyToString);
setExpanded((previousExpanded) => {
const expanded = new Set(previousExpanded);
if (previousExpanded.has(keyStrings[0])) {
expanded.delete(keyStrings[0]);
} else {
for (const str of keyStrings) {
expanded.add(str);
}
}
if (expanded) {
sendTelemetry("local-results-alert-table-path-expanded");
}
return expanded;
});
e.stopPropagation();
e.preventDefault();
}, []);
const getNewSelection = (
key: ResultKey | undefined,
direction: NavigationDirection,
): ResultKey => {
if (key === undefined) {
return { resultIndex: 0 };
}
const { resultIndex, pathIndex, pathNodeIndex } = key;
switch (direction) {
case NavigationDirection.up:
case NavigationDirection.down: {
const delta = direction === NavigationDirection.up ? -1 : 1;
if (key.pathNodeIndex !== undefined) {
return {
resultIndex,
pathIndex: key.pathIndex,
pathNodeIndex: key.pathNodeIndex + delta,
};
} else if (pathIndex !== undefined) {
return { resultIndex, pathIndex: pathIndex + delta };
} else {
return { resultIndex: resultIndex + delta };
}
}
case NavigationDirection.left:
if (key.pathNodeIndex !== undefined) {
return { resultIndex, pathIndex: key.pathIndex };
} else if (pathIndex !== undefined) {
return { resultIndex };
} else {
return key;
}
case NavigationDirection.right:
if (pathIndex === undefined) {
return { resultIndex, pathIndex: 0 };
} else if (pathNodeIndex === undefined) {
return { resultIndex, pathIndex, pathNodeIndex: 0 };
} else {
return key;
}
}
};
const handleNavigationEvent = useCallback(
(event: NavigateMsg) => {
const key = getNewSelection(selectedItem, event.direction);
// Check if the selected node actually exists (bounds check) and get its location if relevant
let jumpLocation: Location | undefined;
if (key.pathNodeIndex !== undefined) {
jumpLocation = getPathNode(results, key);
if (jumpLocation === undefined) {
return; // Result does not exist
}
} else if (key.pathIndex !== undefined) {
if (getPath(results, key) === undefined) {
return; // Path does not exist
}
jumpLocation = undefined; // When selecting a 'path', don't jump anywhere.
} else {
jumpLocation = getResult(results, key)?.locations?.[0];
if (jumpLocation === undefined) {
return; // Path step does not exist.
}
}
if (jumpLocation !== undefined) {
const parsedLocation = parseSarifLocation(
jumpLocation,
sourceLocationPrefix,
);
if (!isNoLocation(parsedLocation)) {
jumpToLocation(parsedLocation, databaseUri);
}
}
const newExpanded = new Set(expanded);
if (event.direction === NavigationDirection.right) {
// When stepping right, expand to ensure the selected node is visible
newExpanded.add(keyToString({ resultIndex: key.resultIndex }));
if (key.pathIndex !== undefined) {
newExpanded.add(
keyToString({
resultIndex: key.resultIndex,
pathIndex: key.pathIndex,
}),
);
}
} else if (event.direction === NavigationDirection.left) {
// When stepping left, collapse immediately
newExpanded.delete(keyToString(key));
} else {
// When stepping up or down, collapse the previous node
if (selectedItem !== undefined) {
newExpanded.delete(keyToString(selectedItem));
}
}
setExpanded(newExpanded);
setSelectedItem(key);
},
[databaseUri, expanded, results, sourceLocationPrefix, selectedItem],
);
useEffect(() => {
onNavigation.addListener(handleNavigationEvent);
return () => {
onNavigation.removeListener(handleNavigationEvent);
};
}, [handleNavigationEvent]);
const updateSelectionCallback = useCallback(
(resultKey: PathNode | ResultKeysResult | undefined) => {
setSelectedItem(resultKey);
sendTelemetry("local-results-alert-table-path-selected");
},
[],
);
if (!results?.length) {
return <>{noResults}</>;
}
return (
<table className={className}>
{header}
<tbody>
{results.map((result, resultIndex) => (
<AlertTableResultRow
key={resultIndex}
result={result}
resultIndex={resultIndex}
expanded={expanded}
selectedItem={selectedItem}
selectedItemRef={selectedItemRef}
databaseUri={databaseUri}
run={run}
userSettings={userSettings}
sourceLocationPrefix={sourceLocationPrefix}
updateSelectionCallback={updateSelectionCallback}
toggleExpanded={toggle}
/>
))}
{numTruncatedResults ? (
<AlertTableTruncatedMessage
numTruncatedResults={numTruncatedResults}
/>
) : undefined}
</tbody>
</table>
);
}