import { css, html, LitElement, nothing } from "lit"; import type { HomeAssistant } from "../types.ts"; import { loggedEntities } from "../trend-data.ts"; declare const __NF_TREND_HASH__: string; let loaded: Promise | undefined; /** Small shared trigger; the chart and its controls load only after the first click. */ class NfTrendButton extends LitElement { static properties = { hass: { attribute: false }, ids: { attribute: false }, label: { attribute: false }, busy: { state: true }, error: { state: true } }; declare hass: HomeAssistant; declare ids: string[]; declare label: string; private declare busy: boolean; private declare error: string; constructor() { super(); this.ids = []; this.label = ""; this.busy = false; this.error = ""; } private async open(event: Event) { event.stopPropagation(); this.busy = true; this.error = ""; try { const url = new URL(`./nextfloor-trend.js?v=${__NF_TREND_HASH__}`, import.meta.url).href; const mod = await (loaded ??= import(/* @vite-ignore */ url).catch((error) => { loaded = undefined; throw error; })); await mod.openTrend(this.hass, this.ids, this.label); } catch (error) { this.error = error instanceof Error ? error.message : String(error); } finally { this.busy = false; } } render() { if (!loggedEntities(this.hass, this.ids).length) return nothing; const title = this.label ? `Trend anzeigen: ${this.label}` : "Trend anzeigen"; return html`${this.error ? html`${this.error}` : nothing}`; } static styles = css`:host{display:inline-flex;vertical-align:middle;margin-left:4px;font-weight:normal}button{display:inline-flex;align-items:center;justify-content:center;width:25px;height:25px;padding:3px;border:1px solid currentColor;border-radius:6px;background:transparent;color:var(--nf-accent,#40c8ff);opacity:.8;cursor:pointer}button:hover,button:focus-visible{opacity:1;background:#40c8ff18}button:disabled{opacity:.4}span{font:12px system-ui;color:#ff9b9b;max-width:180px}`; } if (!customElements.get("nf-trend-button")) customElements.define("nf-trend-button", NfTrendButton); export function trendButton(hass: HomeAssistant | undefined, ids: readonly string[], label = "") { const logged = loggedEntities(hass, ids); return logged.length ? html`` : nothing; }