feat: add scoped user accounts, password login and garden paving materials
This commit is contained in:
1 parent
c56f1a5ba9
commit
1d67e8ceba
43 files changed
+2024
-625
No files matched your search
@@ -4,6 +4,7 @@
|
||||
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 };
|
||||
@@ -13,7 +14,11 @@ function pageMode(): "designer" | "tile" {
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -22,15 +27,25 @@ async function mount(): Promise<void> {
|
||||
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 = false;
|
||||
(app as SymconPanel).narrow = window.innerWidth < 870;
|
||||
}
|
||||
document.body.replaceChildren(app);
|
||||
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(), { once: true });
|
||||
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)}`;
|
||||
});
|
||||
Reference in new issue
Block a user