Skip to content

Commit 23e096d

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

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

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

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import React from 'react';
33
import {ArrowUpRightFromSquare} from '@gravity-ui/icons';
44
import {Button, Tooltip} from '@gravity-ui/uikit';
55

6-
import {planToSvgQueryApi} from '../../../../store/reducers/planToSvgQuery';
76
import type {QueryPlan, ScriptPlan} from '../../../../types/api/query';
87

8+
import {usePlanToSvg} from './hooks';
99
import i18n from './i18n';
1010

1111
function getButtonView(error: string | null, isLoading: boolean) {
@@ -21,34 +21,7 @@ interface PlanToSvgButtonProps {
2121
}
2222

2323
export function PlanToSvgButton({plan, database}: PlanToSvgButtonProps) {
24-
const [error, setError] = React.useState<string | null>(null);
25-
const [blobUrl, setBlobUrl] = React.useState<string | null>(null);
26-
const [checkPlanToSvg, {isLoading, isUninitialized}] =
27-
planToSvgQueryApi.usePlanToSvgQueryMutation();
28-
29-
React.useEffect(() => {
30-
if (!plan) {
31-
return undefined;
32-
}
33-
34-
checkPlanToSvg({plan, database})
35-
.unwrap()
36-
.then((result) => {
37-
const blob = new Blob([result], {type: 'image/svg+xml'});
38-
const url = URL.createObjectURL(blob);
39-
setBlobUrl(url);
40-
setError(null);
41-
})
42-
.catch((err) => {
43-
setError(JSON.stringify(err));
44-
});
45-
46-
return () => {
47-
if (blobUrl) {
48-
URL.revokeObjectURL(blobUrl);
49-
}
50-
};
51-
}, [checkPlanToSvg, database, plan, blobUrl]);
24+
const {error, blobUrl, isLoading, isUninitialized} = usePlanToSvg(database, plan);
5225

5326
const handleClick = React.useCallback(() => {
5427
if (blobUrl) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React from 'react';
2+
3+
import {planToSvgQueryApi} from '../../../../store/reducers/planToSvgQuery';
4+
import type {QueryPlan, ScriptPlan} from '../../../../types/api/query';
5+
6+
interface UsePlanToSvgResult {
7+
error: string | null;
8+
blobUrl: string | null;
9+
isLoading: boolean;
10+
isUninitialized: boolean;
11+
}
12+
13+
export function usePlanToSvg(database: string, plan?: QueryPlan | ScriptPlan): UsePlanToSvgResult {
14+
const [error, setError] = React.useState<string | null>(null);
15+
const [blobUrl, setBlobUrl] = React.useState<string | null>(null);
16+
const [getPlanToSvg, {isLoading, isUninitialized}] =
17+
planToSvgQueryApi.usePlanToSvgQueryMutation();
18+
19+
React.useEffect(() => {
20+
if (!plan) {
21+
return undefined;
22+
}
23+
24+
let currentUrl: string | null = null;
25+
26+
getPlanToSvg({plan, database})
27+
.unwrap()
28+
.then((result) => {
29+
const blob = new Blob([result], {type: 'image/svg+xml'});
30+
currentUrl = URL.createObjectURL(blob);
31+
setBlobUrl(currentUrl);
32+
setError(null);
33+
})
34+
.catch((err) => {
35+
setError(JSON.stringify(err));
36+
});
37+
38+
return () => {
39+
if (currentUrl) {
40+
URL.revokeObjectURL(currentUrl);
41+
}
42+
};
43+
}, [getPlanToSvg, database, plan]);
44+
45+
return {
46+
error,
47+
blobUrl,
48+
isLoading,
49+
isUninitialized,
50+
};
51+
}

0 commit comments

Comments
 (0)