| 1 | import { createBrowserRouter, redirect } from 'react-router' |
| 2 | import { Layout } from './components/Layout' |
| 3 | import { NotFound } from './components/ui/Notice' |
| 4 | import { Welcome } from './features/auth/Welcome' |
| 5 | import { Home } from './features/feed/Home' |
| 6 | import { Notifications } from './features/notifications/Notifications' |
| 7 | import { Profile } from './features/profile/Profile' |
| 8 | import { getSession } from './lib/api' |
| 9 | |
| 10 | // Without a token everything is the welcome page, and with one the |
| 11 | // welcome page is home. Loaders decide before anything renders. |
| 12 | export const router = createBrowserRouter([ |
| 13 | { |
| 14 | path: '/welcome', |
| 15 | loader: () => (getSession() ? redirect('/') : null), |
| 16 | Component: Welcome, |
| 17 | }, |
| 18 | { |
| 19 | loader: () => (getSession() ? null : redirect('/welcome')), |
| 20 | Component: Layout, |
| 21 | children: [ |
| 22 | { index: true, Component: Home }, |
| 23 | { path: 'u/:handle', Component: Profile }, |
| 24 | { path: 'notifications', Component: Notifications }, |
| 25 | { path: '*', Component: NotFound }, |
| 26 | ], |
| 27 | }, |
| 28 | ]) |