Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter backups by type #23366

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ class HaBackupOverviewBackups extends LitElement {
<div class="card-header">My backups</div>
<div class="card-content">
<ha-md-list>
<ha-md-list-item type="link" href="/config/backup/backups">
<ha-md-list-item
type="link"
href="/config/backup/backups?type=automatic"
>
<ha-svg-icon slot="start" .path=${mdiCalendarSync}></ha-svg-icon>
<div slot="headline">
${automaticStats.count} automatic backups
Expand All @@ -72,7 +75,10 @@ class HaBackupOverviewBackups extends LitElement {
</div>
<ha-icon-next slot="end"></ha-icon-next>
</ha-md-list-item>
<ha-md-list-item type="link" href="/config/backup/backups">
<ha-md-list-item
type="link"
href="/config/backup/backups?type=manual"
>
<ha-svg-icon slot="start" .path=${mdiGestureTap}></ha-svg-icon>
<div slot="headline">${manualStats.count} manual backups</div>
<div slot="supporting-text">
Expand Down
54 changes: 46 additions & 8 deletions src/panels/config/backup/ha-config-backup-backups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,36 @@ class HaConfigBackupBackups extends SubscribeMixin(LitElement) {
})
private _activeCollapsed: string[] = [];

@state() private _searchParams = new URLSearchParams(window.location.search);

@query("hass-tabs-subpage-data-table", true)
private _dataTable!: HaTabsSubpageDataTable;

public connectedCallback() {
super.connectedCallback();
window.addEventListener("location-changed", this._locationChanged);
window.addEventListener("popstate", this._popState);
this._searchParams = new URLSearchParams(window.location.search);
}

disconnectedCallback(): void {
super.disconnectedCallback();
window.removeEventListener("location-changed", this._locationChanged);
window.removeEventListener("popstate", this._popState);
}

private _locationChanged = () => {
if (window.location.search.substring(1) !== this._searchParams.toString()) {
this._searchParams = new URLSearchParams(window.location.search);
}
};

private _popState = () => {
if (window.location.search.substring(1) !== this._searchParams.toString()) {
this._searchParams = new URLSearchParams(window.location.search);
}
};

private _columns = memoizeOne(
(localize: LocalizeFunc): DataTableColumnContainer<BackupRow> => ({
name: {
Expand Down Expand Up @@ -230,13 +257,24 @@ class HaConfigBackupBackups extends SubscribeMixin(LitElement) {
return capitalizeFirstLetter(type);
}

private _data = memoizeOne((backups: BackupContent[]): BackupRow[] =>
backups.map((backup) => ({
...backup,
formatted_type: this._formatBackupType(
backup.with_automatic_settings ? "automatic" : "manual"
),
}))
private _data = memoizeOne(
(backups: BackupContent[], searchParams: URLSearchParams): BackupRow[] => {
const type = searchParams.get("type")?.toLowerCase();
let filteredBackups = backups;
if (type) {
filteredBackups = filteredBackups.filter(
(backup) =>
backup.with_automatic_settings === (type === "automatic") ||
(backup.with_automatic_settings === null && type === "manual")
);
}
return filteredBackups.map((backup) => ({
...backup,
formatted_type: this._formatBackupType(
backup.with_automatic_settings ? "automatic" : "manual"
),
}));
}
);

protected render(): TemplateResult {
Expand Down Expand Up @@ -268,7 +306,7 @@ class HaConfigBackupBackups extends SubscribeMixin(LitElement) {
.route=${this.route}
@row-click=${this._showBackupDetails}
.columns=${this._columns(this.hass.localize)}
.data=${this._data(this.backups)}
.data=${this._data(this.backups, this._searchParams)}
.noDataText=${this.hass.localize("ui.panel.config.backup.no_backups")}
.searchLabel=${this.hass.localize(
"ui.panel.config.backup.picker.search"
Expand Down
Loading