@@ -5,12 +5,15 @@ import type {DropdownMenuItem} from '@gravity-ui/uikit';
55import { Text } from '@gravity-ui/uikit' ;
66
77import { planToSvgApi } from '../../../../../../store/reducers/planToSvg' ;
8- import type { QueryPlan , ScriptPlan , TKqpStatsQuery } from '../../../../../../types/api/query' ;
98import { cn } from '../../../../../../utils/cn' ;
10- import createToast from '../../../../../../utils/createToast' ;
11- import { prepareCommonErrorMessage } from '../../../../../../utils/errors' ;
12- import { parseQueryError } from '../../../../../../utils/query' ;
139import i18n from '../../i18n' ;
10+
11+ import {
12+ createHandleDiagnosticsDownload ,
13+ createHandleDownload ,
14+ createHandleOpenInNewTab ,
15+ } from './utils' ;
16+ import type { QueryResultsInfo } from './utils' ;
1417const b = cn ( 'query-info-dropdown' ) ;
1518
1619export interface MenuItemContentProps {
@@ -29,19 +32,6 @@ export function MenuItemContent({title, description}: MenuItemContentProps) {
2932 ) ;
3033}
3134
32- export interface QueryResultsInfo {
33- ast ?: string ;
34- stats ?: TKqpStatsQuery ;
35- queryText ?: string ;
36- plan ?: QueryPlan | ScriptPlan ;
37- }
38-
39- export interface DiagnosticsData extends QueryResultsInfo {
40- database : string ;
41- timestamp : string ;
42- error ?: ReturnType < typeof parseQueryError > ;
43- }
44-
4535export interface UseQueryInfoMenuItemsProps {
4636 queryResultsInfo : QueryResultsInfo ;
4737 database : string ;
@@ -71,61 +61,24 @@ export function useQueryInfoMenuItems({
7161
7262 const plan = queryResultsInfo . plan ;
7363 if ( plan && hasPlanToSvg ) {
74- const handleGetSvg = ( ) => {
75- if ( blobUrl ) {
76- return Promise . resolve ( blobUrl ) ;
77- }
78-
79- return getPlanToSvg ( { plan, database} )
80- . unwrap ( )
81- . then ( ( result ) => {
82- const blob = new Blob ( [ result ] , { type : 'image/svg+xml' } ) ;
83- const url = URL . createObjectURL ( blob ) ;
84- setBlobUrl ( url ) ;
85- return url ;
86- } )
87- . catch ( ( err ) => {
88- const errorMessage = prepareCommonErrorMessage ( err ) ;
89- createToast ( {
90- title : i18n ( 'text_error-plan-svg' , { error : errorMessage } ) ,
91- name : 'plan-svg-error' ,
92- type : 'error' ,
93- } ) ;
94- return null ;
95- } ) ;
96- } ;
97-
98- const handleOpenInNewTab = ( ) => {
99- handleGetSvg ( ) . then ( ( url ) => {
100- if ( url ) {
101- window . open ( url , '_blank' ) ;
102- }
103- } ) ;
104- } ;
105-
106- const handleDownload = ( ) => {
107- handleGetSvg ( ) . then ( ( url ) => {
108- const link = document . createElement ( 'a' ) ;
109- if ( url ) {
110- link . href = url ;
111- link . download = 'query-plan.svg' ;
112- document . body . appendChild ( link ) ;
113- link . click ( ) ;
114- document . body . removeChild ( link ) ;
115- }
116- } ) ;
64+ const handlersParams = {
65+ plan,
66+ database,
67+ blobUrl,
68+ getPlanToSvg,
69+ setBlobUrl,
11770 } ;
11871
11972 menuItems . push ( [
12073 {
12174 text : (
12275 < MenuItemContent
123- title = { i18n ( 'text_open-new-tab ' ) }
124- description = { i18n ( 'text_open-new-tab_description ' ) }
76+ title = { i18n ( 'text_open-execution-plan ' ) }
77+ description = { i18n ( 'text_open-execution-plan_description ' ) }
12578 />
12679 ) ,
12780 icon : < ArrowUpRightFromSquare className = { b ( 'icon' ) } /> ,
128- action : handleOpenInNewTab ,
81+ action : createHandleOpenInNewTab ( handlersParams ) ,
12982 className : b ( 'menu-item' ) ,
13083 } ,
13184 {
@@ -136,35 +89,13 @@ export function useQueryInfoMenuItems({
13689 />
13790 ) ,
13891 icon : < ArrowDownToLine className = { b ( 'icon' ) } /> ,
139- action : handleDownload ,
92+ action : createHandleDownload ( handlersParams ) ,
14093 className : b ( 'menu-item' ) ,
14194 } ,
14295 ] ) ;
14396 }
14497
14598 if ( queryResultsInfo ) {
146- const handleDiagnosticsDownload = ( ) => {
147- const parsedError = error ? parseQueryError ( error ) : undefined ;
148- const diagnosticsData : DiagnosticsData = {
149- ...queryResultsInfo ,
150- database,
151- timestamp : new Date ( ) . toISOString ( ) ,
152- ...( parsedError && { error : parsedError } ) ,
153- } ;
154-
155- const blob = new Blob ( [ JSON . stringify ( diagnosticsData , null , 2 ) ] , {
156- type : 'application/json' ,
157- } ) ;
158- const url = URL . createObjectURL ( blob ) ;
159- const link = document . createElement ( 'a' ) ;
160- link . href = url ;
161- link . download = `query-diagnostics-${ new Date ( ) . getTime ( ) } .json` ;
162- document . body . appendChild ( link ) ;
163- link . click ( ) ;
164- document . body . removeChild ( link ) ;
165- URL . revokeObjectURL ( url ) ;
166- } ;
167-
16899 menuItems . push ( [
169100 {
170101 text : (
@@ -174,14 +105,14 @@ export function useQueryInfoMenuItems({
174105 />
175106 ) ,
176107 icon : < ArrowDownToLine className = { b ( 'icon' ) } /> ,
177- action : handleDiagnosticsDownload ,
108+ action : createHandleDiagnosticsDownload ( { queryResultsInfo , database , error } ) ,
178109 className : b ( 'menu-item' ) ,
179110 } ,
180111 ] ) ;
181112 }
182113
183114 return menuItems ;
184- } , [ queryResultsInfo , hasPlanToSvg , blobUrl , getPlanToSvg , database ] ) ;
115+ } , [ queryResultsInfo , hasPlanToSvg , database , blobUrl , getPlanToSvg , error ] ) ;
185116
186117 return {
187118 isLoading,
0 commit comments