Skip to content

Commit

Permalink
Simplify data table template (#17825)
Browse files Browse the repository at this point in the history
* Simplify data table template

* Fix backup and gallery
  • Loading branch information
piitaya authored Sep 20, 2023
1 parent 5e107d4 commit 3349031
Show file tree
Hide file tree
Showing 23 changed files with 597 additions and 550 deletions.
8 changes: 4 additions & 4 deletions gallery/src/pages/misc/entity-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export class DemoEntityState extends LitElement {
const columns: DataTableColumnContainer<EntityRowData> = {
icon: {
title: "Icon",
template: (_, entry) => html`
template: (entry) => html`
<state-badge
.stateObj=${entry.stateObj}
.stateColor=${true}
Expand All @@ -360,7 +360,7 @@ export class DemoEntityState extends LitElement {
title: "State",
width: "20%",
sortable: true,
template: (_, entry) =>
template: (entry) =>
html`${computeStateDisplay(
hass.localize,
entry.stateObj,
Expand All @@ -371,14 +371,14 @@ export class DemoEntityState extends LitElement {
},
device_class: {
title: "Device class",
template: (dc) => html`${dc ?? "-"}`,
template: (entry) => html`${entry.device_class ?? "-"}`,
width: "20%",
filterable: true,
sortable: true,
},
domain: {
title: "Domain",
template: (_, entry) => html`${computeDomain(entry.entity_id)}`,
template: (entry) => html`${computeDomain(entry.entity_id)}`,
width: "20%",
filterable: true,
sortable: true,
Expand Down
22 changes: 13 additions & 9 deletions hassio/src/backups/hassio-backups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ import { showHassioCreateBackupDialog } from "../dialogs/backup/show-dialog-hass
import { supervisorTabs } from "../hassio-tabs";
import { hassioStyle } from "../resources/hassio-style";

type BackupItem = HassioBackup & {
secondary: string;
};

@customElement("hassio-backups")
export class HassioBackups extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
Expand Down Expand Up @@ -117,15 +121,15 @@ export class HassioBackups extends LitElement {
}

private _columns = memoizeOne(
(narrow: boolean): DataTableColumnContainer => ({
(narrow: boolean): DataTableColumnContainer<BackupItem> => ({
name: {
title: this.supervisor.localize("backup.name"),
main: true,
sortable: true,
filterable: true,
grows: true,
template: (entry: string, backup: any) =>
html`${entry || backup.slug}
template: (backup) =>
html`${backup.name || backup.slug}
<div class="secondary">${backup.secondary}</div>`,
},
size: {
Expand All @@ -134,16 +138,16 @@ export class HassioBackups extends LitElement {
hidden: narrow,
filterable: true,
sortable: true,
template: (entry: number) => Math.ceil(entry * 10) / 10 + " MB",
template: (backup) => Math.ceil(backup.size * 10) / 10 + " MB",
},
location: {
title: this.supervisor.localize("backup.location"),
width: "15%",
hidden: narrow,
filterable: true,
sortable: true,
template: (entry: string | null) =>
entry || this.supervisor.localize("backup.data_disk"),
template: (backup) =>
backup.location || this.supervisor.localize("backup.data_disk"),
},
date: {
title: this.supervisor.localize("backup.created"),
Expand All @@ -152,8 +156,8 @@ export class HassioBackups extends LitElement {
hidden: narrow,
filterable: true,
sortable: true,
template: (entry: string) =>
relativeTime(new Date(entry), this.hass.locale),
template: (backup) =>
relativeTime(new Date(backup.date), this.hass.locale),
},
secondary: {
title: "",
Expand All @@ -163,7 +167,7 @@ export class HassioBackups extends LitElement {
})
);

private _backupData = memoizeOne((backups: HassioBackup[]) =>
private _backupData = memoizeOne((backups: HassioBackup[]): BackupItem[] =>
backups.map((backup) => ({
...backup,
secondary: this._computeBackupContent(backup),
Expand Down
4 changes: 2 additions & 2 deletions src/components/data-table/ha-data-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface DataTableColumnData<T = any> extends DataTableSortColumnData {
title: TemplateResult | string;
label?: TemplateResult | string;
type?: "numeric" | "icon" | "icon-button" | "overflow-menu" | "flex";
template?: (data: any, row: T) => TemplateResult | string | typeof nothing;
template?: (row: T) => TemplateResult | string | typeof nothing;
width?: string;
maxWidth?: string;
grows?: boolean;
Expand Down Expand Up @@ -431,7 +431,7 @@ export class HaDataTable extends LitElement {
})
: ""}
>
${column.template ? column.template(row[key], row) : row[key]}
${column.template ? column.template(row) : row[key]}
</div>
`;
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,16 @@ export class HaConfigApplicationCredentials extends LitElement {
),
direction: "asc",
grows: true,
template: (_, entry: ApplicationCredential) => html`${entry.name}`,
template: (entry) => html`${entry.name}`,
},
clientId: {
client_id: {
title: localize(
"ui.panel.config.application_credentials.picker.headers.client_id"
),
width: "30%",
direction: "asc",
hidden: narrow,
template: (_, entry: ApplicationCredential) =>
html`${entry.client_id}`,
template: (entry) => html`${entry.client_id}`,
},
application: {
title: localize(
Expand All @@ -81,7 +80,7 @@ export class HaConfigApplicationCredentials extends LitElement {
sortable: true,
width: "30%",
direction: "asc",
template: (_, entry) => html`${domainToName(localize, entry.domain)}`,
template: (entry) => html`${domainToName(localize, entry.domain)}`,
},
};

Expand Down
45 changes: 24 additions & 21 deletions src/panels/config/automation/ha-automation-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ import { findRelated } from "../../../data/search";
import { fetchBlueprints } from "../../../data/blueprint";
import { UNAVAILABLE } from "../../../data/entity";

type AutomationItem = AutomationEntity & {
name: string;
last_triggered?: string | undefined;
disabled: boolean;
};

@customElement("ha-automation-picker")
class HaAutomationPicker extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
Expand All @@ -79,7 +85,7 @@ class HaAutomationPicker extends LitElement {
(
automations: AutomationEntity[],
filteredAutomations?: string[] | null
) => {
): AutomationItem[] => {
if (filteredAutomations === null) {
return [];
}
Expand All @@ -100,14 +106,14 @@ class HaAutomationPicker extends LitElement {

private _columns = memoizeOne(
(narrow: boolean, _locale): DataTableColumnContainer => {
const columns: DataTableColumnContainer = {
const columns: DataTableColumnContainer<AutomationItem> = {
icon: {
title: "",
label: this.hass.localize(
"ui.panel.config.automation.picker.headers.state"
),
type: "icon",
template: (_, automation) =>
template: (automation) =>
html`<ha-state-icon
.state=${automation}
style=${styleMap({
Expand All @@ -128,12 +134,12 @@ class HaAutomationPicker extends LitElement {
direction: "asc",
grows: true,
template: narrow
? (name, automation: any) => {
? (automation) => {
const date = new Date(automation.attributes.last_triggered);
const now = new Date();
const dayDifference = differenceInDays(now, date);
return html`
${name}
${automation.name}
<div class="secondary">
${this.hass.localize("ui.card.automation.last_triggered")}:
${automation.attributes.last_triggered
Expand All @@ -156,20 +162,17 @@ class HaAutomationPicker extends LitElement {
sortable: true,
width: "20%",
title: this.hass.localize("ui.card.automation.last_triggered"),
template: (last_triggered) => {
const date = new Date(last_triggered);
template: (automation) => {
if (!automation.last_triggered) {
return this.hass.localize("ui.components.relative_time.never");
}
const date = new Date(automation.last_triggered);
const now = new Date();
const dayDifference = differenceInDays(now, date);
return html`
${last_triggered
? dayDifference > 3
? formatShortDateTime(
date,
this.hass.locale,
this.hass.config
)
: relativeTime(date, this.hass.locale)
: this.hass.localize("ui.components.relative_time.never")}
${dayDifference > 3
? formatShortDateTime(date, this.hass.locale, this.hass.config)
: relativeTime(date, this.hass.locale)}
`;
},
};
Expand All @@ -178,8 +181,8 @@ class HaAutomationPicker extends LitElement {
columns.disabled = this.narrow
? {
title: "",
template: (disabled: boolean) =>
disabled
template: (automation) =>
automation.disabled
? html`
<simple-tooltip animation-delay="0" position="left">
${this.hass.localize(
Expand All @@ -196,8 +199,8 @@ class HaAutomationPicker extends LitElement {
: {
width: "20%",
title: "",
template: (disabled: boolean) =>
disabled
template: (automation) =>
automation.disabled
? html`
<ha-chip>
${this.hass.localize(
Expand All @@ -212,7 +215,7 @@ class HaAutomationPicker extends LitElement {
title: "",
width: this.narrow ? undefined : "10%",
type: "overflow-menu",
template: (_: string, automation: any) => html`
template: (automation) => html`
<ha-icon-overflow-menu
.hass=${this.hass}
narrow
Expand Down
20 changes: 10 additions & 10 deletions src/panels/config/backup/ha-config-backup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { mdiDelete, mdiDownload, mdiPlus } from "@mdi/js";
import "@lrnwebcomponents/simple-tooltip/simple-tooltip";
import { mdiDelete, mdiDownload, mdiPlus } from "@mdi/js";
import {
css,
CSSResultGroup,
html,
LitElement,
PropertyValues,
TemplateResult,
css,
html,
} from "lit";
import { customElement, property, state } from "lit/decorators";
import memoize from "memoize-one";
Expand Down Expand Up @@ -48,15 +48,15 @@ class HaConfigBackup extends LitElement {
@state() private _backupData?: BackupData;

private _columns = memoize(
(narrow, _language): DataTableColumnContainer => ({
(narrow, _language): DataTableColumnContainer<BackupContent> => ({
name: {
title: this.hass.localize("ui.panel.config.backup.name"),
main: true,
sortable: true,
filterable: true,
grows: true,
template: (entry: string, backup: BackupContent) =>
html`${entry}
template: (backup) =>
html`${backup.name}
<div class="secondary">${backup.path}</div>`,
},
size: {
Expand All @@ -65,7 +65,7 @@ class HaConfigBackup extends LitElement {
hidden: narrow,
filterable: true,
sortable: true,
template: (entry: number) => Math.ceil(entry * 10) / 10 + " MB",
template: (backup) => Math.ceil(backup.size * 10) / 10 + " MB",
},
date: {
title: this.hass.localize("ui.panel.config.backup.created"),
Expand All @@ -74,15 +74,15 @@ class HaConfigBackup extends LitElement {
hidden: narrow,
filterable: true,
sortable: true,
template: (entry: string) =>
relativeTime(new Date(entry), this.hass.locale),
template: (backup) =>
relativeTime(new Date(backup.date), this.hass.locale),
},

actions: {
title: "",
width: "15%",
type: "overflow-menu",
template: (_: string, backup: BackupContent) =>
template: (backup) =>
html`<ha-icon-overflow-menu
.hass=${this.hass}
.narrow=${this.narrow}
Expand Down
Loading

0 comments on commit 3349031

Please sign in to comment.