Skip to content

Commit 7f4ea09

Browse files
Anton StandrikAnton Standrik
authored andcommitted
fix: better code
1 parent b67bda0 commit 7f4ea09

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/containers/Tenant/Query/ExecuteResult/PlanToSvgButton.tsx

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,54 @@ interface PlanToSvgButtonProps {
2121
}
2222

2323
export function PlanToSvgButton({plan, database}: PlanToSvgButtonProps) {
24-
const [svgData, setSvgData] = React.useState<string | null>(null);
2524
const [error, setError] = React.useState<string | null>(null);
25+
const [blobUrl, setBlobUrl] = React.useState<string | null>(null);
2626
const [checkPlanToSvg, {isLoading, isUninitialized}] =
2727
planToSvgQueryApi.usePlanToSvgQueryMutation();
2828

2929
React.useEffect(() => {
3030
if (!plan) {
31-
return;
31+
return undefined;
3232
}
3333

3434
checkPlanToSvg({plan, database})
3535
.unwrap()
3636
.then((result) => {
37-
setSvgData(result);
37+
const blob = new Blob([result], {type: 'image/svg+xml'});
38+
const url = URL.createObjectURL(blob);
39+
setBlobUrl(url);
3840
setError(null);
3941
})
4042
.catch((err) => {
4143
setError(JSON.stringify(err));
4244
});
43-
}, [checkPlanToSvg, database, plan]);
45+
46+
return () => {
47+
if (blobUrl) {
48+
URL.revokeObjectURL(blobUrl);
49+
}
50+
};
51+
}, [checkPlanToSvg, database, plan, blobUrl]);
4452

4553
const handleClick = React.useCallback(() => {
46-
if (svgData) {
47-
const blob = new Blob([svgData], {type: 'image/svg+xml'});
48-
const url = URL.createObjectURL(blob);
49-
window.open(url, '_blank');
54+
if (blobUrl) {
55+
window.open(blobUrl, '_blank');
5056
}
51-
}, [svgData]);
57+
}, [blobUrl]);
5258

5359
if (isUninitialized) {
5460
return null;
5561
}
5662

5763
return (
58-
<Tooltip content={i18n('text_error-plan-svg', {error}) || i18n('text_plan-svg')}>
64+
<Tooltip
65+
content={error ? i18n('text_error-plan-svg', {error}) : i18n('text_open-plan-svg')}
66+
>
5967
<Button
6068
view={getButtonView(error, isLoading)}
6169
loading={isLoading}
6270
onClick={handleClick}
63-
disabled={isLoading || !svgData}
71+
disabled={isLoading || !blobUrl}
6472
>
6573
{i18n('text_plan-svg')}
6674
<Button.Icon>

src/containers/Tenant/Query/ExecuteResult/i18n/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"title.truncated": "Truncated",
1010
"title.result": "Result",
1111
"text_plan-svg": "Plan SVG",
12-
"text_open-plan-svg": "Open query plan in new window",
12+
"text_open-plan-svg": "Open execution plan in new window",
1313
"text_error-plan-svg": "Error: {{error}}"
1414
}

0 commit comments

Comments
 (0)