import { createFileRoute, redirect, type AnyRouter } from '@tanstack/react-router' import { IS_PLATFORM } from '@/lib/constants' // `/` is never rendered — it always redirects: platform sends users to `/org` // (or `/new/new-project` when deep-linked with `?next=new-project`), // self-hosted sends them straight to `/project/default`. export const Route = createFileRoute('/')({ validateSearch: (search: Record): { next?: string } => ({ next: typeof search.next === 'string' ? search.next : undefined, }), beforeLoad: ({ search, location }) => { // Carry the incoming query and hash through to the destination — deep // links like `/?next=new-project&projectName=x` must // keep `projectName`. Only the consumed `next` param is dropped (and only // when it matched); everything else passes through. const carried = (shouldConsumeNext: boolean) => { const carriedSearch: Record = { ...location.search } if (shouldConsumeNext) delete carriedSearch.next return carriedSearch } // `to`, never `href`: preloading a Link whose beforeLoad throws // `redirect({ href })` recurses forever // (https://github.com/TanStack/router/issues/7141). The `` type arguments opt out of the registered route tree's strict // typing so the carried free-form search record is accepted. if (IS_PLATFORM) { if (search.next === 'new-project') { throw redirect({ to: '/new/new-project', search: carried(true), hash: location.hash, }) } throw redirect({ to: '/org', search: carried(false), hash: location.hash }) } throw redirect({ to: '/project/default', search: carried(false), hash: location.hash, }) }, })