1import { elements, populateSelect } from "./dom.js";2import { state } from "./state.js";3import { bindUIEvents } from "./events.js";4import { readRoute, readSession } from "./session.js";5import { selectVolume } from "./navigation.js";6import { refreshCatalog, syncEmptyState } from "./library.js";7import { exposeDiagnostics } from "./diagnostics.js";89export async function start() {10 bindUIEvents();11 try {12 const response = await fetch("/data/catalog.json");13 if (!response.ok) throw new Error(`catalog request returned ${response.status}`);14 state.catalog = await response.json();15 populateSelect(elements.volume, state.catalog.volumes, "title");16 syncEmptyState();17 // The library can change while the application is open -- a bundle18 // dropped in, replaced, or removed -- and the backend says so here.19 // Outside the desktop shell there is no event bridge, and the catalog20 // simply stays as fetched.21 window.runtime?.EventsOn?.("atlas:catalog-changed", () => {22 void refreshCatalog();23 });24 if (state.catalog.volumes.length) {25 const route = readRoute();26 const session = readSession();27 // An address naming somewhere else was typed on purpose, so it wins and28 // arrives clean; anything else resumes where the reader left off.29 const resuming = session &&30 (!route.volume || (route.volume === session.volume && route.world === session.world));31 state.restore = resuming ? session : null;32 await selectVolume(33 route.volume || session?.volume || state.catalog.volumes[0].slug,34 route.world || session?.world,35 );36 state.restore = null;37 } else {38 // An empty library is a fresh install, not a failure: the empty state39 // is already showing, and the first bundle to arrive opens on its own.40 elements.loading.hidden = true;41 }42 exposeDiagnostics();43 requestAnimationFrame(() => elements.viewport.focus({ preventScroll: true }));44 } catch (error) {45 elements.loading.hidden = true;46 elements.fatalMessage.textContent = error instanceof Error ? error.message : String(error);47 elements.fatal.hidden = false;48 }49}