@@ -25,16 +25,18 @@ import type { ToolDefinitionWithSchema } from '@kbn/onechat-common';
2525import { isEsqlTool , isIndexSearchTool } from '@kbn/onechat-common/tools' ;
2626import { ToolType } from '@kbn/onechat-common/tools/definition' ;
2727import { KibanaPageTemplate } from '@kbn/shared-ux-page-kibana-template' ;
28+ import { useUnsavedChangesPrompt } from '@kbn/unsaved-changes-prompt' ;
29+ import { defer } from 'lodash' ;
2830import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
2931import { FormProvider } from 'react-hook-form' ;
30- import { useUnsavedChangesPrompt } from '@kbn/unsaved-changes-prompt' ;
3132import type {
3233 CreateToolPayload ,
3334 CreateToolResponse ,
3435 UpdateToolPayload ,
3536 UpdateToolResponse ,
3637} from '../../../../common/http_api/tools' ;
3738import { useToolForm } from '../../hooks/tools/use_tool_form' ;
39+ import { useKibana } from '../../hooks/use_kibana' ;
3840import { useNavigation } from '../../hooks/use_navigation' ;
3941import { useQueryState } from '../../hooks/use_query_state' ;
4042import { appPaths } from '../../utils/app_paths' ;
@@ -55,7 +57,6 @@ import { ToolTestFlyout } from './execute/test_tools';
5557import { ToolEditContextMenu } from './form/components/tool_edit_context_menu' ;
5658import { ToolForm , ToolFormMode } from './form/tool_form' ;
5759import type { ToolFormData } from './form/types/tool_form_types' ;
58- import { useKibana } from '../../hooks/use_kibana' ;
5960
6061const BUTTON_IDS = {
6162 SAVE : 'save' ,
@@ -108,12 +109,17 @@ export const Tool: React.FC<ToolProps> = ({ mode, tool, isLoading, isSubmitting,
108109 } , [ urlToolType ] ) ;
109110
110111 const form = useToolForm ( tool , initialToolType ) ;
111- const { reset, formState, watch, handleSubmit } = form ;
112+ const { reset, formState, watch, handleSubmit, getValues } = form ;
112113 const { errors, isDirty } = formState ;
113114 const [ showTestFlyout , setShowTestFlyout ] = useState ( false ) ;
114115 const [ submittingButtonId , setSubmittingButtonId ] = useState < string | undefined > ( ) ;
115116 const { services } = useKibana ( ) ;
116- const { application, overlays, http, appParams } = services ;
117+ const {
118+ application : { navigateToUrl } ,
119+ overlays : { openConfirm } ,
120+ http,
121+ appParams : { history } ,
122+ } = services ;
117123
118124 const currentToolId = watch ( 'toolId' ) ;
119125
@@ -126,8 +132,10 @@ export const Tool: React.FC<ToolProps> = ({ mode, tool, isLoading, isSubmitting,
126132 } , [ openTestFlyoutParam , currentToolId , showTestFlyout , setOpenTestFlyoutParam ] ) ;
127133
128134 const handleCancel = useCallback ( ( ) => {
129- navigateToOnechatUrl ( appPaths . tools . list ) ;
130- } , [ navigateToOnechatUrl ] ) ;
135+ // Bypass unsaved changes prompt
136+ reset ( getValues ( ) ) ;
137+ defer ( ( ) => navigateToOnechatUrl ( appPaths . tools . list ) ) ;
138+ } , [ navigateToOnechatUrl , reset , getValues ] ) ;
131139
132140 const handleSave = useCallback (
133141 async (
@@ -205,10 +213,10 @@ export const Tool: React.FC<ToolProps> = ({ mode, tool, isLoading, isSubmitting,
205213 const isViewMode = mode === ToolFormMode . View ;
206214 useUnsavedChangesPrompt ( {
207215 hasUnsavedChanges : ! isViewMode && isDirty ,
208- history : appParams . history ,
216+ history,
209217 http,
210- navigateToUrl : application . navigateToUrl ,
211- openConfirm : overlays . openConfirm ,
218+ navigateToUrl,
219+ openConfirm,
212220 } ) ;
213221 const hasErrors = Object . keys ( errors ) . length > 0 ;
214222
0 commit comments