57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
// (Tech support with -4 hours of sleep and 6th mug of coffee) Uhm, sir, have you tried turning it off and on again?
|
|
// Oh.
|
|
// Oh...
|
|
import { type ScreenName, setCurrentScreen } from "./state.js";
|
|
import { clear, hideTooltip } from "./ui.js";
|
|
|
|
export type ScreenRenderer = (
|
|
appEl: HTMLElement,
|
|
params: Record<string, any>,
|
|
) => (() => void) | void;
|
|
|
|
const appEl = document.getElementById("app") as HTMLElement;
|
|
let activeCleanup: (() => void) | null = null;
|
|
let screenRenderers: Partial<Record<ScreenName, ScreenRenderer>> = {};
|
|
|
|
const RIGHT_SIDEBAR_OFFSET_PX: Partial<Record<ScreenName, number>> = {
|
|
dashboard: 370,
|
|
builder: 440,
|
|
config: 440,
|
|
worldMap: 340,
|
|
};
|
|
|
|
export function registerScreenRenderers(
|
|
renderers: Partial<Record<ScreenName, ScreenRenderer>>,
|
|
): void {
|
|
screenRenderers = renderers;
|
|
}
|
|
|
|
export function navigate(
|
|
screen: ScreenName,
|
|
params: Record<string, any> = {},
|
|
): void {
|
|
if (activeCleanup) {
|
|
try {
|
|
activeCleanup();
|
|
} catch (err) {
|
|
console.warn("Cleanup error:", err);
|
|
}
|
|
activeCleanup = null;
|
|
}
|
|
setCurrentScreen(screen);
|
|
const sidebarOffset = RIGHT_SIDEBAR_OFFSET_PX[screen] ?? 0;
|
|
document.documentElement.style.setProperty(
|
|
"--right-sidebar-offset",
|
|
`${sidebarOffset}px`,
|
|
);
|
|
document.documentElement.classList.toggle(
|
|
"has-right-sidebar",
|
|
sidebarOffset > 0,
|
|
);
|
|
clear(appEl);
|
|
hideTooltip();
|
|
const cleanup = screenRenderers[screen]?.(appEl, params);
|
|
if (typeof cleanup === "function") {
|
|
activeCleanup = cleanup;
|
|
}
|
|
}
|