Skip to content

Commit 98d1a24

Browse files
committed
Convert Graph to a functional component
1 parent 8297751 commit 98d1a24

1 file changed

Lines changed: 33 additions & 41 deletions

File tree

  • extensions/ql-vscode/src/view/results

extensions/ql-vscode/src/view/results/graph.tsx

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,26 @@
11
import * as React from "react";
22
import { select } from "d3";
3-
import { ResultTableProps, jumpToLocation } from "./result-table-utils";
3+
import { jumpToLocation } from "./result-table-utils";
44
import {
55
InterpretedResultSet,
66
GraphInterpretationData,
77
} from "../../common/interface-types";
88
import { graphviz, GraphvizOptions } from "d3-graphviz";
99
import { tryGetLocationFromString } from "../../common/bqrs-utils";
10-
export type GraphProps = ResultTableProps & {
10+
import { useCallback, useEffect } from "react";
11+
export type GraphProps = {
1112
resultSet: InterpretedResultSet<GraphInterpretationData>;
13+
offset: number;
14+
databaseUri: string;
1215
};
1316

1417
const graphClassName = "vscode-codeql__result-tables-graph";
1518
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-
};
4919

50-
public componentDidUpdate = () => {
51-
this.renderGraph();
52-
};
20+
export function Graph({ resultSet, offset, databaseUri }: GraphProps) {
21+
const graphData = resultSet.interpretation?.data?.dot[offset];
5322

54-
private renderGraph = () => {
55-
const { databaseUri, resultSet, offset } = this.props;
23+
const renderGraph = useCallback(() => {
5624
const graphData = resultSet.interpretation?.data?.dot[offset];
5725

5826
if (!graphData) {
@@ -108,5 +76,29 @@ export class Graph extends React.Component<GraphProps> {
10876
}
10977
})
11078
.renderDot(graphData);
111-
};
79+
}, [resultSet, offset, databaseUri]);
80+
81+
useEffect(() => {
82+
renderGraph();
83+
}, [renderGraph]);
84+
85+
if (!graphData) {
86+
return (
87+
<>
88+
<div className={graphClassName}>Graph is not available.</div>
89+
</>
90+
);
91+
}
92+
93+
return (
94+
<>
95+
<div className={graphClassName}>
96+
<strong>Warning:</strong> The Graph Viewer is not a publicly released
97+
feature and will crash on large graphs.
98+
</div>
99+
<div id={graphId} className={graphClassName}>
100+
<span>Rendering graph...</span>
101+
</div>
102+
</>
103+
);
112104
}

0 commit comments

Comments
 (0)