Skip to content

Commit a3921b7

Browse files
authored
Merge pull request #2562 from github/nora/refactor-graph
Convert Graph to a functional component
2 parents 4d8694e + 9ba5701 commit a3921b7

2 files changed

Lines changed: 39 additions & 54 deletions

File tree

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,20 @@
11
import * as React from "react";
22
import { select } from "d3";
3-
import { ResultTableProps, jumpToLocation } from "./result-table-utils";
4-
import {
5-
InterpretedResultSet,
6-
GraphInterpretationData,
7-
} from "../../common/interface-types";
3+
import { jumpToLocation } from "./result-table-utils";
84
import { graphviz, GraphvizOptions } from "d3-graphviz";
95
import { tryGetLocationFromString } from "../../common/bqrs-utils";
10-
export type GraphProps = ResultTableProps & {
11-
resultSet: InterpretedResultSet<GraphInterpretationData>;
6+
import { useCallback, useEffect } from "react";
7+
8+
type GraphProps = {
9+
graphData: string;
10+
databaseUri: string;
1211
};
1312

1413
const graphClassName = "vscode-codeql__result-tables-graph";
1514
const graphId = "graph-results";
16-
export class Graph extends React.Component<GraphProps> {
17-
constructor(props: GraphProps) {
18-
super(props);
19-
}
20-
21-
public render = (): JSX.Element => {
22-
const { resultSet, offset } = this.props;
23-
const graphData = resultSet.interpretation?.data?.dot[offset];
24-
25-
if (!graphData) {
26-
return (
27-
<>
28-
<div className={graphClassName}>Graph is not available.</div>
29-
</>
30-
);
31-
}
32-
33-
return (
34-
<>
35-
<div className={graphClassName}>
36-
<strong>Warning:</strong> The Graph Viewer is not a publicly released
37-
feature and will crash on large graphs.
38-
</div>
39-
<div id={graphId} className={graphClassName}>
40-
<span>Rendering graph...</span>
41-
</div>
42-
</>
43-
);
44-
};
45-
46-
public componentDidMount = () => {
47-
this.renderGraph();
48-
};
49-
50-
public componentDidUpdate = () => {
51-
this.renderGraph();
52-
};
53-
54-
private renderGraph = () => {
55-
const { databaseUri, resultSet, offset } = this.props;
56-
const graphData = resultSet.interpretation?.data?.dot[offset];
5715

16+
export function Graph({ graphData, databaseUri }: GraphProps) {
17+
const renderGraph = useCallback(() => {
5818
if (!graphData) {
5919
return;
6020
}
@@ -108,5 +68,29 @@ export class Graph extends React.Component<GraphProps> {
10868
}
10969
})
11070
.renderDot(graphData);
111-
};
71+
}, [graphData, databaseUri]);
72+
73+
useEffect(() => {
74+
renderGraph();
75+
}, [renderGraph]);
76+
77+
if (!graphData) {
78+
return (
79+
<>
80+
<div className={graphClassName}>Graph is not available.</div>
81+
</>
82+
);
83+
}
84+
85+
return (
86+
<>
87+
<div className={graphClassName}>
88+
<strong>Warning:</strong> The Graph Viewer is not a publicly released
89+
feature and will crash on large graphs.
90+
</div>
91+
<div id={graphId} className={graphClassName}>
92+
<span>Rendering graph...</span>
93+
</div>
94+
</>
95+
);
11296
}

extensions/ql-vscode/src/view/results/result-tables.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,12 @@ class ResultTable extends React.Component<
464464
return <AlertTable {...this.props} resultSet={sarifResultSet} />;
465465
}
466466
case "GraphInterpretationData": {
467-
const grapResultSet = {
468-
...resultSet,
469-
interpretation: { ...resultSet.interpretation, data },
470-
};
471-
return <Graph {...this.props} resultSet={grapResultSet} />;
467+
return (
468+
<Graph
469+
graphData={data?.dot[this.props.offset]}
470+
databaseUri={this.props.databaseUri}
471+
/>
472+
);
472473
}
473474
}
474475
}

0 commit comments

Comments
 (0)