Skip to content

Commit

Permalink
switch middleware to beforeNavigate
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 25, 2024
1 parent e604227 commit d03a835
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 52 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 16 additions & 21 deletions packages/edit-site/src/components/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { PluginArea } from '@wordpress/plugins';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useMemo } from '@wordpress/element';
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -54,27 +54,22 @@ export default function App() {
)
);
}
const middlewares = useMemo(
() => [
( { path, query } ) => {
if ( ! isPreviewingTheme() ) {
return { path, query };
}
const beforeNavigate = useCallback( ( { path, query } ) => {
if ( ! isPreviewingTheme() ) {
return { path, query };
}

return {
path,
query: {
...query,
wp_theme_preview:
'wp_theme_preview' in query
? query.wp_theme_preview
: currentlyPreviewingTheme(),
},
};
return {
path,
query: {
...query,
wp_theme_preview:
'wp_theme_preview' in query
? query.wp_theme_preview
: currentlyPreviewingTheme(),
},
],
[]
);
};
}, [] );

return (
<SlotFillProvider>
Expand All @@ -83,7 +78,7 @@ export default function App() {
<RouterProvider
routes={ routes }
pathArg="p"
middlewares={ middlewares }
beforeNavigate={ beforeNavigate }
>
<AppLayout />
<PluginArea onError={ onPluginAreaError } />
Expand Down
1 change: 0 additions & 1 deletion packages/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"types": "build-types",
"dependencies": {
"@babel/runtime": "7.25.7",
"@wordpress/compose": "*",
"@wordpress/element": "*",
"@wordpress/private-apis": "*",
"@wordpress/url": "*",
Expand Down
19 changes: 6 additions & 13 deletions packages/router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,26 @@
*/
import { useContext, useMemo } from '@wordpress/element';
import { getQueryArgs, getPath, buildQueryString } from '@wordpress/url';
import { compose } from '@wordpress/compose';

/**
* Internal dependencies
*/
import {
ConfigContext,
type Middleware,
type NavigationOptions,
useHistory,
} from './router';
import { ConfigContext, type NavigationOptions, useHistory } from './router';

export function useLink( to: string, options: NavigationOptions = {} ) {
const history = useHistory();
const { pathArg, middlewares } = useContext( ConfigContext );
const { pathArg, beforeNavigate } = useContext( ConfigContext );
function onClick( event: React.SyntheticEvent< HTMLAnchorElement > ) {
event?.preventDefault();
history.navigate( to, options );
}
const query = getQueryArgs( to );
const path = getPath( 'http://domain.com/' + to ) ?? '';
const link = useMemo( () => {
const runMiddlewares = (
middlewares ? compose( ...middlewares ) : ( i: unknown ) => i
) as Middleware;
return runMiddlewares( { path, query } );
}, [ path, query, middlewares ] );
return beforeNavigate
? beforeNavigate( { path, query } )
: { path, query };
}, [ path, query, beforeNavigate ] );

const [ before ] = window.location.href.split( '?' );

Expand Down
26 changes: 11 additions & 15 deletions packages/router/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
getPath,
buildQueryString,
} from '@wordpress/url';
import { compose } from '@wordpress/compose';

/**
* Internal dependencies
Expand Down Expand Up @@ -47,7 +46,7 @@ interface Match {
params?: Record< string, any >;
}

export type Middleware = ( arg: {
export type BeforeNavigate = ( arg: {
path: string;
query: Record< string, any >;
} ) => {
Expand All @@ -57,7 +56,7 @@ export type Middleware = ( arg: {

interface Config {
pathArg: string;
middlewares?: Middleware[];
beforeNavigate?: BeforeNavigate;
}

export interface NavigationOptions {
Expand Down Expand Up @@ -87,19 +86,16 @@ export function useLocation() {
}

export function useHistory() {
const { pathArg, middlewares } = useContext( ConfigContext );
const { pathArg, beforeNavigate } = useContext( ConfigContext );
return useMemo(
() => ( {
navigate( rawPath: string, options: NavigationOptions = {} ) {
const runMiddlewares = (
middlewares
? compose( ...middlewares )
: ( i: unknown ) => i
) as Middleware;
const query = getQueryArgs( rawPath );
const path = getPath( 'http://domain.com/' + rawPath ) ?? '';
const performPush = () => {
const result = runMiddlewares( { path, query } );
const result = beforeNavigate
? beforeNavigate( { path, query } )
: { path, query };
return history.push(
{
search: buildQueryString( {
Expand Down Expand Up @@ -137,7 +133,7 @@ export function useHistory() {
} );
},
} ),
[ pathArg, middlewares ]
[ pathArg, beforeNavigate ]
);
}

Expand Down Expand Up @@ -196,12 +192,12 @@ export default function useMatch(
export function RouterProvider( {
routes,
pathArg,
middlewares,
beforeNavigate,
children,
}: {
routes: Route[];
pathArg: string;
middlewares?: Middleware[];
beforeNavigate?: BeforeNavigate;
children: React.ReactNode;
} ) {
const location = useSyncExternalStore(
Expand All @@ -211,8 +207,8 @@ export function RouterProvider( {
);
const match = useMatch( location, routes, pathArg );
const config = useMemo(
() => ( { middlewares, pathArg } ),
[ middlewares, pathArg ]
() => ( { beforeNavigate, pathArg } ),
[ beforeNavigate, pathArg ]
);

return (
Expand Down
1 change: 0 additions & 1 deletion packages/router/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"types": [ "gutenberg-env" ]
},
"references": [
{ "path": "../compose" },
{ "path": "../element" },
{ "path": "../private-apis" },
{ "path": "../url" }
Expand Down

0 comments on commit d03a835

Please sign in to comment.