Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix browser history when synchronising state with urls #48731

Merged
merged 11 commits into from
Mar 6, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,47 @@ export default function useSyncCanvasModeWithURL() {
);
const { setCanvasMode } = unlock( useDispatch( editSiteStore ) );
const currentCanvasMode = useRef( canvasMode );
const { canvas: canvasInUrl = 'view' } = params;
const { canvas: canvasInUrl } = params;
const currentCanvasInUrl = useRef( canvasInUrl );
useEffect( () => {
currentCanvasMode.current = canvasMode;
if ( currentCanvasMode !== currentCanvasInUrl ) {
if ( canvasMode === 'init' ) {
return;
}

if (
canvasMode === 'edit' &&
currentCanvasInUrl.current !== canvasMode
) {
history.push( {
...params,
canvas: 'edit',
} );
}

if (
canvasMode === 'view' &&
currentCanvasInUrl.current !== undefined
) {
history.push( {
...params,
canvas: canvasMode,
canvas: undefined,
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
} );
}
}, [ canvasMode ] );

useEffect( () => {
currentCanvasInUrl.current = canvasInUrl;
if ( canvasInUrl !== currentCanvasMode.current ) {
setCanvasMode( canvasInUrl );
if (
canvasInUrl === undefined &&
currentCanvasMode.current !== 'view'
) {
setCanvasMode( 'view' );
} else if (
canvasInUrl === 'edit' &&
currentCanvasInUrl.current !== 'edit'
) {
setCanvasMode( 'edit' );
}
}, [ canvasInUrl ] );
}, [ canvasInUrl, params ] );
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export default function useSyncPathWithURL() {
updateUrlParams( {
postType: undefined,
postId: undefined,
path: navigatorLocation.path,
path:
navigatorLocation.path === '/'
? undefined
: navigatorLocation.path,
} );
}
}, [ navigatorLocation?.path, navigatorParams, history ] );
Expand Down