Files
NextFloor/frontend/src/symcon-host.ts
T

52 lines
3.3 KiB
TypeScript

// Entry point served by the IP-Symcon WebHook. It mounts the existing panel in the console designer and
// the existing dashboard card inside the HTML-SDK tile iframe.
import "./main.ts";
import { SymconBridge } from "./symcon-bridge.ts";
import type { HomeAssistant } from "./types.ts";
import { SymconAuthError } from "./symcon-api.ts";
type SymconPanel = HTMLElement & { hass: HomeAssistant; narrow: boolean };
type SymconCard = HTMLElement & { hass: HomeAssistant; setConfig(config: Record<string, unknown>): void };
function pageMode(): "designer" | "tile" {
return new URLSearchParams(window.location.search).get("mode") === "tile" ? "tile" : "designer";
}
async function mount(): Promise<void> {
// Old admin drafts must not survive a switch to a more restricted account in the same browser.
try { localStorage.removeItem("nextfloor.unsaved"); } catch { /* storage may be disabled */ }
const bridge = new SymconBridge();
const settings = window.__NEXTFLOOR_SYMCON__!;
bridge.onExpired = () => { document.body.replaceChildren(); window.location.replace(`${settings.endpoint}/login?mode=${pageMode()}`); };
const hass = await bridge.start();
const tile = pageMode() === "tile";
const app = (tile ? document.createElement("nextfloor-card") : document.createElement("nextfloor-panel")) as SymconCard & SymconPanel;
app.className = tile ? "nf-symcon-tile" : "nf-symcon-designer";
app.hass = hass;
if (tile) {
(app as SymconCard).setConfig({ type: "custom:nextfloor-card", fill: true, room_buttons: true, controls: true, weather: false, announcements: false });
} else {
(app as SymconPanel).narrow = window.innerWidth < 870;
}
const resize = () => { if (!tile) (app as SymconPanel).narrow = window.innerWidth < 870; };
window.addEventListener("resize", resize);
const bar = document.createElement("nav"); bar.className = "nf-session-bar"; bar.setAttribute("aria-label", "Benutzerkonto");
const identity = document.createElement("span"); identity.textContent = `${hass.user?.name ?? ""} · ${hass.user?.is_admin ? "Administrator" : settings.user?.can_control === false ? "Nur ansehen" : settings.user?.control_all ? "Bedienung" : settings.user?.view_all ? "Ansicht und freigegebene Bedienung" : "Freigegebene Räume / Geräte"}`;
const form = document.createElement("form"); form.method = "post"; form.action = `${settings.endpoint}/logout?mode=${pageMode()}`;
const csrf = document.createElement("input"); csrf.type = "hidden"; csrf.name = "csrf"; csrf.value = settings.csrf || "";
const logout = document.createElement("button"); logout.type = "submit"; logout.textContent = "Abmelden";
form.append(csrf, logout); bar.append(identity, form);
document.body.replaceChildren(bar, app);
bridge.onChange(() => {
app.hass = bridge.snapshot();
});
window.addEventListener("pagehide", () => { bridge.stop(); window.removeEventListener("resize", resize); document.body.replaceChildren(); }, { once: true });
window.addEventListener("pageshow", (event) => { if (event.persisted) location.reload(); });
}
void mount().catch((error: unknown) => {
if (error instanceof SymconAuthError) { window.location.replace(`${window.__NEXTFLOOR_SYMCON__!.endpoint}/login?mode=${pageMode()}`); return; }
document.body.textContent = `NextFloor konnte nicht geladen werden: ${error instanceof Error ? error.message : String(error)}`;
});