|
|
import pagejs from '@automattic/calypso-router'; |
|
|
import { createMemoryHistory } from '@tanstack/react-router'; |
|
|
import { getQueryArgs } from '@wordpress/url'; |
|
|
import type { AnyRoute, AnyRouter } from '@tanstack/react-router'; |
|
|
|
|
|
export function getRouterOptions() { |
|
|
return { |
|
|
defaultPreload: 'intent' as const, |
|
|
defaultPreloadStaleTime: 0, |
|
|
defaultNotFoundComponent: () => null, |
|
|
defaultViewTransition: true, |
|
|
|
|
|
|
|
|
|
|
|
history: createMemoryHistory( { initialEntries: [ window.location.pathname ] } ), |
|
|
}; |
|
|
} |
|
|
|
|
|
export function createBrowserHistoryAndMemoryRouterSync( { |
|
|
compatibilityRoutes, |
|
|
}: { |
|
|
compatibilityRoutes?: AnyRoute[]; |
|
|
} = {} ) { |
|
|
let lastPath = ''; |
|
|
|
|
|
const syncBrowserHistoryToRouter = ( router: AnyRouter ) => { |
|
|
const currentPath = `${ window.location.pathname }${ window.location.search }`; |
|
|
const basepath = router.options.basepath; |
|
|
|
|
|
|
|
|
if ( basepath && ! currentPath.startsWith( basepath ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( currentPath !== lastPath ) { |
|
|
router.navigate( { |
|
|
to: window.location.pathname, |
|
|
search: getQueryArgs( window.location.search ), |
|
|
replace: true, |
|
|
} ); |
|
|
lastPath = currentPath; |
|
|
} |
|
|
}; |
|
|
|
|
|
const isCompatibilityRoute = ( router: AnyRouter, url: string ) => { |
|
|
const matches = router.matchRoutes( url ); |
|
|
if ( ! matches ) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
const compatibilityRouteIds = compatibilityRoutes?.map( ( route: AnyRoute ) => route.id ) ?? []; |
|
|
return matches.some( ( match: { routeId: string } ) => |
|
|
compatibilityRouteIds.includes( match.routeId ) |
|
|
); |
|
|
}; |
|
|
|
|
|
const syncMemoryRouterToBrowserHistory = ( router: AnyRouter ) => { |
|
|
|
|
|
return router.history.subscribe( ( { location }: { location: Location } ) => { |
|
|
const { pathname, search } = location; |
|
|
const newUrl = `${ pathname }${ search }`; |
|
|
|
|
|
|
|
|
if ( compatibilityRoutes && isCompatibilityRoute( router, newUrl ) ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( window.location.pathname + window.location.search !== newUrl ) { |
|
|
pagejs.show( newUrl ); |
|
|
lastPath = newUrl; |
|
|
} |
|
|
} ); |
|
|
}; |
|
|
|
|
|
return { |
|
|
syncBrowserHistoryToRouter, |
|
|
syncMemoryRouterToBrowserHistory, |
|
|
}; |
|
|
} |
|
|
|