{"version":3,"file":"Match.cjs","names":[],"sources":["../../src/Match.tsx"],"sourcesContent":["'use client'\n\nimport * as React from 'react'\nimport { useSelector } from '@tanstack/react-store'\nimport { isNotFound, rootRouteId } from '@tanstack/router-core'\nimport { isServer } from '@tanstack/router-core/isServer'\nimport { CatchBoundary, ErrorComponent } from './CatchBoundary'\nimport { useRouter } from './useRouter'\nimport { CatchNotFound } from './not-found'\nimport { matchContext } from './matchContext'\nimport { SafeFragment } from './SafeFragment'\nimport { renderRouteNotFound } from './renderRouteNotFound'\nimport { ScrollRestoration } from './scroll-restoration'\nimport { ClientOnly } from './ClientOnly'\nimport {\n  nonRouteComponentContext,\n  wrapInNonRouteComponentContext,\n} from './nonRouteComponentContext'\nimport type {\n  AnyRoute,\n  AnyRouteMatch,\n  RootRouteOptions,\n} from '@tanstack/router-core'\n\nexport function renderPending(\n  router: ReturnType<typeof useRouter>,\n  route?: AnyRoute,\n) {\n  const PendingComponent =\n    route?.options.pendingComponent ?? router.options.defaultPendingComponent\n  if (!PendingComponent) {\n    return null\n  }\n\n  const pendingElement = <PendingComponent />\n  return process.env.NODE_ENV !== 'production'\n    ? wrapInNonRouteComponentContext(pendingElement, 'pendingComponent')\n    : pendingElement\n}\n\ntype OutletMatchSelection = [\n  parentGlobalNotFound: boolean,\n  parentNotFoundError: unknown,\n]\n\nconst outletMatchSelectionEqual = (\n  a: OutletMatchSelection,\n  b: OutletMatchSelection,\n) => a[0] === b[0] && a[1] === b[1]\n\nconst canWrapInSuspense = (\n  router: ReturnType<typeof useRouter>,\n  route: AnyRoute,\n  ssr: AnyRouteMatch['ssr'],\n) =>\n  !route.isRoot ||\n  (route.options as RootRouteOptions).shellComponent ||\n  route.options.wrapInSuspense ||\n  ssr === false ||\n  ssr === 'data-only' ||\n  !((isServer ?? router.isServer) || router.ssr)\n\nexport const Match = React.memo(function MatchImpl({\n  routeId,\n}: {\n  routeId: string\n}) {\n  const router = useRouter()\n\n  if (isServer ?? router.isServer) {\n    const match = router.stores.byRoute.get(routeId)!.get()!\n    return <MatchView router={router} match={match} />\n  }\n\n  const matchStore = router.stores.getMatchStore(routeId)\n  // eslint-disable-next-line react-hooks/rules-of-hooks\n  const match = useSelector(matchStore)\n  return <MatchView router={router} match={match!} />\n})\n\nfunction MatchView({\n  router,\n  match,\n}: {\n  router: ReturnType<typeof useRouter>\n  match: AnyRouteMatch\n}) {\n  const route: AnyRoute = router.routesById[match.routeId]\n\n  const pendingElement = renderPending(router, route)\n\n  const routeErrorComponent =\n    route.options.errorComponent ?? router.options.defaultErrorComponent\n\n  const routeOnCatch = route.options.onCatch ?? router.options.defaultOnCatch\n\n  const routeNotFoundComponent = route.isRoot\n    ? // If it's the root route, use the _notFound option, with fallback to the notFoundRoute's component\n      (route.options.notFoundComponent ??\n      router.options.notFoundRoute?.options.component)\n    : route.options.notFoundComponent\n\n  const resolvedNoSsr = match.ssr === false || match.ssr === 'data-only'\n  // A root component may render the document itself. Only place its Suspense\n  // boundary in pure CSR, inside an explicit shell, or when explicitly opted in.\n  const ResolvedSuspenseBoundary =\n    canWrapInSuspense(router, route, match.ssr) &&\n    (route.options.wrapInSuspense ??\n      pendingElement ??\n      ((route.options.errorComponent as any)?.preload || resolvedNoSsr))\n      ? React.Suspense\n      : SafeFragment\n\n  const ResolvedCatchBoundary = routeErrorComponent\n    ? CatchBoundary\n    : SafeFragment\n\n  const ResolvedNotFoundBoundary = routeNotFoundComponent\n    ? CatchNotFound\n    : SafeFragment\n\n  const ShellComponent = route.isRoot\n    ? ((route.options as RootRouteOptions).shellComponent ?? SafeFragment)\n    : SafeFragment\n  return (\n    <ShellComponent>\n      <matchContext.Provider value={match.routeId}>\n        <ResolvedSuspenseBoundary fallback={pendingElement}>\n          <ResolvedCatchBoundary\n            getResetKey={() => match}\n            errorComponent={routeErrorComponent as any}\n            onCatch={(error, errorInfo) => {\n              // Forward not found errors (we don't want to show the error component for these)\n              if (isNotFound(error)) {\n                error.routeId ??= match.routeId\n                throw error\n              }\n              if (process.env.NODE_ENV !== 'production') {\n                console.warn(`Warning: Error in route match: ${match.id}`)\n              }\n              routeOnCatch?.(error, errorInfo)\n            }}\n          >\n            <ResolvedNotFoundBoundary\n              fallback={(error) => {\n                error.routeId ??= match.routeId\n\n                if (error.routeId !== match.routeId) {\n                  throw error\n                }\n\n                const notFoundElement = React.createElement(\n                  routeNotFoundComponent!,\n                  error as any,\n                )\n                return process.env.NODE_ENV !== 'production'\n                  ? wrapInNonRouteComponentContext(\n                      notFoundElement,\n                      'notFoundComponent',\n                    )\n                  : notFoundElement\n              }}\n            >\n              {resolvedNoSsr ? (\n                <ClientOnly fallback={pendingElement}>\n                  <MatchInner match={match} />\n                </ClientOnly>\n              ) : (\n                <MatchInner match={match} />\n              )}\n            </ResolvedNotFoundBoundary>\n          </ResolvedCatchBoundary>\n        </ResolvedSuspenseBoundary>\n      </matchContext.Provider>\n      {(isServer ?? router.isServer) &&\n      route.parentRoute?.id === rootRouteId &&\n      router.options.scrollRestoration ? (\n        <ScrollRestoration />\n      ) : null}\n    </ShellComponent>\n  )\n}\n\nexport const MatchInner = React.memo(function MatchInnerImpl({\n  match,\n}: {\n  match: AnyRouteMatch\n}): any {\n  const router = useRouter()\n  const routeId = match.routeId\n  const route = router.routesById[routeId] as AnyRoute\n  const key = React.useMemo(() => {\n    const remountFn =\n      route.options.remountDeps ?? router.options.defaultRemountDeps\n    const remountDeps = remountFn?.({\n      routeId,\n      loaderDeps: match.loaderDeps,\n      params: match._strictParams,\n      search: match._strictSearch,\n    })\n    return remountDeps ? JSON.stringify(remountDeps) : undefined\n  }, [\n    routeId,\n    match.loaderDeps,\n    match._strictParams,\n    match._strictSearch,\n    route.options.remountDeps,\n    router.options.defaultRemountDeps,\n  ])\n  const out = React.useMemo(() => {\n    const Comp = route.options.component ?? router.options.defaultComponent\n    return Comp ? <Comp key={key} /> : <Outlet />\n  }, [key, route.options.component, router.options.defaultComponent])\n\n  if (match.status === 'pending') {\n    if (router.ssr && !canWrapInSuspense(router, route, match.ssr)) {\n      // Replacing an SSR document root with pending UI would remove <html>.\n      // Hydrated matches retain their prior data, so keep rendering it.\n      return out\n    }\n    if (router._tx) {\n      throw router._tx[5]\n    }\n    return renderPending(router, route)\n  }\n\n  if (match.status === 'notFound') {\n    return renderRouteNotFound(router, route, match.error)\n  }\n\n  if (match.status === 'error') {\n    if (isServer ?? router.isServer) {\n      const RouteErrorComponent =\n        (route.options.errorComponent ??\n          router.options.defaultErrorComponent) ||\n        ErrorComponent\n      const errorElement = (\n        <RouteErrorComponent\n          error={match.error}\n          reset={undefined as any}\n          info={{\n            componentStack: '',\n          }}\n        />\n      )\n      return process.env.NODE_ENV !== 'production'\n        ? wrapInNonRouteComponentContext(errorElement, 'errorComponent')\n        : errorElement\n    }\n    throw match.error\n  }\n\n  return out\n})\n\n/**\n * Render the next child match in the route tree. Typically used inside\n * a route component to render nested routes.\n *\n * @link https://tanstack.com/router/latest/docs/framework/react/api/router/outletComponent\n */\nexport const Outlet = React.memo(function OutletImpl() {\n  if (process.env.NODE_ENV !== 'production') {\n    // eslint-disable-next-line react-hooks/rules-of-hooks\n    const nonRouteComponent = React.useContext(nonRouteComponentContext!)\n    if (nonRouteComponent) {\n      console.warn(\n        `Warning: An <Outlet /> was rendered inside a ${nonRouteComponent}. <Outlet /> should only be rendered inside a route component.`,\n      )\n    }\n  }\n\n  const router = useRouter()\n  const routeId = React.useContext(matchContext)!\n\n  let parentGlobalNotFound: boolean\n  let parentNotFoundError: unknown\n  let childRouteId: string | undefined\n\n  if (isServer ?? router.isServer) {\n    const matches = router.stores.matches.get()\n    const parentIndex = matches.findIndex((match) => match.routeId === routeId)\n    const parentMatch = matches[parentIndex]!\n    parentGlobalNotFound = !!parentMatch._notFound\n    parentNotFoundError = parentMatch.error\n    childRouteId = matches[parentIndex + 1]?.routeId\n  } else {\n    const parentMatchStore = router.stores.getMatchStore(routeId)\n\n    // eslint-disable-next-line react-hooks/rules-of-hooks\n    ;[parentGlobalNotFound, parentNotFoundError] = useSelector(\n      parentMatchStore,\n      (match): OutletMatchSelection => [!!match!._notFound, match!.error],\n      { compare: outletMatchSelectionEqual },\n    )\n\n    // eslint-disable-next-line react-hooks/rules-of-hooks\n    childRouteId = useSelector(router.stores.ids, (ids) => {\n      return ids[ids.indexOf(routeId) + 1]\n    })\n  }\n\n  if (parentGlobalNotFound) {\n    return renderRouteNotFound(\n      router,\n      router.routesById[routeId],\n      parentNotFoundError,\n    )\n  }\n\n  if (!childRouteId) {\n    return null\n  }\n\n  const nextMatch = <Match routeId={childRouteId} />\n\n  if (routeId === rootRouteId) {\n    return (\n      <React.Suspense fallback={renderPending(router)}>\n        {nextMatch}\n      </React.Suspense>\n    )\n  }\n\n  return nextMatch\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;AAwBA,SAAgB,cACd,QACA,OACA;CACA,MAAM,mBACJ,OAAO,QAAQ,oBAAoB,OAAO,QAAQ;CACpD,IAAI,CAAC,kBACH,OAAO;CAGT,MAAM,iBAAiB,iBAAA,GAAA,kBAAA,KAAC,kBAAD,CAAmB,CAAA;CAC1C,OAAA,QAAA,IAAA,aAAgC,eAC5B,iCAAA,+BAA+B,gBAAgB,kBAAkB,IACjE;AACN;AAOA,IAAM,6BACJ,GACA,MACG,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE;AAEjC,IAAM,qBACJ,QACA,OACA,QAEA,CAAC,MAAM,UACN,MAAM,QAA6B,kBACpC,MAAM,QAAQ,kBACd,QAAQ,SACR,QAAQ,eACR,GAAG,+BAAA,YAAY,OAAO,aAAa,OAAO;AAE5C,IAAa,QAAQ,MAAM,KAAK,SAAS,UAAU,EACjD,WAGC;CACD,MAAM,SAAS,kBAAA,UAAU;CAEzB,IAAI,+BAAA,YAAY,OAAO,UAErB,OAAO,iBAAA,GAAA,kBAAA,KAAC,WAAD;EAAmB;EAAe,OAD3B,OAAO,OAAO,QAAQ,IAAI,OAAO,EAAG,IACT;CAAQ,CAAA;CAMnD,OAAO,iBAAA,GAAA,kBAAA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