Files
slm-design/examples/react-vite/src/app/app.tsx
2026-08-10 19:13:20 +03:00

47 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Navigate, Route, Routes } from 'react-router-dom'
import { SignInScreen } from 'compositions/screens/sign-in'
import { StorefrontScreen } from 'compositions/screens/storefront'
import { OrdersProvider } from 'domains/orders'
import { useSessionState } from 'domains/session'
import styles from './styles/app.module.css'
/**
* Browser entry, выбирающий экран по session lifecycle.
*
* Используется для:
* - защиты storefront route пользовательской сессией
* - завершения bootstrap до первого route render
*/
export const App = () => {
const { status } = useSessionState()
if (status === 'restoring') {
return (
<main className={styles.loader} aria-live="polite">
<span className={styles.loaderMark}>S</span>
<strong>Восстанавливаем сессию</strong>
<small>Simple API · JWT rotation</small>
</main>
)
}
const isAuthenticated = status === 'authenticated'
const signInElement = isAuthenticated ? <Navigate to="/" replace /> : <SignInScreen />
const storefrontElement = isAuthenticated ? (
<OrdersProvider>
<StorefrontScreen />
</OrdersProvider>
) : (
<Navigate to="/login" replace />
)
return (
<Routes>
<Route path="/login" element={signInElement} />
<Route path="/" element={storefrontElement} />
<Route path="*" element={<Navigate to={isAuthenticated ? '/' : '/login'} replace />} />
</Routes>
)
}