import type { CSSResultGroup, TemplateResult } from "lit"; import { css, html, LitElement, nothing } from "lit"; import { customElement, property } from "lit/decorators"; import { fireEvent } from "../common/dom/fire_event"; import type { LocalizeFunc } from "../common/translations/localize"; import type { Analytics, AnalyticsPreferences } from "../data/analytics"; import { haStyle } from "../resources/styles"; import "./ha-settings-row"; import "./ha-switch"; import "./ha-tooltip"; import type { HaSwitch } from "./ha-switch"; const ADDITIONAL_PREFERENCES = ["usage", "statistics"] as const; declare global { interface HASSDomEvents { "analytics-preferences-changed": { preferences: AnalyticsPreferences }; } } @customElement("ha-analytics") export class HaAnalytics extends LitElement { @property({ attribute: false }) public localize!: LocalizeFunc; @property({ attribute: false }) public analytics?: Analytics; @property({ attribute: "translation_key_panel" }) public translationKeyPanel: | "page-onboarding" | "config" = "config"; protected render(): TemplateResult { const loading = this.analytics === undefined; const baseEnabled = !loading && this.analytics!.preferences.base; return html` ${this.localize( `ui.panel.${this.translationKeyPanel}.analytics.preferences.base.title` )} ${this.localize( `ui.panel.${this.translationKeyPanel}.analytics.preferences.base.description` )} ${ADDITIONAL_PREFERENCES.map( (preference) => html` ${this.localize( `ui.panel.${this.translationKeyPanel}.analytics.preferences.${preference}.title` )} ${this.localize( `ui.panel.${this.translationKeyPanel}.analytics.preferences.${preference}.description` )} ${baseEnabled ? nothing : html` ${this.localize( `ui.panel.${this.translationKeyPanel}.analytics.need_base_enabled` )} `} ` )} ${this.localize( `ui.panel.${this.translationKeyPanel}.analytics.preferences.diagnostics.title` )} ${this.localize( `ui.panel.${this.translationKeyPanel}.analytics.preferences.diagnostics.description` )} `; } protected updated(changedProps) { super.updated(changedProps); this.shadowRoot!.querySelectorAll("*[data-for]").forEach((el) => { const forEl = (el as HTMLElement).dataset.for; delete (el as HTMLElement).dataset.for; el.addEventListener("click", () => { const toFocus = this.shadowRoot!.querySelector( `*[name=${forEl}]` ) as HTMLElement | null; if (toFocus) { toFocus.focus(); toFocus.click(); } }); }); } private _handleRowClick(ev: Event) { const target = ev.currentTarget as HaSwitch; const preference = (target as any).preference; const preferences = this.analytics ? { ...this.analytics.preferences } : {}; if (preferences[preference] === target.checked) { return; } preferences[preference] = target.checked; if ( ADDITIONAL_PREFERENCES.some((entry) => entry === preference) && target.checked ) { preferences.base = true; } else if (preference === "base" && !target.checked) { preferences.usage = false; preferences.statistics = false; } fireEvent(this, "analytics-preferences-changed", { preferences }); } static get styles(): CSSResultGroup { return [ haStyle, css` .error { color: var(--error-color); } ha-settings-row { padding: 0; } span[slot="heading"], span[slot="description"] { cursor: pointer; } `, ]; } } declare global { interface HTMLElementTagNameMap { "ha-analytics": HaAnalytics; } }