-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wpcom-block-editor-nux: avoid using useLocation which throws exceptio…
…n outside Site Editor in GB 19.9.0 (#40656)
- Loading branch information
Showing
2 changed files
with
22 additions
and
13 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/packages/jetpack-mu-wpcom/changelog/fix-uselocation-exception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
31 changes: 18 additions & 13 deletions
31
projects/packages/jetpack-mu-wpcom/src/common/hooks/use-canvas-mode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |