Fix back from CYS via LYS goes to Home, not LYS (#46665)

* Add goBack action to customize-store

* Add changelog

* Add __experimentLocationStack to history

* Update customize-store goBack action

* Rename __experimentalLocationStack
This commit is contained in:
Chi-Hsuan Huang 2024-04-22 21:26:32 +12:00 committed by GitHub
parent 118932d2ee
commit b9ea5bacd8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 65 additions and 4 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: add
Add __experimentalLocationStack prop to history

View File

@ -7,13 +7,16 @@ import { parse } from 'qs';
// See https://github.com/ReactTraining/react-router/blob/master/FAQ.md#how-do-i-access-the-history-object-outside-of-components // See https://github.com/ReactTraining/react-router/blob/master/FAQ.md#how-do-i-access-the-history-object-outside-of-components
// ^ This is a bit outdated but there's no newer documentation - the replacement for this is to use <unstable_HistoryRouter /> https://reactrouter.com/docs/en/v6/routers/history-router // ^ This is a bit outdated but there's no newer documentation - the replacement for this is to use <unstable_HistoryRouter /> https://reactrouter.com/docs/en/v6/routers/history-router
interface WooLocation extends Location {
pathname: string;
}
/** /**
* Extension of history.BrowserHistory but also adds { pathname: string } to the location object. * Extension of history.BrowserHistory but also adds { pathname: string } to the location object.
*/ */
export interface WooBrowserHistory extends BrowserHistory { export interface WooBrowserHistory extends BrowserHistory {
location: Location & { location: WooLocation;
pathname: string; readonly __experimentalLocationStack: WooLocation[];
};
} }
let _history: WooBrowserHistory; let _history: WooBrowserHistory;
@ -35,6 +38,31 @@ let _history: WooBrowserHistory;
function getHistory(): WooBrowserHistory { function getHistory(): WooBrowserHistory {
if ( ! _history ) { if ( ! _history ) {
const browserHistory = createBrowserHistory(); const browserHistory = createBrowserHistory();
let locationStack: WooLocation[] = [ browserHistory.location ];
const updateNextLocationStack = (
action: WooBrowserHistory[ 'action' ],
location: WooBrowserHistory[ 'location' ]
) => {
switch ( action ) {
case 'POP':
locationStack = locationStack.slice(
0,
locationStack.length - 1
);
break;
case 'PUSH':
locationStack = [ ...locationStack, location ];
break;
case 'REPLACE':
locationStack = [
...locationStack.slice( 0, locationStack.length - 1 ),
location,
];
break;
}
};
_history = { _history = {
get action() { get action() {
return browserHistory.action; return browserHistory.action;
@ -65,6 +93,9 @@ function getHistory(): WooBrowserHistory {
pathname, pathname,
}; };
}, },
get __experimentalLocationStack() {
return [ ...locationStack ];
},
createHref: browserHistory.createHref, createHref: browserHistory.createHref,
push: browserHistory.push, push: browserHistory.push,
replace: browserHistory.replace, replace: browserHistory.replace,
@ -81,6 +112,10 @@ function getHistory(): WooBrowserHistory {
} ); } );
}, },
}; };
browserHistory.listen( () =>
updateNextLocationStack( _history.action, _history.location )
);
} }
return _history; return _history;
} }

View File

@ -11,6 +11,7 @@ import {
getNewPath, getNewPath,
getQuery, getQuery,
updateQueryString, updateQueryString,
getHistory,
} from '@woocommerce/navigation'; } from '@woocommerce/navigation';
import { OPTIONS_STORE_NAME } from '@woocommerce/data'; import { OPTIONS_STORE_NAME } from '@woocommerce/data';
import { dispatch, resolveSelect } from '@wordpress/data'; import { dispatch, resolveSelect } from '@wordpress/data';
@ -84,6 +85,22 @@ const redirectToWooHome = () => {
navigateOrParent( window, url ); navigateOrParent( window, url );
}; };
const goBack = () => {
const history = getHistory();
if (
history.__experimentalLocationStack.length >= 2 &&
! history.__experimentalLocationStack[
history.__experimentalLocationStack.length - 2
].search.includes( 'customize-store' )
) {
// If the previous location is not a customize-store step, go back in history.
history.back();
return;
}
redirectToWooHome();
};
const redirectToThemes = ( _context: customizeStoreStateMachineContext ) => { const redirectToThemes = ( _context: customizeStoreStateMachineContext ) => {
if ( isWooExpress() ) { if ( isWooExpress() ) {
window.location.href = window.location.href =
@ -130,6 +147,7 @@ export const machineActions = {
updateQueryStep, updateQueryStep,
redirectToWooHome, redirectToWooHome,
redirectToThemes, redirectToThemes,
goBack,
}; };
export const customizeStoreStateMachineActions = { export const customizeStoreStateMachineActions = {
@ -330,7 +348,7 @@ export const customizeStoreStateMachineDefinition = createMachine( {
}, },
on: { on: {
CLICKED_ON_BREADCRUMB: { CLICKED_ON_BREADCRUMB: {
actions: 'redirectToWooHome', actions: 'goBack',
}, },
DESIGN_WITH_AI: { DESIGN_WITH_AI: {
actions: [ 'recordTracksDesignWithAIClicked' ], actions: [ 'recordTracksDesignWithAIClicked' ],

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix back from CYS via LYS goes to Home, not LYS