Skip to content

Commit bfae001

Browse files
Pull out AlertTableHeader to a separate component/file
1 parent 4e92688 commit bfae001

2 files changed

Lines changed: 78 additions & 55 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import * as React from "react";
2+
import { useCallback } from "react";
3+
import { vscode } from "../vscode-api";
4+
import {
5+
InterpretedResultsSortColumn,
6+
InterpretedResultsSortState,
7+
SortDirection,
8+
} from "../../common/interface-types";
9+
import { nextSortDirection } from "./result-table-utils";
10+
11+
export function AlertTableHeader({
12+
sortState,
13+
}: {
14+
sortState?: InterpretedResultsSortState;
15+
}) {
16+
const sortClass = useCallback(
17+
(column: InterpretedResultsSortColumn): string => {
18+
if (sortState !== undefined && sortState.sortBy === column) {
19+
return sortState.sortDirection === SortDirection.asc
20+
? "sort-asc"
21+
: "sort-desc";
22+
} else {
23+
return "sort-none";
24+
}
25+
},
26+
[sortState],
27+
);
28+
29+
const getNextSortState = useCallback(
30+
(
31+
column: InterpretedResultsSortColumn,
32+
): InterpretedResultsSortState | undefined => {
33+
const prevDirection =
34+
sortState && sortState.sortBy === column
35+
? sortState.sortDirection
36+
: undefined;
37+
const nextDirection = nextSortDirection(prevDirection, true);
38+
return nextDirection === undefined
39+
? undefined
40+
: { sortBy: column, sortDirection: nextDirection };
41+
},
42+
[sortState],
43+
);
44+
45+
const toggleSortStateForColumn = useCallback(
46+
(column: InterpretedResultsSortColumn): void => {
47+
vscode.postMessage({
48+
t: "changeInterpretedSort",
49+
sortState: getNextSortState(column),
50+
});
51+
},
52+
[getNextSortState],
53+
);
54+
55+
const clickCallback = useCallback(
56+
() => toggleSortStateForColumn("alert-message"),
57+
[toggleSortStateForColumn],
58+
);
59+
60+
return (
61+
<thead>
62+
<tr>
63+
<th colSpan={2}></th>
64+
<th
65+
className={`${sortClass(
66+
"alert-message",
67+
)} vscode-codeql__alert-message-cell`}
68+
colSpan={3}
69+
onClick={clickCallback}
70+
>
71+
Message
72+
</th>
73+
</tr>
74+
</thead>
75+
);
76+
}

extensions/ql-vscode/src/view/results/alert-table.tsx

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
ResultTableProps,
1010
selectableZebraStripe,
1111
jumpToLocation,
12-
nextSortDirection,
1312
emptyQueryResultsMessage,
1413
} from "./result-table-utils";
1514
import { onNavigation } from "./results";
@@ -18,19 +17,16 @@ import {
1817
NavigateMsg,
1918
NavigationDirection,
2019
SarifInterpretationData,
21-
InterpretedResultsSortColumn,
22-
SortDirection,
23-
InterpretedResultsSortState,
2420
} from "../../common/interface-types";
2521
import {
2622
parseSarifPlainTextMessage,
2723
parseSarifLocation,
2824
isNoLocation,
2925
} from "../../common/sarif-utils";
30-
import { vscode } from "../vscode-api";
3126
import { isWholeFileLoc, isLineColumnLoc } from "../../common/bqrs-utils";
3227
import { ScrollIntoViewHelper } from "./scroll-into-view-helper";
3328
import { sendTelemetry } from "../common/telemetry";
29+
import { AlertTableHeader } from "./alert-table-header";
3430

3531
export type PathTableProps = ResultTableProps & {
3632
resultSet: InterpretedResultSet<SarifInterpretationData>;
@@ -74,38 +70,6 @@ export class PathTable extends React.Component<PathTableProps, PathTableState> {
7470
e.preventDefault();
7571
}
7672

77-
sortClass(column: InterpretedResultsSortColumn): string {
78-
const sortState = this.props.resultSet.interpretation.data.sortState;
79-
if (sortState !== undefined && sortState.sortBy === column) {
80-
return sortState.sortDirection === SortDirection.asc
81-
? "sort-asc"
82-
: "sort-desc";
83-
} else {
84-
return "sort-none";
85-
}
86-
}
87-
88-
getNextSortState(
89-
column: InterpretedResultsSortColumn,
90-
): InterpretedResultsSortState | undefined {
91-
const oldSortState = this.props.resultSet.interpretation.data.sortState;
92-
const prevDirection =
93-
oldSortState && oldSortState.sortBy === column
94-
? oldSortState.sortDirection
95-
: undefined;
96-
const nextDirection = nextSortDirection(prevDirection, true);
97-
return nextDirection === undefined
98-
? undefined
99-
: { sortBy: column, sortDirection: nextDirection };
100-
}
101-
102-
toggleSortStateForColumn(column: InterpretedResultsSortColumn): void {
103-
vscode.postMessage({
104-
t: "changeInterpretedSort",
105-
sortState: this.getNextSortState(column),
106-
});
107-
}
108-
10973
renderNoResults(): JSX.Element {
11074
if (this.props.nonemptyRawResults) {
11175
return (
@@ -129,23 +93,6 @@ export class PathTable extends React.Component<PathTableProps, PathTableState> {
12993
render(): JSX.Element {
13094
const { databaseUri, resultSet } = this.props;
13195

132-
const header = (
133-
<thead>
134-
<tr>
135-
<th colSpan={2}></th>
136-
<th
137-
className={`${this.sortClass(
138-
"alert-message",
139-
)} vscode-codeql__alert-message-cell`}
140-
colSpan={3}
141-
onClick={() => this.toggleSortStateForColumn("alert-message")}
142-
>
143-
Message
144-
</th>
145-
</tr>
146-
</thead>
147-
);
148-
14996
const rows: JSX.Element[] = [];
15097
const { numTruncatedResults, sourceLocationPrefix } =
15198
resultSet.interpretation;
@@ -468,7 +415,7 @@ export class PathTable extends React.Component<PathTableProps, PathTableState> {
468415

469416
return (
470417
<table className={className}>
471-
{header}
418+
<AlertTableHeader sortState={resultSet.interpretation.data.sortState} />
472419
<tbody>{rows}</tbody>
473420
</table>
474421
);

0 commit comments

Comments
 (0)