Skip to content

Commit

Permalink
wpcom-block-editor-nux: avoid using useLocation which throws exceptio…
Browse files Browse the repository at this point in the history
…n outside Site Editor in GB 19.9.0 (#40656)
  • Loading branch information
fushar authored Dec 18, 2024
1 parent 693d3d5 commit f02d391
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

wpcom-block-editor-nux: avoid using useLocation which now throws exception outside Site Editor in Gutenberg 19.9.0
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { useSelect } from '@wordpress/data';
import useLocation from './use-location';
import { subscribe, useSelect } from '@wordpress/data';
import { useEffect, useState } from 'react';

const useCanvasMode = () => {
const location = useLocation();
const [ canvasMode, setCanvasMode ] = useState< string | null >( null );
const isSiteEditor = useSelect( select => !! select( 'core/edit-site' ), [] );

return useSelect(
select => {
// The canvas mode is limited to the site editor.
if ( ! select( 'core/edit-site' ) ) {
return null;
}
useEffect( () => {
// The canvas mode is limited to the site editor.
if ( ! isSiteEditor ) {
return;
}

return new URLSearchParams( location?.search ).get( 'canvas' ) || 'view';
},
[ location?.search ]
);
const unsubscribe = subscribe( () => {
const mode = new URLSearchParams( window.location?.search ).get( 'canvas' ) || 'view';
setCanvasMode( mode );
} );

return () => unsubscribe();
}, [ isSiteEditor ] );

return canvasMode;
};

export default useCanvasMode;

0 comments on commit f02d391

Please sign in to comment.