import { useEffect, useState } from 'react' import Creator from './Creator.jsx' import Rules from './Rules.jsx' import Maps from './Maps.jsx' import Characters from './Characters.jsx' import Presets from './Presets.jsx' import Lobbies from './Lobbies.jsx' import Login from './Login.jsx' import Account from './Account.jsx' import Editor from './Editor.jsx' import { claimLegacy } from './legacy.js' const useHash = () => { const [h, setH] = useState(window.location.hash.slice(1) || 'creator') useEffect(() => { const f = () => setH(window.location.hash.slice(1) || 'creator') window.addEventListener('hashchange', f) return () => window.removeEventListener('hashchange', f) }, []) return h } export default function App() { const hash = useHash() const [auth, setAuth] = useState(null) // null = checking, {user, registration_open} const [rules, setRules] = useState(null) const [error, setError] = useState('') // Used to load a saved character into the creator const [loadRequest, setLoadRequest] = useState(null) const [charName, setCharName] = useState('') // name of the character being worked on, shown in the nav // Ask the server who we are; any later 401 (session expired) sends us back to the login useEffect(() => { fetch('/api/auth/me').then((r) => r.json()).then(setAuth).catch(() => setError('Could not reach the server.')) const out = () => { setAuth((a) => ({ user: null, registration_open: a?.registration_open })); setRules(null) } window.addEventListener('aw-unauthorized', out) return () => window.removeEventListener('aw-unauthorized', out) }, []) const username = auth?.user?.username const loadRules = () => fetch('/api/rules').then((r) => r.json()).then(setRules).catch(() => setError('Could not load the rules.')) useEffect(() => { if (!username) return claimLegacy().finally(loadRules) }, [username]) // eslint-disable-line const logout = async () => { await fetch('/api/auth/logout', { method: 'POST' }).catch(() => {}) window.location.hash = 'creator' setAuth({ user: null, registration_open: auth.registration_open }); setRules(null) } if (error) return

{error}

if (!auth) return

Loading …

if (!auth.user) return
setAuth({ ...auth, user: u })} />
const page = hash.split('/')[0] const nav = [['creator', charName ? `Character: ${charName}` : 'Character'], ['rules', 'Rules'], ['presets', 'Presets'], ['maps', 'Maps'], ['characters', 'My characters'], ['lobbies', 'Lobbies'], ...(auth.user.admin ? [['editor', 'Editor']] : [])] return ( <>
{!rules &&

Loading rules …

} {rules && page === 'creator' && setLoadRequest(null)} onName={setCharName} />} {rules && page === 'rules' && } {rules && page === 'presets' && } {page === 'maps' && } {rules && page === 'characters' && ( { setLoadRequest(entry); window.location.hash = 'creator' }} onNew={() => { setLoadRequest({ new: true }); window.location.hash = 'creator' }} /> )} {rules && page === 'lobbies' && ( { setLoadRequest(entry); window.location.hash = 'creator' }} /> )} {rules && page === 'editor' && auth.user.admin && } {page === 'account' && }
) }