diff --git a/web/src/App.tsx b/web/src/App.tsx index a3293df..a5980e1 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -29,16 +29,11 @@ function App() { window.client = client const connState = useEventAsState(client.rpc.connect) const clientState = useEventAsState(client.state) - useEffect(() => { - Notification.requestPermission() - .then(permission => console.log("Notification permission:", permission)) - client.rpc.start() - return () => client.rpc.stop() - }, [client]) + useEffect(() => client.start(), [client]) if (connState?.error) { return
- error {`${connState.error}`} :( + {`${connState.error} \u{1F63F}`}
} else if (!connState?.connected || !clientState) { const msg = connState?.connected ? diff --git a/web/src/api/client.ts b/web/src/api/client.ts index 4072027..88138a2 100644 --- a/web/src/api/client.ts +++ b/web/src/api/client.ts @@ -38,6 +38,41 @@ export default class Client { queueMicrotask(() => this.#handleEmoteRoomsChange())) } + async #reallyStart(signal: AbortSignal) { + try { + const resp = await fetch("_gomuks/auth", { + method: "POST", + signal, + }) + if (!resp.ok) { + this.rpc.connect.emit({ + connected: false, + error: new Error(`Authentication failed: ${resp.statusText}`), + }) + return + } + } catch (err) { + const error = err instanceof Error ? err : new Error(`${err}`) + this.rpc.connect.emit({ connected: false, error }) + } + if (signal.aborted) { + return + } + console.log("Successfully authenticated, connecting to websocket") + this.rpc.start() + Notification.requestPermission() + .then(permission => console.log("Notification permission:", permission)) + } + + start(): () => void { + const abort = new AbortController() + this.#reallyStart(abort.signal) + return () => { + abort.abort() + this.rpc.stop() + } + } + get userID(): UserID { return this.state.current?.is_logged_in ? this.state.current.user_id : "" } diff --git a/web/src/main.tsx b/web/src/main.tsx index 13951f1..173a9d9 100644 --- a/web/src/main.tsx +++ b/web/src/main.tsx @@ -18,16 +18,8 @@ import { createRoot } from "react-dom/client" import App from "./App.tsx" import "./index.css" -fetch("_gomuks/auth", { method: "POST" }).then(resp => { - if (resp.ok) { - createRoot(document.getElementById("root")!).render( - - - , - ) - } else { - window.alert("Authentication failed: " + resp.statusText) - } -}, err => { - window.alert("Authentication failed: " + err) -}) +createRoot(document.getElementById("root")!).render( + + + , +)