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 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 4x 4x 71x 2x 2x 2x 1x 1x 1x 2x 1x 1x 1x 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 } 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>
)
}
// Mode context — provides the operating mode to child components without hook violations
const ModeContext = createContext<'home' | 'passkey'>('home')
const VersionContext = createContext<string>('dev')
export function useMode() {
return useContext(ModeContext)
}
export function useVersion() {
return useContext(VersionContext)
}
function AppLayout() {
const [mode, setMode] = useState<'home' | 'passkey' | null>(null)
const [version, setVersion] = useState('dev')
useEffect(() => {
api.system
.mode()
.then((data) => {
setMode(data.mode === 'passkey' ? 'passkey' : 'home')
setVersion(data.version || 'dev')
})
.catch(() => setMode('home'))
}, [])
if (mode === null) {
return <LoadingFallback />
}
Iif (mode === 'passkey') {
return (
<VersionContext.Provider value={version}>
<ModeContext.Provider value="passkey">
<AuthProvider>
<AIStatusProvider>
<AuthProfileBridge>
<ErrorBoundary>
<Toaster position="top-center" richColors />
<Suspense fallback={<LoadingFallback />}>
<Outlet />
</Suspense>
</ErrorBoundary>
</AuthProfileBridge>
</AIStatusProvider>
</AuthProvider>
</ModeContext.Provider>
</VersionContext.Provider>
)
}
return (
<VersionContext.Provider value={version}>
<ModeContext.Provider value="home">
<AIStatusProvider>
<ProfileProvider>
<ErrorBoundary>
<Toaster position="top-center" richColors />
<Suspense fallback={<LoadingFallback />}>
<Outlet />
</Suspense>
</ErrorBoundary>
</ProfileProvider>
</AIStatusProvider>
</ModeContext.Provider>
</VersionContext.Provider>
)
}
function AuthProfileBridge({ children }: { children: React.ReactNode }) {
const { profile: authProfile, isLoading } = useAuth()
if (isLoading) {
return <LoadingFallback />
}
return <ProfileProvider authProfile={authProfile}>{children}</ProfileProvider>
}
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 />,
},
],
},
])
|