Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Social Engine & Remote Navigator8 * File : apps/navigator-web/src/App.tsx9 * Module : navigator-web10 * Repository : https://git.spboucher.ai/maclustr-social11 * Project Website : https://social.spboucher.ai12 * Created : 2026-12-0113 * Last Modified : 2026-12-0114 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Shell: preferences, tiny path router (no library), authentication gate.43import { useCallback, useEffect, useMemo, useState } from 'react';44import { goToOtherApp, loadMe, logout, type Me } from '../../shared/api.ts';45import { LangCtx, makeT, type Lang } from './i18n.ts';46import { IcoTheme } from './icons.tsx';47import { saveLang, saveTheme, storedLang, storedTheme, systemTheme, type Theme } from './prefs.ts';48import { Handoff, Login, Setup } from './screens/Auth.tsx';49import { EmergencyBanner, EmergencyControl } from './screens/Emergency.tsx';50import { Sessions } from './screens/Sessions.tsx';51import { ErrorLine, usePoll } from './ui.tsx';52import { Viewer } from './viewer/Viewer.tsx';5354const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;5556export function App() {57 const [lang, setLangState] = useState<Lang>(storedLang);58 const [theme, setThemeState] = useState<Theme>(() => storedTheme() ?? systemTheme());59 const [path, setPath] = useState(window.location.pathname);60 const [me, setMe] = useState<Me | null | undefined>(undefined);61 const [bootErr, setBootErr] = useState<unknown>(null);62 const [actionErr, setActionErr] = useState<unknown>(null);63 const t = useMemo(() => makeT(lang), [lang]);64 const langCtx = useMemo(() => ({ lang, t, setLang: (l: Lang) => { saveLang(l); setLangState(l); } }), [lang, t]);6566 useEffect(() => { document.documentElement.lang = lang; }, [lang]);67 useEffect(() => { const pop = () => setPath(window.location.pathname); window.addEventListener('popstate', pop); return () => window.removeEventListener('popstate', pop); }, []);68 const navigate = useCallback((to: string) => { window.history.pushState(null, '', to); setPath(to); window.scrollTo(0, 0); }, []);69 const refreshMe = useCallback(async () => { try { setMe(await loadMe()); setBootErr(null); } catch (e) { setBootErr(e); } }, []);70 const special = path === '/handoff' || path === '/setup';71 // Also notices an emergency stop engaged elsewhere and an expired session.72 usePoll(refreshMe, 15000, !special);73 const afterAuth = useCallback(async () => { window.history.replaceState(null, '', '/'); setPath('/'); await refreshMe(); }, [refreshMe]);7475 const toggleTheme = () => { const n: Theme = theme === 'dark' ? 'light' : 'dark'; saveTheme(n); setThemeState(n); };76 const prefs = (77 <div className="prefs">78 <button type="button" className="btn quiet" onClick={() => langCtx.setLang(lang === 'fr' ? 'en' : 'fr')} lang={lang === 'fr' ? 'en' : 'fr'} aria-label={t('prefs.lang')}>{lang === 'fr' ? 'English' : 'Français'}</button>79 <button type="button" className="btn quiet" onClick={toggleTheme} aria-label={theme === 'dark' ? t('prefs.to_light') : t('prefs.to_dark')} title={theme === 'dark' ? t('prefs.to_light') : t('prefs.to_dark')}><IcoTheme /></button>80 </div>81 );82 const run = (fn: () => Promise<void>) => () => { setActionErr(null); fn().catch(setActionErr); };8384 let body;85 if (path === '/handoff') body = <><div className="bar minimal">{prefs}</div><Handoff onDone={afterAuth} /></>;86 else if (path === '/setup') body = <><div className="bar minimal">{prefs}</div><Setup onDone={afterAuth} /></>;87 else if (me === undefined) body = <main className="auth">{bootErr ? <><ErrorLine error={bootErr} /><button type="button" className="btn" onClick={() => void refreshMe()}>{t('common.retry')}</button></> : <p role="status">{t('common.loading')}</p>}</main>;88 else if (me === null) body = <><div className="bar minimal">{prefs}</div><Login onDone={refreshMe} /></>;89 else {90 const m = /^\/s\/([^/]+)\/?$/.exec(path);91 if (m && UUID.test(m[1]!)) body = <Viewer key={m[1]} sessionId={m[1]!.toLowerCase()} me={me} navigate={navigate} refreshMe={refreshMe} />;92 else body = (93 <>94 <header className="bar">95 <a className="wordmark" href="/" onClick={(e) => { e.preventDefault(); navigate('/'); }}>MacLustr <span>Navigator</span></a>96 {me.env !== 'production' && <span className="tag">{me.env}</span>}97 <span className="spacer" />98 <span className="who">{me.user.display_name || me.user.handle} · {me.user.role}</span>99 <button type="button" className="btn quiet" onClick={run(() => goToOtherApp('social'))}>{t('bar.social')}</button>100 <button type="button" className="btn quiet" onClick={run(async () => { await logout(); await refreshMe(); })}>{t('bar.logout')}</button>101 {prefs}102 <EmergencyControl me={me} engaged={me.emergency.emergency_stop} refreshMe={refreshMe} />103 </header>104 <EmergencyBanner me={me} engaged={me.emergency.emergency_stop} />105 <div aria-live="polite"><ErrorLine error={actionErr ?? bootErr} onDismiss={() => { setActionErr(null); setBootErr(null); }} /></div>106 {path === '/' ? <Sessions me={me} emergency={me.emergency.emergency_stop} navigate={navigate} refreshMe={refreshMe} />107 : <main className="page"><h1>{t('nf.title')}</h1><p>{t('nf.lead')}</p><p><a className="btn" href="/" onClick={(e) => { e.preventDefault(); navigate('/'); }}>{t('nf.back')}</a></p></main>}108 </>109 );110 }111 return <LangCtx.Provider value={langCtx}>{body}</LangCtx.Provider>;112}113