|
|
import type { AppRouterInstance } from '../app-router-context.shared-runtime' |
|
|
import type { Params } from '../../../server/request/params' |
|
|
import type { NextRouter } from './router' |
|
|
|
|
|
import React, { useMemo, useRef } from 'react' |
|
|
import { PathnameContext } from '../hooks-client-context.shared-runtime' |
|
|
import { isDynamicRoute } from './utils' |
|
|
import { asPathToSearchParams } from './utils/as-path-to-search-params' |
|
|
import { getRouteRegex } from './utils/route-regex' |
|
|
|
|
|
|
|
|
export function adaptForAppRouterInstance( |
|
|
pagesRouter: NextRouter |
|
|
): AppRouterInstance { |
|
|
return { |
|
|
back() { |
|
|
pagesRouter.back() |
|
|
}, |
|
|
forward() { |
|
|
pagesRouter.forward() |
|
|
}, |
|
|
refresh() { |
|
|
pagesRouter.reload() |
|
|
}, |
|
|
hmrRefresh() {}, |
|
|
push(href, { scroll } = {}) { |
|
|
void pagesRouter.push(href, undefined, { scroll }) |
|
|
}, |
|
|
replace(href, { scroll } = {}) { |
|
|
void pagesRouter.replace(href, undefined, { scroll }) |
|
|
}, |
|
|
prefetch(href) { |
|
|
void pagesRouter.prefetch(href) |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function adaptForSearchParams( |
|
|
router: Pick<NextRouter, 'isReady' | 'query' | 'asPath'> |
|
|
): URLSearchParams { |
|
|
if (!router.isReady || !router.query) { |
|
|
return new URLSearchParams() |
|
|
} |
|
|
|
|
|
return asPathToSearchParams(router.asPath) |
|
|
} |
|
|
|
|
|
export function adaptForPathParams( |
|
|
router: Pick<NextRouter, 'isReady' | 'pathname' | 'query' | 'asPath'> |
|
|
): Params | null { |
|
|
if (!router.isReady || !router.query) { |
|
|
return null |
|
|
} |
|
|
const pathParams: Params = {} |
|
|
const routeRegex = getRouteRegex(router.pathname) |
|
|
const keys = Object.keys(routeRegex.groups) |
|
|
for (const key of keys) { |
|
|
pathParams[key] = router.query[key]! |
|
|
} |
|
|
return pathParams |
|
|
} |
|
|
|
|
|
export function PathnameContextProviderAdapter({ |
|
|
children, |
|
|
router, |
|
|
...props |
|
|
}: React.PropsWithChildren<{ |
|
|
router: Pick<NextRouter, 'pathname' | 'asPath' | 'isReady' | 'isFallback'> |
|
|
isAutoExport: boolean |
|
|
}>) { |
|
|
const ref = useRef(props.isAutoExport) |
|
|
const value = useMemo(() => { |
|
|
|
|
|
|
|
|
|
|
|
const isAutoExport = ref.current |
|
|
if (isAutoExport) { |
|
|
ref.current = false |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (isDynamicRoute(router.pathname)) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (router.isFallback) { |
|
|
return null |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isAutoExport && !router.isReady) { |
|
|
return null |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let url: URL |
|
|
try { |
|
|
url = new URL(router.asPath, 'http://f') |
|
|
} catch (_) { |
|
|
|
|
|
return '/' |
|
|
} |
|
|
|
|
|
return url.pathname |
|
|
}, [router.asPath, router.isFallback, router.isReady, router.pathname]) |
|
|
|
|
|
return ( |
|
|
<PathnameContext.Provider value={value}> |
|
|
{children} |
|
|
</PathnameContext.Provider> |
|
|
) |
|
|
} |
|
|
|