Skip to content

Commit

Permalink
Use new model
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Feb 4, 2025
1 parent 353d723 commit 91831a4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
25 changes: 17 additions & 8 deletions src/data/backup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { memoize } from "@fullcalendar/core/internal";
import { setHours, setMinutes } from "date-fns";
import type { HassConfig } from "home-assistant-js-websocket";
import memoizeOne from "memoize-one";
import checkValidDate from "../common/datetime/check_valid_date";
import {
formatDateTime,
formatDateTimeNumeric,
Expand All @@ -11,7 +13,6 @@ import type { HomeAssistant } from "../types";
import { fileDownload } from "../util/file_download";
import { domainToName } from "./integration";
import type { FrontendLocaleData } from "./translation";
import checkValidDate from "../common/datetime/check_valid_date";

export const enum BackupScheduleRecurrence {
NEVER = "never",
Expand Down Expand Up @@ -104,6 +105,9 @@ export interface BackupContent {
name: string;
agents: Record<string, BackupContentAgent>;
failed_agent_ids?: string[];
extra_metadata?: {
"supervisor.addon_update"?: boolean;
};
with_automatic_settings: boolean;
addon_update?: boolean;
}
Expand Down Expand Up @@ -322,17 +326,22 @@ export const computeBackupSize = (backup: BackupContent) =>

export type BackupType = "automatic" | "manual" | "addon_update";

export const BACKUP_TYPE_ORDER: BackupType[] = [
"automatic",
"manual",
"addon_update",
];
const BACKUP_TYPE_ORDER: BackupType[] = ["automatic", "manual", "addon_update"];

export const getBackupTypes = memoize((isHassio: boolean) =>
isHassio
? BACKUP_TYPE_ORDER
: BACKUP_TYPE_ORDER.filter((type) => type !== "addon_update")
);

export const computeBackupType = (backup: BackupContent): BackupType => {
export const computeBackupType = (
backup: BackupContent,
isHassio: boolean
): BackupType => {
if (backup.with_automatic_settings) {
return "automatic";
}
if (backup.addon_update) {
if (isHassio && backup.extra_metadata?.["supervisor.addon_update"]) {
return "addon_update";
}
return "manual";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import type { CSSResultGroup } from "lit";
import { css, html, LitElement } from "lit";
import { customElement, property } from "lit/decorators";
import memoizeOne from "memoize-one";
import { isComponentLoaded } from "../../../../../common/config/is_component_loaded";
import "../../../../../components/ha-button";
import "../../../../../components/ha-card";
import "../../../../../components/ha-icon-next";
import "../../../../../components/ha-md-list";
import "../../../../../components/ha-md-list-item";
import type { BackupContent, BackupType } from "../../../../../data/backup";
import {
BACKUP_TYPE_ORDER,
computeBackupSize,
computeBackupType,
getBackupTypes,
} from "../../../../../data/backup";
import { haStyle } from "../../../../../resources/styles";
import type { HomeAssistant } from "../../../../../types";
Expand Down Expand Up @@ -46,17 +47,21 @@ class HaBackupOverviewBackups extends LitElement {
@property({ attribute: false }) public backups: BackupContent[] = [];

private _stats = memoizeOne(
(backups: BackupContent[]): [BackupType, BackupStats][] =>
BACKUP_TYPE_ORDER.map((type) => {
(
backups: BackupContent[],
isHassio: boolean
): [BackupType, BackupStats][] =>
getBackupTypes(isHassio).map((type) => {
const backupsOfType = backups.filter(
(backup) => computeBackupType(backup) === type
(backup) => computeBackupType(backup, isHassio) === type
);
return [type, computeBackupStats(backupsOfType)] as const;
})
);

render() {
const stats = this._stats(this.backups);
const isHassio = isComponentLoaded(this.hass, "hassio");
const stats = this._stats(this.backups, isHassio);

return html`
<ha-card class="my-backups">
Expand Down
36 changes: 24 additions & 12 deletions src/panels/config/backup/ha-config-backup-backups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { CSSResultGroup, TemplateResult } from "lit";
import { html, LitElement, nothing } from "lit";
import { customElement, property, query, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { isComponentLoaded } from "../../../common/config/is_component_loaded";
import { relativeTime } from "../../../common/datetime/relative_time";
import { storage } from "../../../common/decorators/storage";
import { fireEvent, type HASSDomEvent } from "../../../common/dom/fire_event";
Expand Down Expand Up @@ -39,14 +40,14 @@ import type {
BackupContent,
} from "../../../data/backup";
import {
BACKUP_TYPE_ORDER,
compareAgents,
computeBackupAgentName,
computeBackupSize,
computeBackupType,
deleteBackup,
generateBackup,
generateBackupWithAutomaticSettings,
getBackupTypes,
isLocalAgent,
isNetworkMountAgent,
} from "../../../data/backup";
Expand Down Expand Up @@ -275,9 +276,13 @@ class HaConfigBackupBackups extends SubscribeMixin(LitElement) {
);

private _groupOrder = memoizeOne(
(activeGrouping: string | undefined, localize: LocalizeFunc) =>
(
activeGrouping: string | undefined,
localize: LocalizeFunc,
isHassio: boolean
) =>
activeGrouping === "formatted_type"
? BACKUP_TYPE_ORDER.map((type) =>
? getBackupTypes(isHassio).map((type) =>
localize(`ui.panel.config.backup.type.${type}`)
)
: undefined
Expand All @@ -301,18 +306,19 @@ class HaConfigBackupBackups extends SubscribeMixin(LitElement) {
(
backups: BackupContent[],
filters: DataTableFiltersValues,
localize: LocalizeFunc
localize: LocalizeFunc,
isHassio: boolean
): BackupRow[] => {
const typeFilter = filters["ha-filter-states"] as string[] | undefined;
let filteredBackups = backups;
if (typeFilter?.length) {
filteredBackups = filteredBackups.filter((backup) => {
const type = computeBackupType(backup);
const type = computeBackupType(backup, isHassio);
return typeFilter.includes(type);
});
}
return filteredBackups.map((backup) => {
const type = computeBackupType(backup);
const type = computeBackupType(backup, isHassio);
const agentIds = Object.keys(backup.agents);
return {
...backup,
Expand All @@ -331,8 +337,13 @@ class HaConfigBackupBackups extends SubscribeMixin(LitElement) {
protected render(): TemplateResult {
const backupInProgress =
"state" in this.manager && this.manager.state === "in_progress";

const data = this._data(this.backups, this._filters, this.hass.localize);
const isHassio = isComponentLoaded(this.hass, "hassio");
const data = this._data(
this.backups,
this._filters,
this.hass.localize,
isHassio
);
const maxDisplayedAgents = Math.min(
this._maxAgents(data),
this.narrow ? 3 : 5
Expand Down Expand Up @@ -367,7 +378,8 @@ class HaConfigBackupBackups extends SubscribeMixin(LitElement) {
.initialCollapsedGroups=${this._activeCollapsed}
.groupOrder=${this._groupOrder(
this._activeGrouping,
this.hass.localize
this.hass.localize,
isHassio
)}
@grouping-changed=${this._handleGroupingChanged}
@collapsed-changed=${this._handleCollapseChanged}
Expand Down Expand Up @@ -431,7 +443,7 @@ class HaConfigBackupBackups extends SubscribeMixin(LitElement) {
.hass=${this.hass}
.label=${this.hass.localize("ui.panel.config.backup.backup_type")}
.value=${this._filters["ha-filter-states"]}
.states=${this._states(this.hass.localize)}
.states=${this._states(this.hass.localize, isHassio)}
@data-table-filter-changed=${this._filterChanged}
slot="filter-pane"
expanded
Expand All @@ -456,8 +468,8 @@ class HaConfigBackupBackups extends SubscribeMixin(LitElement) {
`;
}

private _states = memoizeOne((localize: LocalizeFunc) =>
BACKUP_TYPE_ORDER.map((type) => ({
private _states = memoizeOne((localize: LocalizeFunc, isHassio: boolean) =>
getBackupTypes(isHassio).map((type) => ({
value: type,
label: localize(`ui.panel.config.backup.type.${type}`),
}))
Expand Down

0 comments on commit 91831a4

Please sign in to comment.