import { LogOut, ShieldCheck } from 'lucide-react' import { useCallback, useEffect, useRef, useState, type FormEvent } from 'react' import { ApiError, call, type Card } from './api' import { AuthError, expiresAt, signIn, signOut, verifyCode } from './auth' import { Comments, Dashboard, Journal, Members, Reviews } from './screens' /** Déconnexion automatique après 15 minutes sans toucher à la console. */ const IDLE_MS = 15 * 60_000 type Tab = 'dashboard' | 'members' | 'reviews' | 'comments' | 'journal' const TABS: { id: Tab; label: string }[] = [ { id: 'dashboard', label: 'Tableau de bord' }, { id: 'members', label: 'Membres' }, { id: 'reviews', label: 'Avis' }, { id: 'comments', label: 'Commentaires' }, { id: 'journal', label: 'Journal' }, ] export default function App() { // Jeton gardé en mémoire seulement : recharger la page déconnecte const [session, setSession] = useState<{ token: string; me: Card }>() const [notice, setNotice] = useState() const logout = useCallback((why?: string) => { setSession((s) => { if (s) void signOut(s.token) return undefined }) setNotice(why) }, []) // Fin de validité du jeton, inactivité : retour à l'écran de connexion useEffect(() => { if (!session) return const end = window.setTimeout(() => logout('Session terminée (1 h) : reconnecte-toi.'), Math.max(0, expiresAt(session.token) - Date.now() - 30_000)) let idle = window.setTimeout(() => logout('Déconnecté après 15 minutes sans activité.'), IDLE_MS) const touch = () => { window.clearTimeout(idle) idle = window.setTimeout(() => logout('Déconnecté après 15 minutes sans activité.'), IDLE_MS) } for (const ev of ['pointerdown', 'keydown'] as const) window.addEventListener(ev, touch) return () => { window.clearTimeout(end) window.clearTimeout(idle) for (const ev of ['pointerdown', 'keydown'] as const) window.removeEventListener(ev, touch) } }, [session, logout]) if (!session) return (setNotice(undefined), setSession({ token, me }))} /> return } function Login({ notice, onReady }: { notice?: string; onReady: (token: string, me: Card) => void }) { const [step, setStep] = useState<{ token: string; factorId: string }>() const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [code, setCode] = useState('') const [busy, setBusy] = useState(false) const [error, setError] = useState() const codeRef = useRef(null) const submit = async (e: FormEvent) => { e.preventDefault() if (busy) return setBusy(true) setError(undefined) try { if (!step) { const r = await signIn(email, password) setPassword('') if (!r.factorId) { await signOut(r.token) throw new AuthError("Ce compte n'a pas la double authentification. Active-la d'abord dans l'app (Réglages → Mon compte), elle est obligatoire ici.") } setStep({ token: r.token, factorId: r.factorId }) setTimeout(() => codeRef.current?.focus(), 0) } else { const token = await verifyCode(step.token, step.factorId, code.replace(/\s/g, '')) const me = await call(token, 'hello') onReady(token, me) } } catch (err) { setError(err instanceof AuthError || err instanceof ApiError ? err.message : 'Erreur inattendue.') if (step) setCode('') } finally { setBusy(false) } } return (
Console

Bobinoo

Accès réservé. Chaque connexion est inscrite au journal et signalée par e-mail.

{notice &&

{notice}

}
{!step ? ( <> setEmail(e.target.value)} required /> setPassword(e.target.value)} required /> ) : ( <>

Code à 6 chiffres de ton application d'authentification :

setCode(e.target.value)} required /> )} {error &&

{error}

} {step && ( )}
) } function Console({ token, me, onLogout }: { token: string; me: Card; onLogout: (why?: string) => void }) { const [tab, setTab] = useState('dashboard') // Erreur de session renvoyée par le serveur : retour à la connexion const onAuthError = useCallback( (e: unknown) => { if (e instanceof ApiError && (e.status === 401 || e.status === 403)) onLogout(e.message) }, [onLogout], ) const props = { token, onAuthError } return (
Console Bobinoo @{me.username}
{tab === 'dashboard' && } {tab === 'members' && } {tab === 'reviews' && } {tab === 'comments' && } {tab === 'journal' && }
) }