70 lines
2.1 KiB
TypeScript
70 lines
2.1 KiB
TypeScript
import { useMatch } from './useMatch'
|
|
import type {
|
|
StructuralSharingOption,
|
|
ValidateSelected,
|
|
} from './structuralSharing'
|
|
import type {
|
|
AnyRouter,
|
|
RegisteredRouter,
|
|
ResolveUseLoaderDeps,
|
|
StrictOrFrom,
|
|
UseLoaderDepsResult,
|
|
} from '@tanstack/router-core'
|
|
|
|
export interface UseLoaderDepsBaseOptions<
|
|
TRouter extends AnyRouter,
|
|
TFrom,
|
|
TSelected,
|
|
TStructuralSharing,
|
|
> {
|
|
select?: (
|
|
deps: ResolveUseLoaderDeps<TRouter, TFrom>,
|
|
) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
|
|
}
|
|
|
|
export type UseLoaderDepsOptions<
|
|
TRouter extends AnyRouter,
|
|
TFrom extends string | undefined,
|
|
TSelected,
|
|
TStructuralSharing,
|
|
> = StrictOrFrom<TRouter, TFrom> &
|
|
UseLoaderDepsBaseOptions<TRouter, TFrom, TSelected, TStructuralSharing> &
|
|
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>
|
|
|
|
export type UseLoaderDepsRoute<out TId> = <
|
|
TRouter extends AnyRouter = RegisteredRouter,
|
|
TSelected = unknown,
|
|
TStructuralSharing extends boolean = boolean,
|
|
>(
|
|
opts?: UseLoaderDepsBaseOptions<TRouter, TId, TSelected, TStructuralSharing> &
|
|
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
|
|
) => UseLoaderDepsResult<TRouter, TId, TSelected>
|
|
|
|
/**
|
|
* Read and select the current route's loader dependencies object.
|
|
*
|
|
* Options:
|
|
* - `from`: Choose which route's loader deps to read
|
|
* - `select`: Map the deps to a derived value
|
|
* - `structuralSharing`: Enable structural sharing for stable references
|
|
*
|
|
* @returns The loader deps (or selected value) for the matched route.
|
|
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useLoaderDepsHook
|
|
*/
|
|
export function useLoaderDeps<
|
|
TRouter extends AnyRouter = RegisteredRouter,
|
|
const TFrom extends string | undefined = undefined,
|
|
TSelected = unknown,
|
|
TStructuralSharing extends boolean = boolean,
|
|
>(
|
|
opts: UseLoaderDepsOptions<TRouter, TFrom, TSelected, TStructuralSharing>,
|
|
): UseLoaderDepsResult<TRouter, TFrom, TSelected> {
|
|
const { select, ...rest } = opts
|
|
return useMatch({
|
|
...rest,
|
|
select: (s) => {
|
|
return select ? select(s.loaderDeps) : s.loaderDeps
|
|
},
|
|
}) as UseLoaderDepsResult<TRouter, TFrom, TSelected>
|
|
}
|