Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ class DatasourceControl extends PureComponent {
datasourceKey: `${datasource.id}__${datasource.type}`,
sql: datasource.sql,
};

const defaultDatasourceMenuItems = [];
if (this.props.isEditable && !isMissingDatasource) {
defaultDatasourceMenuItems.push({
Expand Down
11 changes: 9 additions & 2 deletions superset-frontend/src/hooks/useUnsavedChangesPrompt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getClientErrorObject, t } from '@superset-ui/core';
import { useEffect, useRef, useCallback, useState } from 'react';
import { useHistory } from 'react-router-dom';
import { useBeforeUnload } from 'src/hooks/useBeforeUnload';
import type { Location } from 'history';

type UseUnsavedChangesPromptProps = {
hasUnsavedChanges: boolean;
Expand Down Expand Up @@ -69,15 +70,21 @@ export const useUnsavedChangesPrompt = ({
}, [onSave]);

const blockCallback = useCallback(
({ pathname }: { pathname: string }) => {
({
pathname,
state,
}: {
pathname: Location['pathname'];
state: Location['state'];
}) => {
if (manualSaveRef.current) {
manualSaveRef.current = false;
return undefined;
}

confirmNavigationRef.current = () => {
unblockRef.current?.();
history.push(pathname);
history.push(pathname, state);
};

setShowModal(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { renderHook } from '@testing-library/react-hooks';
import { renderHook, act } from '@testing-library/react-hooks';
import { Router } from 'react-router-dom';
import { createMemoryHistory } from 'history';
import { useUnsavedChangesPrompt } from '.';
Expand Down Expand Up @@ -124,3 +124,42 @@ test('should close modal when handleConfirmNavigation is called', () => {

expect(result.current.showModal).toBe(false);
});

test('should preserve pathname and state when confirming navigation', () => {
const onSave = jest.fn();
const history = createMemoryHistory();
const wrapper = ({ children }: any) => (
<Router history={history}>{children}</Router>
);

const locationState = { fromDashboard: true, dashboardId: 123 };
const pathname = '/another-page';

const { result } = renderHook(
() => useUnsavedChangesPrompt({ hasUnsavedChanges: true, onSave }),
{ wrapper },
);

const pushSpy = jest.spyOn(history, 'push');

// Simulate a blocked navigation (the hook sets up history.block internally)
act(() => {
history.push(pathname, locationState);
});

// Modal should now be visible
expect(result.current.showModal).toBe(true);

// Confirm navigation
act(() => {
result.current.handleConfirmNavigation();
});

// Modal should close
expect(result.current.showModal).toBe(false);

// Verify correct call with pathname and state preserved
expect(pushSpy).toHaveBeenCalledWith(pathname, locationState);

pushSpy.mockRestore();
});
Loading