diff --git a/.changeset/stale-berries-stare.md b/.changeset/stale-berries-stare.md new file mode 100644 index 00000000000..4758ee7af91 --- /dev/null +++ b/.changeset/stale-berries-stare.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": minor +--- + +Handle `replace` in the app-bridge routing updates diff --git a/src/apps/components/AppFrame/appActionsHandler.test.ts b/src/apps/components/AppFrame/appActionsHandler.test.ts index 6d769665cc0..a4448a171ef 100644 --- a/src/apps/components/AppFrame/appActionsHandler.test.ts +++ b/src/apps/components/AppFrame/appActionsHandler.test.ts @@ -139,6 +139,36 @@ describe("AppActionsHandler", function () { "/dashboard/apps/XYZ/app/foo/bar", ); }); + it("replaces dashboard url properly", () => { + const mockHistoryReplaceState = jest.fn(); + jest + .spyOn(window.history, "replaceState") + .mockImplementation(mockHistoryReplaceState); + + const { + result: { + current: { handle }, + }, + } = renderHook(() => + AppActionsHandler.useHandleUpdateRoutingAction("XYZ"), + ); + + handle({ + type: "updateRouting", + payload: { + actionId: "123", + newRoute: "/foo/bar", + replace: true, + }, + }); + + expect(mockHistoryReplaceState).toHaveBeenCalledTimes(1); + expect(mockHistoryReplaceState).toHaveBeenCalledWith( + null, + "", + "/dashboard/apps/XYZ/app/foo/bar", + ); + }); }); describe("useHandleRedirectAction", () => { describe("Open in the new browser context", () => { diff --git a/src/apps/components/AppFrame/appActionsHandler.ts b/src/apps/components/AppFrame/appActionsHandler.ts index aeccff443c0..d0f165a4722 100644 --- a/src/apps/components/AppFrame/appActionsHandler.ts +++ b/src/apps/components/AppFrame/appActionsHandler.ts @@ -157,12 +157,13 @@ const useHandleRedirectAction = (appId: string) => { const useHandleUpdateRoutingAction = (appId: string) => ({ handle: (action: UpdateRouting) => { - const { newRoute, actionId } = action.payload; + const { newRoute, replace, actionId } = action.payload; debug( - `Handling UpdateRouting action with ID: %s and new route: %s`, + `Handling UpdateRouting action with ID: %s and new route: %s (replace=%s)`, actionId, newRoute, + replace, ); const exactLocation = urlJoin( @@ -173,7 +174,11 @@ const useHandleUpdateRoutingAction = (appId: string) => ({ debug(`Update to new nested route: %s`, exactLocation); - window.history.pushState(null, "", exactLocation); + if (replace) { + window.history.replaceState(null, "", exactLocation); + } else { + window.history.pushState(null, "", exactLocation); + } return createResponseStatus(actionId, true); },