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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 1x 1x 1x 1x 1x 1x 1x 4x 71x 2x 2x 1x 1x 2x 1x 1x 1x 2x 2x 3x 3x 1x 2x 2x 2x 2x 2x 4x | import { createContext, lazy, Suspense, useContext, useState, useEffect } from 'react'
import { createBrowserRouter, Outlet, Navigate, useLocation, useRouteError } from 'react-router-dom'
import { Toaster } from 'sonner'
import { AIStatusProvider, useAIStatus } from './contexts/AIStatusContext'
import { ProfileProvider, useProfile } from './contexts/ProfileContext'
import { AuthProvider, useAuth } from './contexts/AuthContext'
import { api } from './api/client'
import ErrorBoundary from './components/ErrorBoundary'
const ProfileSelector = lazy(() => import('./screens/ProfileSelector'))
const PasskeyLogin = lazy(() => import('./screens/PasskeyLogin'))
const PasskeyRegister = lazy(() => import('./screens/PasskeyRegister'))
const Home = lazy(() => import('./screens/Home'))
const Search = lazy(() => import('./screens/Search'))
const RecipeDetail = lazy(() => import('./screens/RecipeDetail'))
const PlayMode = lazy(() => import('./screens/PlayMode'))
const Favorites = lazy(() => import('./screens/Favorites'))
const AllRecipes = lazy(() => import('./screens/AllRecipes'))
const Collections = lazy(() => import('./screens/Collections'))
const CollectionDetail = lazy(() => import('./screens/CollectionDetail'))
const Settings = lazy(() => import('./screens/Settings'))
// PairDevice and PasskeyManage are now inline tabs in Settings
function LoadingFallback() {
return (
<div className="flex min-h-screen items-center justify-center bg-background">
<div className="text-muted-foreground">Loading...</div>
</div>
)
}
// Syncs Sonner's theme with the app's CSS dark-mode class (.dark on <html>).
// Without this the Toaster defaults to light theme even when the user has
// selected a dark profile, making success/error colours look wrong.
function ThemeAwareToaster() {
const [theme, setTheme] = useState<'light' | 'dark'>(() =>
document.documentElement.classList.contains('dark') ? 'dark' : 'light'
)
useEffect(() => {
const observer = new MutationObserver(() => {
setTheme(document.documentElement.classList.contains('dark') ? 'dark' : 'light')
})
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] })
return () => observer.disconnect()
}, [])
return <Toaster position="top-center" richColors theme={theme} />
}
// Mode context — provides the operating mode to child components without hook violations
const ModeContext = createContext<'home' | 'passkey'>('home')
export function useMode() {
return useContext(ModeContext)
}
// Version is no longer fetched from the server (/api/system/mode/ dropped the
// `version` key in v1.42.0 to eliminate fingerprinting). Instead, the CD
// pipeline bakes COOKIE_VERSION into the SPA bundle as VITE_COOKIE_VERSION
// (see Dockerfile.prod). Falls back to 'dev' for local dev builds.
export function useVersion() {
return import.meta.env.VITE_COOKIE_VERSION || 'dev'
}
function AppLayout() {
const [mode, setMode] = useState<'home' | 'passkey' | null>(null)
useEffect(() => {
api.system
.mode()
.then((data) => {
setMode(data.mode === 'passkey' ? 'passkey' : 'home')
})
.catch(() => setMode('home'))
}, [])
if (mode === null) {
return <LoadingFallback />
}
Iif (mode === 'passkey') {
return (
<ModeContext.Provider value="passkey">
{/* Toaster lives OUTSIDE AuthProfileBridge on purpose.
AuthProfileBridge unmounts its children while isLoading=true
(e.g. during refreshSession after self-delete or login).
If the Toaster is inside, any in-flight toast is torn down
mid-animation and renders unstyled at the document-flow
default position (top-left on mobile). Keeping it at the
ModeContext level means it survives every auth-state change
within passkey mode. */}
<ThemeAwareToaster />
<AuthProvider>
<AIStatusProvider>
<AuthProfileBridge>
<ErrorBoundary>
<Suspense fallback={<LoadingFallback />}>
<Outlet />
</Suspense>
</ErrorBoundary>
</AuthProfileBridge>
</AIStatusProvider>
</AuthProvider>
</ModeContext.Provider>
)
}
return (
<ModeContext.Provider value="home">
{/* Same reasoning as the passkey branch above. */}
<ThemeAwareToaster />
<AIStatusProvider>
<ProfileProviderWithAIRefresh>
<ErrorBoundary>
<Suspense fallback={<LoadingFallback />}>
<Outlet />
</Suspense>
</ErrorBoundary>
</ProfileProviderWithAIRefresh>
</AIStatusProvider>
</ModeContext.Provider>
)
}
// Wraps ProfileProvider and forwards the AI status refresh so that
// AI features appear immediately after a profile is selected on first
// visit (before the initial /api/ai/status call a session was absent).
function ProfileProviderWithAIRefresh({
children,
authProfile,
}: {
children: React.ReactNode
authProfile?: { id: number; name: string; avatar_color: string; theme: string; unit_preference: string } | null
}) {
const { refresh } = useAIStatus()
return (
<ProfileProvider authProfile={authProfile} onProfileSelected={refresh}>
{children}
</ProfileProvider>
)
}
function AuthProfileBridge({ children }: { children: React.ReactNode }) {
const { profile: authProfile, isLoading } = useAuth()
if (isLoading) {
return <LoadingFallback />
}
return <ProfileProviderWithAIRefresh authProfile={authProfile}>{children}</ProfileProviderWithAIRefresh>
}
function ProtectedRoute({ children }: { children: React.ReactNode }) {
const { profile, loading } = useProfile()
const location = useLocation()
if (loading) {
return <LoadingFallback />
}
if (!profile) {
return <Navigate to="/" state={{ from: location }} replace />
}
return <>{children}</>
}
function PublicRoute({ children }: { children: React.ReactNode }) {
const { profile, loading } = useProfile()
if (loading) {
return <LoadingFallback />
}
Iif (profile) {
return <Navigate to="/home" replace />
}
return <>{children}</>
}
function RootRoute() {
const mode = useMode()
Iif (mode === 'passkey') {
return <PasskeyLogin />
}
return <ProfileSelector />
}
function RouteErrorFallback() {
const error = useRouteError()
console.error('Route error:', error)
return (
<div className="flex min-h-screen flex-col items-center justify-center bg-background p-4">
<div className="w-full max-w-md rounded-xl border border-border bg-card p-8 text-center shadow-lg">
<div className="mx-auto mb-4 flex h-16 w-16 items-center justify-center rounded-full bg-destructive/10">
<svg className="h-8 w-8 text-destructive" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
<path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z" />
<path d="M12 9v4" />
<path d="M12 17h.01" />
</svg>
</div>
<h1 className="mb-2 text-xl font-semibold text-foreground">Something went wrong</h1>
<p className="mb-6 text-sm text-muted-foreground">
An unexpected error occurred. Please try again.
</p>
<div className="flex flex-col gap-2">
<button
onClick={() => window.location.href = '/'}
className="w-full rounded-lg bg-primary px-4 py-2.5 text-sm font-medium text-primary-foreground transition-colors hover:bg-primary/90"
>
Go Home
</button>
<button
onClick={() => window.location.reload()}
className="w-full rounded-lg bg-muted px-4 py-2.5 text-sm font-medium text-muted-foreground transition-colors hover:bg-muted/80 hover:text-foreground"
>
Reload Page
</button>
</div>
</div>
</div>
)
}
export const router = createBrowserRouter([
{
element: <AppLayout />,
errorElement: <RouteErrorFallback />,
children: [
{
path: '/',
element: (
<PublicRoute>
<RootRoute />
</PublicRoute>
),
},
{
path: '/register',
element: (
<PublicRoute>
<PasskeyRegister />
</PublicRoute>
),
},
{
path: '/home',
element: (
<ProtectedRoute>
<Home />
</ProtectedRoute>
),
},
{
path: '/search',
element: (
<ProtectedRoute>
<Search />
</ProtectedRoute>
),
},
{
path: '/recipe/:id',
element: (
<ProtectedRoute>
<RecipeDetail />
</ProtectedRoute>
),
},
{
path: '/recipe/:id/play',
element: (
<ProtectedRoute>
<PlayMode />
</ProtectedRoute>
),
},
{
path: '/favorites',
element: (
<ProtectedRoute>
<Favorites />
</ProtectedRoute>
),
},
{
path: '/all-recipes',
element: (
<ProtectedRoute>
<AllRecipes />
</ProtectedRoute>
),
},
{
path: '/collections',
element: (
<ProtectedRoute>
<Collections />
</ProtectedRoute>
),
},
{
path: '/collection/:id',
element: (
<ProtectedRoute>
<CollectionDetail />
</ProtectedRoute>
),
},
{
path: '/settings',
element: (
<ProtectedRoute>
<Settings />
</ProtectedRoute>
),
},
{
path: '/pair-device',
element: <Navigate to="/settings" replace />,
},
{
path: '/passkeys',
element: <Navigate to="/settings" replace />,
},
{
path: '*',
element: <Navigate to="/" replace />,
},
],
},
])
|