From 2de5294819b174f044fcf2e24dd49c0ff236b989 Mon Sep 17 00:00:00 2001 From: Marco Antonio Ghiani Date: Thu, 26 Jun 2025 18:28:26 +0200 Subject: [PATCH] [useUnsavedChangesPrompt] Don't prompt when history is replaced given its option. (#225471) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 📓 Summary Fixes an issue with prompting the user about leaving the page when the URL changes due to data source changes. https://github.com/user-attachments/assets/88e36769-96a4-4499-87f0-1e6db4502725 --- .../unsaved_changes_prompt.test.tsx | 20 +++++++++++++++++++ .../unsaved_changes_prompt.tsx | 12 ++++++++--- .../stream_detail_enrichment/page_content.tsx | 1 + 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/platform/packages/shared/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/unsaved_changes_prompt.test.tsx b/src/platform/packages/shared/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/unsaved_changes_prompt.test.tsx index 6315b9781d80..b5f30c3e78a3 100644 --- a/src/platform/packages/shared/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/unsaved_changes_prompt.test.tsx +++ b/src/platform/packages/shared/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/unsaved_changes_prompt.test.tsx @@ -119,4 +119,24 @@ describe('useUnsavedChangesPrompt', () => { expect(blockSpy).not.toBeCalled(); }); + + it('should not block if replaced when shouldPromptOnReplace is false', () => { + renderHook(() => + useUnsavedChangesPrompt({ + hasUnsavedChanges: false, + http: coreStart.http, + openConfirm: coreStart.overlays.openConfirm, + history, + navigateToUrl, + shouldPromptOnReplace: false, + }) + ); + + act(() => history.replace('/test')); + + expect(history.location.pathname).toBe('/test'); + expect(history.location.search).toBe(''); + expect(coreStart.overlays.openConfirm).not.toBeCalled(); + expect(addSpy).not.toBeCalledWith('beforeunload', expect.anything()); + }); }); diff --git a/src/platform/packages/shared/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/unsaved_changes_prompt.tsx b/src/platform/packages/shared/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/unsaved_changes_prompt.tsx index 12264f438283..ddf577d98fbc 100644 --- a/src/platform/packages/shared/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/unsaved_changes_prompt.tsx +++ b/src/platform/packages/shared/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/unsaved_changes_prompt.tsx @@ -42,10 +42,12 @@ interface SpaBlockingProps extends BaseProps { cancelButtonText?: string; confirmButtonText?: string; blockSpaNavigation?: true; + shouldPromptOnReplace?: boolean; } interface BrowserBlockingProps extends BaseProps { blockSpaNavigation: false; + shouldPromptOnReplace?: boolean; } type Props = SpaBlockingProps | BrowserBlockingProps; @@ -54,7 +56,7 @@ const isSpaBlocking = (props: Props): props is SpaBlockingProps => props.blockSpaNavigation !== false; export const useUnsavedChangesPrompt = (props: Props) => { - const { hasUnsavedChanges, blockSpaNavigation = true } = props; + const { hasUnsavedChanges, blockSpaNavigation = true, shouldPromptOnReplace = true } = props; useEffect(() => { if (hasUnsavedChanges) { @@ -87,7 +89,11 @@ export const useUnsavedChangesPrompt = (props: Props) => { cancelButtonText = DEFAULT_CANCEL_BUTTON, } = props; - const unblock = history.block((state) => { + const unblock = history.block((state, action) => { + if (!shouldPromptOnReplace && action === 'REPLACE') { + return; + } + async function confirmAsync() { const confirmResponse = await openConfirm(messageText, { title: titleText, @@ -113,5 +119,5 @@ export const useUnsavedChangesPrompt = (props: Props) => { }); return unblock; - }, [hasUnsavedChanges, blockSpaNavigation, props]); + }, [hasUnsavedChanges, blockSpaNavigation, shouldPromptOnReplace, props]); }; diff --git a/x-pack/platform/plugins/shared/streams_app/public/components/data_management/stream_detail_enrichment/page_content.tsx b/x-pack/platform/plugins/shared/streams_app/public/components/data_management/stream_detail_enrichment/page_content.tsx index 125ef71a8542..96efd39ebab5 100644 --- a/x-pack/platform/plugins/shared/streams_app/public/components/data_management/stream_detail_enrichment/page_content.tsx +++ b/x-pack/platform/plugins/shared/streams_app/public/components/data_management/stream_detail_enrichment/page_content.tsx @@ -89,6 +89,7 @@ export function StreamDetailEnrichmentContentImpl() { http: core.http, navigateToUrl: core.application.navigateToUrl, openConfirm: core.overlays.openConfirm, + shouldPromptOnReplace: false, }); if (!isReady) {