-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathAlertTableResultRow.tsx
More file actions
147 lines (139 loc) · 4.47 KB
/
AlertTableResultRow.tsx
File metadata and controls
147 lines (139 loc) · 4.47 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
import type { Result, Run } from "sarif";
import type {
PathNode,
Result as ResultKeysResult,
ResultKey,
} from "./result-keys";
import { getAllPaths, keyToString } from "./result-keys";
import { info, listUnordered } from "./octicons";
import { selectableZebraStripe } from "./result-table-utils";
import { AlertTableDropdownIndicatorCell } from "./AlertTableDropdownIndicatorCell";
import { useCallback, useMemo } from "react";
import { SarifLocation } from "./locations/SarifLocation";
import { SarifMessageWithLocations } from "./locations/SarifMessageWithLocations";
import { AlertTablePathRow } from "./AlertTablePathRow";
import type { UserSettings } from "../../common/interface-types";
import { Badge } from "../common/Badge";
export interface Props {
result: Result;
resultIndex: number;
expanded: Set<string>;
selectedItem: undefined | ResultKey;
selectedItemRef: React.RefObject<HTMLTableRowElement | undefined>;
databaseUri: string;
sourceLocationPrefix: string;
run?: Run;
userSettings: UserSettings;
updateSelectionCallback: (
resultKey: PathNode | ResultKeysResult | undefined,
) => void;
toggleExpanded: (e: React.MouseEvent, keys: ResultKey[]) => void;
}
export function AlertTableResultRow(props: Props) {
const {
result,
resultIndex,
expanded,
selectedItem,
selectedItemRef,
databaseUri,
sourceLocationPrefix,
updateSelectionCallback,
toggleExpanded,
} = props;
const resultKey: ResultKeysResult = useMemo(
() => ({ resultIndex }),
[resultIndex],
);
const handleSarifLocationClicked = useCallback(
() => updateSelectionCallback(resultKey),
[resultKey, updateSelectionCallback],
);
const handleDropdownClick = useCallback(
(e: React.MouseEvent) => {
const indices =
getAllPaths(result).length === 1
? [resultKey, { ...resultKey, pathIndex: 0 }]
: /* if there's exactly one path, auto-expand
* the path when expanding the result */
[resultKey];
toggleExpanded(e, indices);
},
[result, resultKey, toggleExpanded],
);
const resultRowIsSelected =
selectedItem?.resultIndex === resultIndex &&
selectedItem.pathIndex === undefined;
const text = result.message.text || "[no text]";
const msg =
result.relatedLocations === undefined ? (
<span key="0">{text}</span>
) : (
<SarifMessageWithLocations
msg={text}
relatedLocations={result.relatedLocations}
sourceLocationPrefix={sourceLocationPrefix}
databaseUri={databaseUri}
onClick={handleSarifLocationClicked}
/>
);
const allPaths = getAllPaths(result);
const shortestPath = Math.min(
...allPaths.map((path) => path.locations.length),
);
const currentResultExpanded = expanded.has(keyToString(resultKey));
return (
<>
<tr
ref={
resultRowIsSelected && selectedItemRef.current !== undefined
? (selectedItemRef as React.RefObject<HTMLTableRowElement>)
: undefined
}
{...selectableZebraStripe(resultRowIsSelected, resultIndex)}
>
{result.codeFlows === undefined ? (
<>
<td className="vscode-codeql__icon-cell">{info}</td>
<td colSpan={4}>{msg}</td>
</>
) : (
<>
<AlertTableDropdownIndicatorCell
expanded={currentResultExpanded}
onClick={handleDropdownClick}
/>
<td className="vscode-codeql__icon-cell">{listUnordered}</td>
<td className="vscode-codeql__icon-cell">
<Badge title="Shortest path">{shortestPath}</Badge>
</td>
<td colSpan={3}>{msg}</td>
</>
)}
<td className="vscode-codeql__location-cell">
{result.locations && result.locations.length > 0 && (
<SarifLocation
loc={result.locations[0]}
sourceLocationPrefix={sourceLocationPrefix}
databaseUri={databaseUri}
onClick={handleSarifLocationClicked}
/>
)}
</td>
</tr>
{currentResultExpanded &&
result.codeFlows &&
allPaths.map((path, pathIndex) => (
<AlertTablePathRow
key={`${resultIndex}-${pathIndex}`}
{...props}
path={path}
pathIndex={pathIndex}
currentPathExpanded={expanded.has(
keyToString({ resultIndex, pathIndex }),
)}
/>
))}
</>
);
}