All files / src/features/landing/routes LandingRoute.tsx

100% Statements 15/15
100% Branches 8/8
100% Functions 4/4
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                        2x                                             4x   4x           1x 1x       6x 6x   6x 2x 1x     4x 3x               1x    
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Navigate } from 'react-router-dom'
 
import { useCurrentUser } from '@/features/auth/api/auth.hooks'
import { useWakeApi } from '@/hooks/useWakeApi'
import { readCookie } from '@/lib/cookies'
 
import { LandingPage } from '../components/LandingPage'
 
// The double-submit cookie: readable by JS on purpose, set with every session
// and cleared at logout, and it lives exactly as long as the refresh token.
const SESSION_HINT_COOKIE = 'csrf_token'
 
/**
 * `/` — the landing page for a visitor, the dashboard for someone signed in.
 *
 * **No request at all for the visitor.** Whether a session exists cannot be
 * read from JS (the tokens are HttpOnly), and asking the API would hold this
 * page for as long as the free Render instance takes to wake — about a minute,
 * on exactly the visit that matters most, someone following the CV's link. The
 * CSRF cookie is the hint instead: without it there is no session to find, and
 * the page renders at once.
 *
 * With it, `/auth/me` decides: signed in goes to the dashboard, as `/` always
 * did; a 401 (the http client's silent refresh already tried) shows the page.
 * `expectsAnonymous` keeps that 401 from tripping the global "session expired"
 * redirect to /login.
 *
 * A demo session sees the page too. The demo starts here, and a visitor who
 * presses the browser's back button from inside it expects to come back here;
 * redirected to the dashboard, the button looked broken. The page's demo
 * buttons then return to the running demo instead of starting another.
 */
export function LandingRoute() {
  const [hasSessionHint] = useState(() => readCookie(SESSION_HINT_COOKIE) !== null)
 
  return hasSessionHint ? <SessionAwareLanding /> : <VisitorLanding />
}
 
function VisitorLanding() {
  // Nothing on this page needs the API, but the demo button and sign-in will,
  // so the sleeping instance starts waking while the visitor reads.
  useWakeApi()
  return <LandingPage />
}
 
function SessionAwareLanding() {
  const { t } = useTranslation()
  const currentUser = useCurrentUser({ expectsAnonymous: true })
 
  if (currentUser.isSuccess) {
    if (currentUser.data.demoExpiresAt) return <LandingPage demoActive />
    return <Navigate to="/dashboard" replace />
  }
 
  if (currentUser.isPending) {
    return (
      <div role="status" className="flex min-h-screen items-center justify-center bg-auth-surface">
        <div className="h-8 w-8 animate-spin rounded-full border-2 border-primary border-t-transparent" />
        <span className="sr-only">{t('common.loading')}</span>
      </div>
    )
  }
 
  return <LandingPage />
}