108 lines
4.3 KiB
TypeScript
108 lines
4.3 KiB
TypeScript
// file: crates/ksp-app-solprices-desk/frontend/ts/main.ts
|
|
// version: 3
|
|
|
|
import "bootstrap";
|
|
import ResizeObserver from "resize-observer-polyfill";
|
|
import "simplebar";
|
|
import { getCurrentWindow } from "@tauri-apps/api/window";
|
|
import type { RuntimeStatusDto } from "./bindings/ksp_app_solprices_desk/dto_common/RuntimeStatusDto.ts";
|
|
import { frontendDebug, frontendInfo, frontendTrace, frontendWarn, installFrontendConsoleBridge } from "./frontend_log";
|
|
import { invokeKsp } from "./invoke";
|
|
|
|
(window as Window & typeof globalThis & { ResizeObserver?: typeof ResizeObserver }).ResizeObserver = ResizeObserver;
|
|
installFrontendConsoleBridge("main");
|
|
|
|
type ViewId = "prices" | "diagnostics";
|
|
|
|
const viewTitles: Record<ViewId, string> = {
|
|
prices: "Prices",
|
|
diagnostics: "Diagnostics",
|
|
};
|
|
|
|
function isViewId(value: string | undefined): value is ViewId {
|
|
return value === "prices" || value === "diagnostics";
|
|
}
|
|
|
|
function activateView(viewId: ViewId, source: "startup" | "user"): void {
|
|
document.querySelectorAll<HTMLElement>("[data-view-panel]").forEach(panel => {
|
|
panel.hidden = panel.dataset.viewPanel !== viewId;
|
|
});
|
|
document.querySelectorAll<HTMLButtonElement>("[data-view]").forEach(button => {
|
|
button.classList.toggle("active", button.dataset.view === viewId);
|
|
});
|
|
const header = document.querySelector<HTMLElement>("#headerViewTitle");
|
|
if (header) {
|
|
header.textContent = viewTitles[viewId];
|
|
}
|
|
document.title = `SOL Prices Desk — ${viewTitles[viewId]}`;
|
|
if (source !== "startup") {
|
|
frontendDebug("main", "SOL Prices Desk view activated", { viewId, source });
|
|
}
|
|
}
|
|
|
|
function bindNavigation(): void {
|
|
document.querySelectorAll<HTMLButtonElement>("[data-view]").forEach(button => {
|
|
button.addEventListener("click", () => {
|
|
const viewId = button.dataset.view;
|
|
frontendDebug("main", "SOL Prices Desk navigation control clicked", { viewId: viewId ?? "unknown" });
|
|
if (isViewId(viewId)) {
|
|
activateView(viewId, "user");
|
|
}
|
|
});
|
|
});
|
|
frontendTrace("main", "SOL Prices Desk navigation handlers installed");
|
|
}
|
|
|
|
function renderRuntimeStatus(status: RuntimeStatusDto): void {
|
|
const values: Record<string, string> = {
|
|
runtimeCompositeProfile: status.activeCompositeProfile,
|
|
runtimeOffchainProfile: status.activeOffchainProfile,
|
|
runtimeVersion: status.applicationVersion,
|
|
runtimeShellPhase: status.shellPhase,
|
|
runtimeConfigDocuments: status.configDocumentCount.toString(),
|
|
runtimeLoggingProfile: status.activeLoggingProfile ?? "fallback",
|
|
runtimeLoggingFallback: status.fallbackLoggingActive ? "oui" : "non",
|
|
};
|
|
for (const [elementId, value] of Object.entries(values)) {
|
|
const element = document.querySelector<HTMLElement>(`#${elementId}`);
|
|
if (element) {
|
|
element.textContent = value;
|
|
}
|
|
}
|
|
const diagnostic = document.querySelector<HTMLElement>("#runtimeDiagnostic");
|
|
if (diagnostic) {
|
|
diagnostic.hidden = status.startupDiagnostic === null;
|
|
diagnostic.textContent = status.startupDiagnostic === null ? "" : `${status.startupDiagnostic.domain}:${status.startupDiagnostic.code} — ${status.startupDiagnostic.message}`;
|
|
}
|
|
frontendTrace("main", "SOL Prices Desk scaffold runtime status rendered", {
|
|
fallbackLoggingActive: status.fallbackLoggingActive,
|
|
configDocumentCount: status.configDocumentCount,
|
|
});
|
|
}
|
|
|
|
async function loadRuntimeStatus(): Promise<void> {
|
|
const status = await invokeKsp<RuntimeStatusDto>("main", "get_runtime_status");
|
|
renderRuntimeStatus(status);
|
|
}
|
|
|
|
async function initializeMain(): Promise<void> {
|
|
const windowLabel = getCurrentWindow().label;
|
|
frontendInfo("main", "SOL Prices Desk main frontend loaded", { windowLabel });
|
|
bindNavigation();
|
|
activateView("prices", "startup");
|
|
try {
|
|
await loadRuntimeStatus();
|
|
} catch {
|
|
const diagnostic = document.querySelector<HTMLElement>("#runtimeDiagnostic");
|
|
if (diagnostic) {
|
|
diagnostic.hidden = false;
|
|
diagnostic.textContent = "Le statut runtime initial n'a pas pu être chargé.";
|
|
}
|
|
frontendWarn("main", "SOL Prices Desk startup runtime status load failed");
|
|
}
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
void initializeMain();
|
|
});
|