From e52d11a87ef3b3bd76540e0c94145369a4711c9c Mon Sep 17 00:00:00 2001 From: RJ <27843274+rjchow@users.noreply.github.com> Date: Tue, 12 Sep 2023 16:32:50 +1000 Subject: [PATCH] add: url navigation to cys (#40068) * add: url navigation to cys * bugfix for url not updating * url handling for design-with-ai * fixed url syncing so that it's working with @woocommerce/navigation * changed useLocation to useQuery in assembler-hub save-hub --- .../assembler-hub/sidebar/index.tsx | 57 +++++---- .../assembler-hub/sidebar/save-hub.tsx | 15 +-- .../sidebar-navigation-screen-main.tsx | 14 +-- .../customize-store/design-with-ai/actions.ts | 25 ++++ .../design-with-ai/services.ts | 13 ++ .../design-with-ai/state-machine.tsx | 89 ++++++++++++- .../customize-store/design-with-ai/types.ts | 5 +- .../client/customize-store/index.tsx | 119 +++++++++++++++--- .../changelog/add-cys-url-navigation | 4 + 9 files changed, 275 insertions(+), 66 deletions(-) create mode 100644 plugins/woocommerce/changelog/add-cys-url-navigation diff --git a/plugins/woocommerce-admin/client/customize-store/assembler-hub/sidebar/index.tsx b/plugins/woocommerce-admin/client/customize-store/assembler-hub/sidebar/index.tsx index a99de4b0433..2478b41814b 100644 --- a/plugins/woocommerce-admin/client/customize-store/assembler-hub/sidebar/index.tsx +++ b/plugins/woocommerce-admin/client/customize-store/assembler-hub/sidebar/index.tsx @@ -13,10 +13,6 @@ import { // @ts-ignore No types for this exist yet. __experimentalUseNavigator as useNavigator, } from '@wordpress/components'; -// @ts-ignore No types for this exist yet. -import { privateApis as routerPrivateApis } from '@wordpress/router'; -// @ts-ignore No types for this exist yet. -import { unlock } from '@wordpress/edit-site/build-module/lock-unlock'; /** * Internal dependencies @@ -31,8 +27,12 @@ import { SidebarNavigationScreenPages } from './sidebar-navigation-screen-pages' import { SidebarNavigationScreenLogo } from './sidebar-navigation-screen-logo'; import { SaveHub } from './save-hub'; - -const { useLocation, useHistory } = unlock( routerPrivateApis ); +import { + addHistoryListener, + getQuery, + updateQueryString, + useQuery, +} from '@woocommerce/navigation'; function isSubset( subset: { @@ -48,18 +48,27 @@ function isSubset( } function useSyncPathWithURL() { - const history = useHistory(); - const { params: urlParams } = useLocation(); - const { location: navigatorLocation, params: navigatorParams } = - useNavigator(); + const urlParams = useQuery(); + const { + location: navigatorLocation, + params: navigatorParams, + goTo, + } = useNavigator(); const isMounting = useRef( true ); useEffect( () => { - // The navigatorParams are only initially filled properly when the - // navigator screens mount. so we ignore the first synchronisation. + // The navigatorParams are only initially filled properly after the + // navigator screens mounts. so we don't do the query string update initially. + // however we also do want to add an event listener for popstate so that we can + // update the navigator when the user navigates using the browser back button if ( isMounting.current ) { isMounting.current = false; + addHistoryListener( ( event: PopStateEvent ) => { + if ( event.type === 'popstate' ) { + goTo( ( getQuery() as Record< string, string > ).path ); + } + } ); return; } @@ -73,7 +82,7 @@ function useSyncPathWithURL() { ...urlParams, ...newUrlParams, }; - history.push( updatedParams ); + updateQueryString( {}, updatedParams.path ); } updateUrlParams( { @@ -97,28 +106,28 @@ function SidebarScreens() { useSyncPathWithURL(); return ( <> - + - + - + - + - + - + - + - + @@ -126,8 +135,10 @@ function SidebarScreens() { } function Sidebar() { - const { params: urlParams } = useLocation(); - const initialPath = useRef( urlParams.path ?? '/customize-store' ); + const urlParams = getQuery() as Record< string, string >; + const initialPath = useRef( + urlParams.path ?? '/customize-store/assembler-hub' + ); return ( <> { const saveNoticeId = 'site-edit-save-notice'; - const { params } = useLocation(); + const urlParams = useQuery(); const { sendEvent } = useContext( CustomizeStoreContext ); // @ts-ignore No types for this exist yet. @@ -141,7 +134,7 @@ export const SaveHub = () => { } ); // Only run when path changes. // eslint-disable-next-line react-hooks/exhaustive-deps - }, [ params.path ] ); + }, [ urlParams.path ] ); const save = async () => { removeNotice( saveNoticeId ); @@ -182,7 +175,7 @@ export const SaveHub = () => { }; const renderButton = () => { - if ( params.path === '/customize-store' ) { + if ( urlParams.path === '/customize-store/assembler-hub' ) { return (