164 lines
5.1 KiB
TypeScript
164 lines
5.1 KiB
TypeScript
// Literally everything (pls help me my autocorrect is trying to insult me how does this even work)
|
|
import {
|
|
api,
|
|
clearTabToken,
|
|
getSettings,
|
|
saveSettings,
|
|
setServerJoinOk,
|
|
} from "./api.js";
|
|
import { navigate, registerScreenRenderers } from "./router.js";
|
|
import {
|
|
loadCatalogs,
|
|
refreshMe,
|
|
refreshWorld,
|
|
startEvents,
|
|
} from "./session.js";
|
|
import {
|
|
applyThemeFromState,
|
|
type DebriefScreenParams,
|
|
getCurrentScreen,
|
|
type LaunchScreenParams,
|
|
state,
|
|
type WikiScreenParams,
|
|
} from "./state.js";
|
|
import { normalizeJoinCode } from "../shared/coop.js";
|
|
import { applyVisualPreferences } from "./theme.js";
|
|
import { initNotificationDrawer, initTooltipController, toast } from "./ui.js";
|
|
import { renderAuth } from "./screens/auth.js";
|
|
import { renderContracts } from "./screens/contracts.js";
|
|
import { renderDashboard } from "./screens/dashboard.js";
|
|
import { renderDebrief } from "./screens/debrief.js";
|
|
import { renderLaunchSequence } from "./screens/launchSequence.js";
|
|
import { renderMainMenu } from "./screens/mainMenu.js";
|
|
import { renderMissionConfig } from "./screens/missionConfig.js";
|
|
import { renderOnboarding } from "./screens/onboarding.js";
|
|
import { renderRocketBuilder } from "./screens/rocketBuilder.js";
|
|
import { renderWiki } from "./screens/wiki.js";
|
|
import { renderWorldMap } from "./screens/worldMap.js";
|
|
import { initWalkthroughController } from "./wiki/walkthrough.js";
|
|
import type { WikiArticleId } from "./wiki/wikiData.js";
|
|
import { openWikiDrawer } from "./wiki/wikiModal.js";
|
|
|
|
registerScreenRenderers({
|
|
mainMenu: (el) => renderMainMenu(el),
|
|
auth: (el) => renderAuth(el),
|
|
onboarding: (el) => renderOnboarding(el),
|
|
dashboard: (el) => renderDashboard(el),
|
|
builder: (el) => renderRocketBuilder(el),
|
|
config: (el) => renderMissionConfig(el),
|
|
launch: (el, p) => renderLaunchSequence(el, p as LaunchScreenParams),
|
|
debrief: (el, p) => renderDebrief(el, p as DebriefScreenParams),
|
|
contracts: (el) => renderContracts(el),
|
|
worldMap: (el) => renderWorldMap(el),
|
|
wiki: (el, p) => renderWiki(el, p as WikiScreenParams),
|
|
});
|
|
|
|
async function waitForStylesAndLoad(): Promise<void> {
|
|
const pendingLinks = Array.from(
|
|
document.querySelectorAll('link[rel="stylesheet"]'),
|
|
) as HTMLLinkElement[];
|
|
const cssPromises = pendingLinks
|
|
.filter((link) => !link.sheet)
|
|
.map(
|
|
(link) =>
|
|
new Promise<void>((resolve) => {
|
|
link.addEventListener("load", () => resolve(), { once: true });
|
|
link.addEventListener("error", () => resolve(), { once: true });
|
|
}),
|
|
);
|
|
const loadPromise =
|
|
document.readyState === "complete"
|
|
? Promise.resolve()
|
|
: new Promise<void>((resolve) => {
|
|
window.addEventListener("load", () => resolve(), { once: true });
|
|
});
|
|
await Promise.race([
|
|
Promise.all([loadPromise, ...cssPromises]).then(() => undefined),
|
|
new Promise<void>((resolve) => window.setTimeout(resolve, 1500)),
|
|
]);
|
|
}
|
|
|
|
async function boot(): Promise<void> {
|
|
saveSettings(getSettings());
|
|
applyThemeFromState();
|
|
applyVisualPreferences();
|
|
initNotificationDrawer();
|
|
initTooltipController();
|
|
initWalkthroughController();
|
|
window.addEventListener("sgywftp:open-wiki", (ev) => {
|
|
const detail =
|
|
(
|
|
ev as CustomEvent<{
|
|
articleId?: WikiArticleId;
|
|
glossaryKey?: string;
|
|
query?: string;
|
|
preferDrawer?: boolean;
|
|
}>
|
|
).detail ?? {};
|
|
const cur = getCurrentScreen();
|
|
const preserveScreens = new Set([
|
|
"builder",
|
|
"config",
|
|
"launch",
|
|
"debrief",
|
|
"contracts",
|
|
"worldMap",
|
|
]);
|
|
if (detail.preferDrawer || preserveScreens.has(cur)) {
|
|
openWikiDrawer({
|
|
articleId: detail.articleId,
|
|
glossaryKey: detail.glossaryKey,
|
|
query: detail.query,
|
|
});
|
|
} else {
|
|
navigate("wiki", {
|
|
articleId: detail.articleId,
|
|
glossaryKey: detail.glossaryKey,
|
|
query: detail.query,
|
|
returnTo: cur === "wiki" ? "dashboard" : cur,
|
|
});
|
|
}
|
|
});
|
|
const readyPromise = waitForStylesAndLoad();
|
|
try {
|
|
await loadCatalogs();
|
|
} catch (err) {
|
|
toast(`Failed to load mission catalogs: ${(err as Error).message}`, "err");
|
|
}
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
const passParam = urlParams.get("pass");
|
|
const joinParam = urlParams.get("join");
|
|
if (passParam || joinParam) {
|
|
const url = new URL(window.location.href);
|
|
url.searchParams.delete("pass");
|
|
url.searchParams.delete("join");
|
|
window.history.replaceState(null, "", url.toString());
|
|
}
|
|
if (passParam) {
|
|
if (state.serverInfo?.serverPasswordRequired) {
|
|
try {
|
|
await api.post("/api/join", { password: passParam }, false);
|
|
setServerJoinOk(true);
|
|
toast("Server access granted via link.", "ok");
|
|
} catch {
|
|
toast("Server password in link was rejected.", "err");
|
|
}
|
|
}
|
|
}
|
|
if (joinParam) {
|
|
state.pendingJoinCode = normalizeJoinCode(joinParam);
|
|
clearTabToken();
|
|
await readyPromise;
|
|
navigate("auth");
|
|
return;
|
|
}
|
|
await refreshMe();
|
|
if (state.agency) {
|
|
await refreshWorld();
|
|
startEvents();
|
|
}
|
|
await readyPromise;
|
|
navigate("mainMenu");
|
|
}
|
|
|
|
void boot();
|