Lmit _initializeLocalize

This commit is contained in:
Joakim Sørensen 2021-06-15 09:20:21 +00:00
parent 04a4d26b18
commit f5c42d24ed

View File

@ -1,6 +1,7 @@
import { Collection, UnsubscribeFunc } from "home-assistant-js-websocket"; import { Collection, UnsubscribeFunc } from "home-assistant-js-websocket";
import { LitElement, PropertyValues } from "lit"; import { LitElement, PropertyValues } from "lit";
import { property, state } from "lit/decorators"; import { property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { atLeastVersion } from "../../src/common/config/version"; import { atLeastVersion } from "../../src/common/config/version";
import { computeLocalize } from "../../src/common/translations/localize"; import { computeLocalize } from "../../src/common/translations/localize";
import { fetchHassioAddonsInfo } from "../../src/data/hassio/addon"; import { fetchHassioAddonsInfo } from "../../src/data/hassio/addon";
@ -36,6 +37,11 @@ declare global {
} }
} }
const isIngress = memoizeOne(
(panel?: HassioPanelInfo, route?: Route) =>
(panel?.config && "ingress" in panel.config) || route?.path === "/ingress"
);
export class SupervisorBaseElement extends urlSyncMixin( export class SupervisorBaseElement extends urlSyncMixin(
ProvideHassLitMixin(LitElement) ProvideHassLitMixin(LitElement)
) { ) {
@ -43,7 +49,7 @@ export class SupervisorBaseElement extends urlSyncMixin(
localize: () => "", localize: () => "",
}; };
@property({ attribute: false }) public panel!: HassioPanelInfo; @property({ attribute: false }) public panel?: HassioPanelInfo;
@property({ attribute: false }) public route?: Route; @property({ attribute: false }) public route?: Route;
@ -53,13 +59,13 @@ export class SupervisorBaseElement extends urlSyncMixin(
@state() private _language = "en"; @state() private _language = "en";
public connectedCallback(): void { @state() private _ingress = true;
super.connectedCallback();
this._initializeLocalize();
}
public disconnectedCallback() { public disconnectedCallback() {
super.disconnectedCallback(); super.disconnectedCallback();
if (this._ingress) {
return;
}
Object.keys(this._unsubs).forEach((unsub) => { Object.keys(this._unsubs).forEach((unsub) => {
this._unsubs[unsub](); this._unsubs[unsub]();
}); });
@ -67,6 +73,11 @@ export class SupervisorBaseElement extends urlSyncMixin(
protected updated(changedProperties: PropertyValues) { protected updated(changedProperties: PropertyValues) {
super.updated(changedProperties); super.updated(changedProperties);
this._ingress = isIngress(this.panel, this.route);
if (this._ingress) {
return;
}
if (changedProperties.has("hass")) { if (changedProperties.has("hass")) {
const oldHass = changedProperties.get("hass") as const oldHass = changedProperties.get("hass") as
| HomeAssistant | HomeAssistant
@ -108,22 +119,27 @@ export class SupervisorBaseElement extends urlSyncMixin(
protected firstUpdated(changedProps: PropertyValues): void { protected firstUpdated(changedProps: PropertyValues): void {
super.firstUpdated(changedProps); super.firstUpdated(changedProps);
this._ingress = isIngress(this.panel, this.route);
if (this._ingress) {
return;
}
if ( if (
this._language !== this.hass.language && this._language !== this.hass.language &&
this.hass.language !== undefined this.hass.language !== undefined
) { ) {
this._language = this.hass.language; this._language = this.hass.language;
} }
this._initializeLocalize(); this._initializeLocalize();
if (
!(this.panel.config && "ingress" in this.panel.config) &&
this.route?.path !== "/ingress"
) {
this._initSupervisor(); this._initSupervisor();
} }
}
private async _initializeLocalize() { private async _initializeLocalize() {
if (this._ingress) {
return;
}
const { language, data } = await getTranslation( const { language, data } = await getTranslation(
null, null,
this._language, this._language,