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

Type the router package #65854

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/router/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancements

- Rewrite the package in typescript ([#65854](https://github.com/WordPress/gutenberg/pull/65854)).

## 1.9.0 (2024-10-03)

## 1.8.0 (2024-09-19)
Expand Down
1 change: 1 addition & 0 deletions packages/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"types": "build-types",
"dependencies": {
"@babel/runtime": "^7.16.0",
"@wordpress/element": "file:../element",
Expand Down
26 changes: 17 additions & 9 deletions packages/router/src/history.js → packages/router/src/history.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/**
* External dependencies
*/
import { createBrowserHistory } from 'history';
import { createBrowserHistory, type BrowserHistory } from 'history';

/**
* WordPress dependencies
*/
import { buildQueryString } from '@wordpress/url';

export interface EnhancedHistory extends BrowserHistory {
getLocationWithParams: () => Location;
}

const history = createBrowserHistory();

const originalHistoryPush = history.push;
Expand All @@ -16,7 +20,7 @@ const originalHistoryReplace = history.replace;
// Preserve the `wp_theme_preview` query parameter when navigating
// around the Site Editor.
// TODO: move this hack out of the router into Site Editor code.
function preserveThemePreview( params ) {
function preserveThemePreview( params: Record< string, any > ) {
if ( params.hasOwnProperty( 'wp_theme_preview' ) ) {
return params;
}
Expand All @@ -28,12 +32,15 @@ function preserveThemePreview( params ) {
return { ...params, wp_theme_preview: currentThemePreview };
}

function push( params, state ) {
function push( params: Record< string, any >, state: Record< string, any > ) {
const search = buildQueryString( preserveThemePreview( params ) );
return originalHistoryPush.call( history, { search }, state );
}

function replace( params, state ) {
function replace(
params: Record< string, any >,
state: Record< string, any >
) {
const search = buildQueryString( preserveThemePreview( params ) );
return originalHistoryReplace.call( history, { search }, state );
}
Expand All @@ -54,8 +61,9 @@ function getLocationWithParams() {
return locationWithParams;
}

history.push = push;
history.replace = replace;
history.getLocationWithParams = getLocationWithParams;

export default history;
export default {
...history,
push,
replace,
getLocationWithParams,
};
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import {
* Internal dependencies
*/
import history from './history';
import type { EnhancedHistory } from './history';

const RoutesContext = createContext();
const HistoryContext = createContext();
const RoutesContext = createContext< Location | null >( null );
const HistoryContext = createContext< EnhancedHistory >( history );

export function useLocation() {
return useContext( RoutesContext );
Expand All @@ -23,7 +24,7 @@ export function useHistory() {
return useContext( HistoryContext );
}

export function RouterProvider( { children } ) {
export function RouterProvider( { children }: { children: React.ReactNode } ) {
const location = useSyncExternalStore(
history.listen,
history.getLocationWithParams,
Expand Down
17 changes: 17 additions & 0 deletions packages/router/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://json.schemastore.org/tsconfig.json",
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types",
"types": [ "gutenberg-env" ],
"allowJs": false,
"checkJs": false
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
},
"references": [
{ "path": "../element" },
{ "path": "../private-apis" },
{ "path": "../url" }
],
"include": [ "src/**/*" ]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
{ "path": "packages/redux-routine" },
{ "path": "packages/report-flaky-tests" },
{ "path": "packages/rich-text" },
{ "path": "packages/router" },
{ "path": "packages/style-engine" },
{ "path": "packages/sync" },
{ "path": "packages/token-list" },
Expand Down
Loading