Skip to content

Commit

Permalink
Help Center: use URL params for determining editor canvas state (#95964)
Browse files Browse the repository at this point in the history
* Help Center: Use router params for canvas mode

* Add wordpress router dep

* Dedupe wordpress router

* Only return canvas mode value in the site editor

* Match wordpress router version of wpcom-block-editor

* Reset yarn.lock from trunk
  • Loading branch information
holdercp authored Nov 4, 2024
1 parent 02546ff commit b4cbdd8
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 29 deletions.
2 changes: 1 addition & 1 deletion apps/help-center/help-center-gutenberg-disconnected.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useMediaQuery } from '@wordpress/compose';
import { useEffect, useReducer } from '@wordpress/element';
import { registerPlugin } from '@wordpress/plugins';
import ReactDOM from 'react-dom';
import { useCanvasMode } from './hooks';
import { useCanvasMode } from './hooks/use-canvas-mode';
import './help-center.scss';

function HelpCenterContent() {
Expand Down
2 changes: 1 addition & 1 deletion apps/help-center/help-center-gutenberg.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
import { useCallback, useEffect, useState, useReducer } from '@wordpress/element';
import { registerPlugin } from '@wordpress/plugins';
import ReactDOM from 'react-dom';
import { useCanvasMode } from './hooks';
import { useCanvasMode } from './hooks/use-canvas-mode';
import './help-center.scss';

const queryClient = new QueryClient();
Expand Down
27 changes: 0 additions & 27 deletions apps/help-center/hooks.js

This file was deleted.

20 changes: 20 additions & 0 deletions apps/help-center/hooks/use-canvas-mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useSelect } from '@wordpress/data';
import { useLocation } from './use-location';

const useCanvasMode = () => {
const location = useLocation();

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

return new URLSearchParams( location?.search ).get( 'canvas' ) || 'view';
},
[ location?.search ]
);
};

export { useCanvasMode };
13 changes: 13 additions & 0 deletions apps/help-center/hooks/use-location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { getUnlock } from '../utils';

const unlock = getUnlock();

let useLocation;

// The routerPrivateApis may be unavailable.
if ( unlock && routerPrivateApis && unlock( routerPrivateApis ) ) {
useLocation = unlock( routerPrivateApis ).useLocation;
}

export { useLocation };
1 change: 1 addition & 0 deletions apps/help-center/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@wordpress/data": "^10.8.0",
"@wordpress/plugins": "^7.8.0",
"@wordpress/private-apis": "^1.8.0",
"@wordpress/router": "^1.8.0",
"postcss-prefix-selector": "^1.16.1"
},
"devDependencies": {
Expand Down
23 changes: 23 additions & 0 deletions apps/help-center/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';

/**
* Get unlock API from Gutenberg.
* Sometimes Gutenberg doesn't allow you to re-register the module and throws an error.
*/
export const getUnlock = () => {
/**
* Sometimes Gutenberg doesn't allow you to re-register the module and throws an error.
*/
let unlock;
try {
unlock = __dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',
'@wordpress/edit-site'
).unlock;
return unlock;
} catch ( error ) {
// eslint-disable-next-line no-console
console.error( 'Error: Unable to get the unlock api. Reason: %s', error );
return undefined;
}
};
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,7 @@ __metadata:
"@wordpress/plugins": "npm:^7.8.0"
"@wordpress/private-apis": "npm:^1.8.0"
"@wordpress/readable-js-assets-webpack-plugin": "npm:3.0.0"
"@wordpress/router": "npm:^1.8.0"
copy-webpack-plugin: "npm:^10.2.4"
postcss-prefix-selector: "npm:^1.16.1"
typescript: "npm:^5.3.3"
Expand Down

0 comments on commit b4cbdd8

Please sign in to comment.