|
| 1 | +import * as React from 'react' |
| 2 | +import { useStore } from '@tanstack/react-store' |
| 3 | +import { isServer } from '@tanstack/router-core/isServer' |
| 4 | +import { useRouter } from './useRouter' |
| 5 | +import type { |
| 6 | + AnyRouter, |
| 7 | + LinkOptions, |
| 8 | + ParsedLocation, |
| 9 | + RegisteredRouter, |
| 10 | +} from '@tanstack/router-core' |
| 11 | + |
| 12 | +/** |
| 13 | + * How a target is matched against the previous history entry: |
| 14 | + * - `'pathname'` — match by pathname only (so going back restores the previous |
| 15 | + * entry's exact search params and scroll position). |
| 16 | + * - `'exact'` — match by pathname **and** search. |
| 17 | + */ |
| 18 | +export type BackNavigationMatch = 'pathname' | 'exact' |
| 19 | + |
| 20 | +/** |
| 21 | + * Decide whether navigating to `target` would land on the *previous* history |
| 22 | + * entry, meaning a `history.back()` is preferable to a push. |
| 23 | + * |
| 24 | + * Pure and framework-agnostic: it relies only on the router's in-memory |
| 25 | + * per-index entry tracking (`router.getHistoryEntry`). Returns `false` at the |
| 26 | + * start of history (index 0) and when the previous entry is unknown (e.g. a |
| 27 | + * fresh page load or deep link), so callers degrade gracefully to a normal push. |
| 28 | + */ |
| 29 | +export function resolveIsBackNavigation( |
| 30 | + router: AnyRouter, |
| 31 | + currentLocation: ParsedLocation, |
| 32 | + target: Pick<ParsedLocation, 'pathname' | 'searchStr'>, |
| 33 | + match: BackNavigationMatch = 'pathname', |
| 34 | +): boolean { |
| 35 | + const currentIndex = currentLocation.state.__TSR_index ?? 0 |
| 36 | + if (currentIndex === 0) return false |
| 37 | + const previous = router.getHistoryEntry(currentIndex - 1) |
| 38 | + if (!previous || previous.pathname !== target.pathname) return false |
| 39 | + if (match === 'exact') return previous.searchStr === target.searchStr |
| 40 | + return true |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Returns `true` when a link/navigation to the given options would resolve to |
| 45 | + * the *previous* history entry — i.e. clicking it should go "back" rather than |
| 46 | + * push a new entry. |
| 47 | + * |
| 48 | + * This is the primitive behind the `preferBack` prop on `Link`. Use it directly |
| 49 | + * when building custom links or buttons that want the same history-aware |
| 50 | + * behavior. It returns `false` on the server and falls back to `false` whenever |
| 51 | + * the previous entry is unknown to the router, so it is always safe to branch on. |
| 52 | + * |
| 53 | + * Pass `match: 'exact'` to also require the search to match (defaults to |
| 54 | + * `'pathname'`, which restores the previous entry's exact search and scroll). |
| 55 | + * |
| 56 | + * @example |
| 57 | + * const isBack = useIsBackNavigation({ to: '/issues' }) |
| 58 | + * // isBack === true -> call router.history.back() |
| 59 | + * // isBack === false -> navigate normally |
| 60 | + */ |
| 61 | +export function useIsBackNavigation< |
| 62 | + TRouter extends AnyRouter = RegisteredRouter, |
| 63 | + const TFrom extends string = string, |
| 64 | + const TTo extends string | undefined = undefined, |
| 65 | + const TMaskFrom extends string = TFrom, |
| 66 | + const TMaskTo extends string = '', |
| 67 | +>( |
| 68 | + options: LinkOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>, |
| 69 | + match: BackNavigationMatch = 'pathname', |
| 70 | +): boolean { |
| 71 | + const router = useRouter() |
| 72 | + |
| 73 | + // On the server there is no client-side history to pop, and the previous |
| 74 | + // entry is never known, so back-navigation is always false. |
| 75 | + if (isServer ?? router.isServer) { |
| 76 | + return false |
| 77 | + } |
| 78 | + |
| 79 | + // eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static (compile-time `isServer`) |
| 80 | + const currentLocation = useStore( |
| 81 | + router.stores.location, |
| 82 | + (l) => l, |
| 83 | + (prev, next) => prev.href === next.href, |
| 84 | + ) |
| 85 | + |
| 86 | + // eslint-disable-next-line react-hooks/rules-of-hooks |
| 87 | + return React.useMemo(() => { |
| 88 | + const next = router.buildLocation({ |
| 89 | + _fromLocation: currentLocation, |
| 90 | + ...options, |
| 91 | + } as any) |
| 92 | + return resolveIsBackNavigation(router, currentLocation, next, match) |
| 93 | + }, [router, currentLocation, options, match]) |
| 94 | +} |
0 commit comments