import { Link as TanStackLink, useMatches, useRouterState, useSearch, useParams as useTanStackParams, useRouter as useTanStackRouter, type AnyRouter, } from '@tanstack/react-router' import { DefaultFrameworkImage, DefaultFrameworkLink, FrameworkProvider, resolveStaticImageSrc, type FrameworkContextValue, type FrameworkImageProps, type FrameworkLinkProps, } from 'common' import { forwardRef, useMemo, type ForwardedRef, type ReactNode } from 'react' import { applyBasePath, isAbsoluteUrl, splitInternalUrl, toRoutePattern } from '@/lib/internal-url' import { navigateToUrl } from '@/lib/navigation' function normalizeParams( search: Record | undefined, params: Record | undefined ): Record { const merged = { ...search, ...params } const normalized: Record = {} for (const [key, value] of Object.entries(merged)) { if (typeof value === 'string') { normalized[key] = value } else if (Array.isArray(value)) { normalized[key] = value.map(String) } else if (value !== undefined && value !== null) { normalized[key] = String(value) } } return normalized } function resolveSearchOrHashOnlyTarget( target: string, currentPathname: string, currentSearch: string ): string { if (target.startsWith('?')) { return `${currentPathname}${target}` } if (target.startsWith('#')) { return `${currentPathname}${currentSearch}${target}` } return target } const StudioLink = forwardRef(function StudioLink( props: FrameworkLinkProps, ref: ForwardedRef ) { const { href, prefetch, replace, scroll: _scroll, shallow: _shallow, children, ...rest } = props const currentPathname = useRouterState({ select: (state) => (state.resolvedLocation ?? state.location).pathname, }) const currentSearch = useRouterState({ select: (state) => (state.resolvedLocation ?? state.location).searchStr, }) const resolvedHref = resolveSearchOrHashOnlyTarget(href, currentPathname, currentSearch) const { to, search, hash } = splitInternalUrl(resolvedHref) if (isAbsoluteUrl(to)) { return } const preload = prefetch === false ? false : prefetch ? 'intent' : undefined return ( to={to} search={search as never} hash={hash} replace={replace} preload={preload} ref={ref} {...rest} > {children} ) }) function StudioImage({ src, ...props }: FrameworkImageProps) { return } export function StudioFrameworkProvider({ children }: { children: ReactNode }) { const router = useTanStackRouter() const committedLocation = useRouterState({ select: (state) => state.resolvedLocation ?? state.location, }) // `resolvedLocation` stays unset until the first load commits. Before then `location` can already // point at a redirect target while the initial matches are still pending. const isReady = useRouterState({ select: (state) => state.resolvedLocation !== undefined }) const leafRouteId = useMatches({ select: (matches) => matches[matches.length - 1]?.routeId }) ?? committedLocation.pathname const routeParams = useTanStackParams({ strict: false }) const searchParams = useSearch({ strict: false }) const value = useMemo(() => { const pathname = toRoutePattern(leafRouteId) const asPath = committedLocation.href const currentPathname = committedLocation.pathname const currentSearch = committedLocation.searchStr const params = normalizeParams( searchParams as Record | undefined, routeParams as Record | undefined ) return { Link: StudioLink, Image: StudioImage, pathname, asPath, isReady, params, push: async (path: string) => { await navigateToUrl( router.navigate, resolveSearchOrHashOnlyTarget(path, currentPathname, currentSearch) ) }, } }, [ committedLocation.href, committedLocation.pathname, committedLocation.searchStr, isReady, leafRouteId, routeParams, router, searchParams, ]) return {children} }