41 lines
2.8 KiB
TypeScript
41 lines
2.8 KiB
TypeScript
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<typeof import("./trend-dialog.ts")> | 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`<button type="button" title=${title} aria-label=${title} aria-haspopup="dialog" ?disabled=${this.busy} @pointerdown=${(e: Event) => e.stopPropagation()} @click=${(e: Event) => void this.open(e)}>
|
|
<svg width="15" height="15" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.5" aria-hidden="true"><path d="M3 2v15h15M5 13l4-5 4 2 4-6"/></svg>
|
|
</button>${this.error ? html`<span role="alert">${this.error}</span>` : 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`<nf-trend-button .hass=${hass} .ids=${logged} .label=${label}></nf-trend-button>` : nothing;
|
|
}
|