Move snapshot toggle to persistent checkbox

This commit is contained in:
Joakim Sørensen 2021-06-15 16:40:24 +00:00
parent 0e3eed0563
commit 0bfeb22209
6 changed files with 76 additions and 14 deletions

View File

@ -977,6 +977,7 @@ class HassioAddonInfo extends LitElement {
showDialogSupervisorUpdate(this, {
supervisor: this.supervisor,
name: this.addon.name,
slug: this.addon.slug,
version: this.addon.version_latest,
snapshotParams: {
name: `addon_${this.addon.slug}_${this.addon.version}`,

View File

@ -161,6 +161,7 @@ export class HassioUpdate extends LitElement {
showDialogSupervisorUpdate(this, {
supervisor: this.supervisor,
name: "Home Assistant Core",
slug: "core",
version: this.supervisor.core.version_latest,
snapshotParams: {
name: `core_${this.supervisor.core.version}`,

View File

@ -2,19 +2,32 @@ import "@material/mwc-button/mwc-button";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, state } from "lit/decorators";
import { fireEvent } from "../../../../src/common/dom/fire_event";
import "../../../../src/components/ha-checkbox";
import "../../../../src/components/ha-circular-progress";
import "../../../../src/components/ha-dialog";
import "../../../../src/components/ha-settings-row";
import "../../../../src/components/ha-svg-icon";
import "../../../../src/components/ha-switch";
import {
extractApiErrorMessage,
ignoreSupervisorError,
} from "../../../../src/data/hassio/common";
import {
SupervisorFrontendPrefrences,
fetchSupervisorFrontendPreferences,
saveSupervisorFrontendPreferences,
} from "../../../../src/data/supervisor/supervisor";
import { createHassioPartialSnapshot } from "../../../../src/data/hassio/snapshot";
import { haStyle, haStyleDialog } from "../../../../src/resources/styles";
import type { HomeAssistant } from "../../../../src/types";
import { SupervisorDialogSupervisorUpdateParams } from "./show-dialog-update";
import memoizeOne from "memoize-one";
const snapshot_before_update = memoizeOne(
(slug: string, frontendPrefrences: SupervisorFrontendPrefrences) =>
slug in frontendPrefrences.snapshot_before_update
? frontendPrefrences.snapshot_before_update[slug]
: true
);
@customElement("dialog-supervisor-update")
class DialogSupervisorUpdate extends LitElement {
@ -22,12 +35,12 @@ class DialogSupervisorUpdate extends LitElement {
@state() private _opened = false;
@state() private _createSnapshot = true;
@state() private _action: "snapshot" | "update" | null = null;
@state() private _error?: string;
@state() private _frontendPrefrences?: SupervisorFrontendPrefrences;
@state()
private _dialogParams?: SupervisorDialogSupervisorUpdateParams;
@ -36,14 +49,17 @@ class DialogSupervisorUpdate extends LitElement {
): Promise<void> {
this._opened = true;
this._dialogParams = params;
this._frontendPrefrences = await fetchSupervisorFrontendPreferences(
this.hass
);
await this.updateComplete;
}
public closeDialog(): void {
this._action = null;
this._createSnapshot = true;
this._error = undefined;
this._dialogParams = undefined;
this._frontendPrefrences = undefined;
fireEvent(this, "dialog-closed", { dialog: this.localName });
}
@ -56,7 +72,7 @@ class DialogSupervisorUpdate extends LitElement {
}
protected render(): TemplateResult {
if (!this._dialogParams) {
if (!this._dialogParams || !this._frontendPrefrences) {
return html``;
}
return html`
@ -82,6 +98,16 @@ class DialogSupervisorUpdate extends LitElement {
</div>
<ha-settings-row>
<ha-checkbox
.checked=${snapshot_before_update(
this._dialogParams.slug,
this._frontendPrefrences
)}
haptic
@click=${this._toggleSnapshot}
slot="prefix"
>
</ha-checkbox>
<span slot="heading">
${this._dialogParams.supervisor.localize(
"dialog.update.snapshot"
@ -94,12 +120,6 @@ class DialogSupervisorUpdate extends LitElement {
this._dialogParams.name
)}
</span>
<ha-switch
.checked=${this._createSnapshot}
haptic
@click=${this._toggleSnapshot}
>
</ha-switch>
</ha-settings-row>
<mwc-button @click=${this.closeDialog} slot="secondaryAction">
${this._dialogParams.supervisor.localize("common.cancel")}
@ -133,12 +153,27 @@ class DialogSupervisorUpdate extends LitElement {
`;
}
private _toggleSnapshot() {
this._createSnapshot = !this._createSnapshot;
private async _toggleSnapshot(): Promise<void> {
this._frontendPrefrences!.snapshot_before_update[
this._dialogParams!.slug
] = !snapshot_before_update(
this._dialogParams!.slug,
this._frontendPrefrences!
);
await saveSupervisorFrontendPreferences(
this.hass,
this._frontendPrefrences!
);
}
private async _update() {
if (this._createSnapshot) {
if (
snapshot_before_update(
this._dialogParams!.slug,
this._frontendPrefrences!
)
) {
this._action = "snapshot";
try {
await createHassioPartialSnapshot(

View File

@ -5,6 +5,7 @@ export interface SupervisorDialogSupervisorUpdateParams {
supervisor: Supervisor;
name: string;
version: string;
slug: string;
snapshotParams: any;
updateHandler: () => Promise<void>;
}

View File

@ -164,6 +164,7 @@ class HassioCoreInfo extends LitElement {
showDialogSupervisorUpdate(this, {
supervisor: this.supervisor,
name: "Home Assistant Core",
slug: "core",
version: this.supervisor.core.version_latest,
snapshotParams: {
name: `core_${this.supervisor.core.version}`,

View File

@ -2,6 +2,7 @@ import { Connection, getCollection } from "home-assistant-js-websocket";
import { Store } from "home-assistant-js-websocket/dist/store";
import { LocalizeFunc } from "../../common/translations/localize";
import { HomeAssistant } from "../../types";
import { fetchFrontendUserData, saveFrontendUserData } from "../frontend";
import { HassioAddonsInfo } from "../hassio/addon";
import { HassioHassOSInfo, HassioHostInfo } from "../hassio/host";
import { NetworkInfo } from "../hassio/network";
@ -13,6 +14,28 @@ import {
} from "../hassio/supervisor";
import { SupervisorStore } from "./store";
export interface SupervisorFrontendPrefrences {
snapshot_before_update: Record<string, boolean>;
}
declare global {
interface FrontendUserData {
supervisor: SupervisorFrontendPrefrences;
}
}
export const fetchSupervisorFrontendPreferences = async (
hass: HomeAssistant
): Promise<SupervisorFrontendPrefrences> => {
const stored = await fetchFrontendUserData(hass.connection, "supervisor");
return stored || { snapshot_before_update: {} };
};
export const saveSupervisorFrontendPreferences = (
hass: HomeAssistant,
data: SupervisorFrontendPrefrences
) => saveFrontendUserData(hass.connection, "supervisor", data);
export const supervisorWSbaseCommand = {
type: "supervisor/api",
method: "GET",