Files
med-notes/.pnpm-store/v10/files/b0/2a51ea76be22e99753c78ca9206eb7a664a670e08807ab9682e497a621b6367c45cd8632f2b868d206358df2be888819b867ef06d278d24dd750bf8393f23d
2025-05-09 05:30:08 +02:00

94 lines
2.2 KiB
Plaintext

import { useMatch } from './useMatch'
import type {
StructuralSharingOption,
ValidateSelected,
} from './structuralSharing'
import type {
AnyRouter,
RegisteredRouter,
ResolveUseSearch,
StrictOrFrom,
ThrowConstraint,
ThrowOrOptional,
UseSearchResult,
} from '@tanstack/router-core'
export interface UseSearchBaseOptions<
TRouter extends AnyRouter,
TFrom,
TStrict extends boolean,
TThrow extends boolean,
TSelected,
TStructuralSharing,
> {
select?: (
state: ResolveUseSearch<TRouter, TFrom, TStrict>,
) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
shouldThrow?: TThrow
}
export type UseSearchOptions<
TRouter extends AnyRouter,
TFrom,
TStrict extends boolean,
TThrow extends boolean,
TSelected,
TStructuralSharing,
> = StrictOrFrom<TRouter, TFrom, TStrict> &
UseSearchBaseOptions<
TRouter,
TFrom,
TStrict,
TThrow,
TSelected,
TStructuralSharing
> &
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>
export type UseSearchRoute<out TFrom> = <
TRouter extends AnyRouter = RegisteredRouter,
TSelected = unknown,
TStructuralSharing extends boolean = boolean,
>(
opts?: UseSearchBaseOptions<
TRouter,
TFrom,
/* TStrict */ true,
/* TThrow */ true,
TSelected,
TStructuralSharing
> &
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
) => UseSearchResult<TRouter, TFrom, true, TSelected>
export function useSearch<
TRouter extends AnyRouter = RegisteredRouter,
const TFrom extends string | undefined = undefined,
TStrict extends boolean = true,
TThrow extends boolean = true,
TSelected = unknown,
TStructuralSharing extends boolean = boolean,
>(
opts: UseSearchOptions<
TRouter,
TFrom,
TStrict,
ThrowConstraint<TStrict, TThrow>,
TSelected,
TStructuralSharing
>,
): ThrowOrOptional<
UseSearchResult<TRouter, TFrom, TStrict, TSelected>,
TThrow
> {
return useMatch({
from: opts.from!,
strict: opts.strict,
shouldThrow: opts.shouldThrow,
structuralSharing: opts.structuralSharing,
select: (match: any) => {
return opts.select ? opts.select(match.search) : match.search
},
}) as any
}