Skip to content

Commit b67bda0

Browse files
Anton StandrikAnton Standrik
authored andcommitted
fix: some fixes
1 parent 1c0b4f7 commit b67bda0

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

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

Whitespace-only changes.
Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,72 @@
11
import React from 'react';
22

33
import {ArrowUpRightFromSquare} from '@gravity-ui/icons';
4-
import {Button} from '@gravity-ui/uikit';
4+
import {Button, Tooltip} from '@gravity-ui/uikit';
55

66
import {planToSvgQueryApi} from '../../../../store/reducers/planToSvgQuery';
77
import type {QueryPlan, ScriptPlan} from '../../../../types/api/query';
88

99
import i18n from './i18n';
1010

11+
function getButtonView(error: string | null, isLoading: boolean) {
12+
if (error) {
13+
return 'flat-danger';
14+
}
15+
return isLoading ? 'flat-secondary' : 'flat-info';
16+
}
17+
1118
interface PlanToSvgButtonProps {
1219
plan?: QueryPlan | ScriptPlan;
1320
database: string;
1421
}
1522

1623
export function PlanToSvgButton({plan, database}: PlanToSvgButtonProps) {
24+
const [svgData, setSvgData] = React.useState<string | null>(null);
25+
const [error, setError] = React.useState<string | null>(null);
1726
const [checkPlanToSvg, {isLoading, isUninitialized}] =
1827
planToSvgQueryApi.usePlanToSvgQueryMutation();
1928

2029
React.useEffect(() => {
21-
let checkPlanToSvgMutation: {abort: () => void} | null;
22-
if (plan) {
23-
checkPlanToSvgMutation = checkPlanToSvg({plan, database});
30+
if (!plan) {
31+
return;
2432
}
2533

26-
return () => checkPlanToSvgMutation?.abort();
34+
checkPlanToSvg({plan, database})
35+
.unwrap()
36+
.then((result) => {
37+
setSvgData(result);
38+
setError(null);
39+
})
40+
.catch((err) => {
41+
setError(JSON.stringify(err));
42+
});
2743
}, [checkPlanToSvg, database, plan]);
2844

45+
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');
50+
}
51+
}, [svgData]);
52+
2953
if (isUninitialized) {
3054
return null;
3155
}
3256

3357
return (
34-
<Button
35-
view={isLoading ? 'flat-secondary' : 'flat-info'}
36-
loading={isLoading}
37-
target="_blank"
38-
>
39-
{i18n('text_plan-svg')}
40-
<Button.Icon>
41-
<ArrowUpRightFromSquare />
42-
</Button.Icon>
43-
</Button>
58+
<Tooltip content={i18n('text_error-plan-svg', {error}) || i18n('text_plan-svg')}>
59+
<Button
60+
view={getButtonView(error, isLoading)}
61+
loading={isLoading}
62+
onClick={handleClick}
63+
disabled={isLoading || !svgData}
64+
>
65+
{i18n('text_plan-svg')}
66+
<Button.Icon>
67+
<ArrowUpRightFromSquare />
68+
</Button.Icon>
69+
</Button>
70+
</Tooltip>
4471
);
4572
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
"trace": "Trace",
99
"title.truncated": "Truncated",
1010
"title.result": "Result",
11-
"text_plan-svg": "Plan SVG"
11+
"text_plan-svg": "Plan SVG",
12+
"text_open-plan-svg": "Open query plan in new window",
13+
"text_error-plan-svg": "Error: {{error}}"
1214
}

src/services/api.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,18 @@ export class YdbEmbeddedAPI extends AxiosWrapper {
580580
);
581581
}
582582
planToSvg({database, plan}: PlanToSvgQueryParams, {signal}: {signal?: AbortSignal} = {}) {
583-
return this.post<{test: string}>(
583+
return this.post<string>(
584584
this.getPath('/viewer/plan2svg'),
585585
plan,
586586
{database},
587587
{
588-
requestConfig: {signal},
588+
requestConfig: {
589+
signal,
590+
responseType: 'text',
591+
headers: {
592+
Accept: 'image/svg+xml',
593+
},
594+
},
589595
},
590596
);
591597
}

src/store/reducers/planToSvgQuery.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type {QueryPlan, ScriptPlan} from '../../types/api/query';
2-
import type {IQueryResult} from '../../types/store/query';
32

43
import {api} from './api';
54

@@ -10,7 +9,8 @@ export interface PlanToSvgQueryParams {
109

1110
export const planToSvgQueryApi = api.injectEndpoints({
1211
endpoints: (build) => ({
13-
planToSvgQuery: build.mutation<IQueryResult, PlanToSvgQueryParams>({
12+
planToSvgQuery: build.mutation<string, PlanToSvgQueryParams>({
13+
// Changed return type to string
1414
queryFn: async ({plan, database}, {signal}) => {
1515
try {
1616
const response = await window.api.planToSvg(
@@ -21,8 +21,6 @@ export const planToSvgQueryApi = api.injectEndpoints({
2121
{signal},
2222
);
2323

24-
console.log(response);
25-
2624
return {data: response};
2725
} catch (error) {
2826
return {error};

0 commit comments

Comments
 (0)