Optimise data table and device dashboard (#9217)

This commit is contained in:
Bram Kragten 2021-05-25 13:12:44 +02:00 committed by GitHub
parent 0a478ee1da
commit 8af05e2726
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 22 deletions

View File

@ -173,8 +173,8 @@ export class HaDataTable extends LitElement {
this.updateComplete.then(() => this._calcTableHeight()); this.updateComplete.then(() => this._calcTableHeight());
} }
protected updated(properties: PropertyValues) { public willUpdate(properties: PropertyValues) {
super.updated(properties); super.willUpdate(properties);
if (properties.has("columns")) { if (properties.has("columns")) {
this._filterable = Object.values(this.columns).some( this._filterable = Object.values(this.columns).some(
@ -342,6 +342,10 @@ export class HaDataTable extends LitElement {
layout: Layout1d, layout: Layout1d,
// @ts-expect-error // @ts-expect-error
renderItem: (row: DataTableRowData, index) => { renderItem: (row: DataTableRowData, index) => {
// not sure how this happens...
if (!row) {
return "";
}
if (row.append) { if (row.append) {
return html` return html`
<div class="mdc-data-table__row">${row.content}</div> <div class="mdc-data-table__row">${row.content}</div>
@ -474,15 +478,16 @@ export class HaDataTable extends LitElement {
} }
if (this.appendRow || this.hasFab) { if (this.appendRow || this.hasFab) {
this._items = [...data]; const items = [...data];
if (this.appendRow) { if (this.appendRow) {
this._items.push({ append: true, content: this.appendRow }); items.push({ append: true, content: this.appendRow });
} }
if (this.hasFab) { if (this.hasFab) {
this._items.push({ empty: true }); items.push({ empty: true });
} }
this._items = items;
} else { } else {
this._items = data; this._items = data;
} }

View File

@ -68,6 +68,30 @@ export class HaConfigDeviceDashboard extends LitElement {
@state() private _numHiddenDevices = 0; @state() private _numHiddenDevices = 0;
private _ignoreLocationChange = false;
public constructor() {
super();
window.addEventListener("location-changed", () => {
if (this._ignoreLocationChange) {
this._ignoreLocationChange = false;
return;
}
if (
window.location.search.substring(1) !== this._searchParms.toString()
) {
this._searchParms = new URLSearchParams(window.location.search);
}
});
window.addEventListener("popstate", () => {
if (
window.location.search.substring(1) !== this._searchParms.toString()
) {
this._searchParms = new URLSearchParams(window.location.search);
}
});
}
private _activeFilters = memoizeOne( private _activeFilters = memoizeOne(
( (
entries: ConfigEntry[], entries: ConfigEntry[],
@ -78,10 +102,6 @@ export class HaConfigDeviceDashboard extends LitElement {
filters.forEach((value, key) => { filters.forEach((value, key) => {
switch (key) { switch (key) {
case "config_entry": { case "config_entry": {
// If we are requested to show the devices for a given config entry,
// also show the disabled ones by default.
this._showDisabled = true;
const configEntry = entries.find( const configEntry = entries.find(
(entry) => entry.entry_id === value (entry) => entry.entry_id === value
); );
@ -118,7 +138,6 @@ export class HaConfigDeviceDashboard extends LitElement {
) => { ) => {
// Some older installations might have devices pointing at invalid entryIDs // Some older installations might have devices pointing at invalid entryIDs
// So we guard for that. // So we guard for that.
let outputDevices: DeviceRowData[] = devices; let outputDevices: DeviceRowData[] = devices;
const deviceLookup: { [deviceId: string]: DeviceRegistryEntry } = {}; const deviceLookup: { [deviceId: string]: DeviceRegistryEntry } = {};
@ -315,14 +334,14 @@ export class HaConfigDeviceDashboard extends LitElement {
} }
); );
public constructor() { public willUpdate(changedProps) {
super(); if (changedProps.has("_searchParms")) {
window.addEventListener("location-changed", () => { if (this._searchParms.get("config_entry")) {
this._searchParms = new URLSearchParams(window.location.search); // If we are requested to show the devices for a given config entry,
}); // also show the disabled ones by default.
window.addEventListener("popstate", () => { this._showDisabled = true;
this._searchParms = new URLSearchParams(window.location.search); }
}); }
} }
protected render(): TemplateResult { protected render(): TemplateResult {
@ -435,6 +454,7 @@ export class HaConfigDeviceDashboard extends LitElement {
private _handleRowClicked(ev: HASSDomEvent<RowClickedEvent>) { private _handleRowClicked(ev: HASSDomEvent<RowClickedEvent>) {
const deviceId = ev.detail.id; const deviceId = ev.detail.id;
this._ignoreLocationChange = true;
navigate(this, `/config/devices/device/${deviceId}`); navigate(this, `/config/devices/device/${deviceId}`);
} }

View File

@ -391,10 +391,18 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) {
public constructor() { public constructor() {
super(); super();
window.addEventListener("location-changed", () => { window.addEventListener("location-changed", () => {
this._searchParms = new URLSearchParams(window.location.search); if (
window.location.search.substring(1) !== this._searchParms.toString()
) {
this._searchParms = new URLSearchParams(window.location.search);
}
}); });
window.addEventListener("popstate", () => { window.addEventListener("popstate", () => {
this._searchParms = new URLSearchParams(window.location.search); if (
window.location.search.substring(1) !== this._searchParms.toString()
) {
this._searchParms = new URLSearchParams(window.location.search);
}
}); });
} }
@ -623,8 +631,8 @@ export class HaConfigEntities extends SubscribeMixin(LitElement) {
loadEntityEditorDialog(); loadEntityEditorDialog();
} }
protected updated(changedProps): void { public willUpdate(changedProps): void {
super.updated(changedProps); super.willUpdate(changedProps);
const oldHass = changedProps.get("hass"); const oldHass = changedProps.get("hass");
let changed = false; let changed = false;
if (!this.hass || !this._entities) { if (!this.hass || !this._entities) {