|
| 1 | +import { css, html, LitElement, nothing } from "lit"; |
| 2 | +import { customElement, property, query, state } from "lit/decorators"; |
| 3 | +import { relativeTime } from "../../../common/datetime/relative_time"; |
| 4 | +import { fireEvent } from "../../../common/dom/fire_event"; |
| 5 | +import "../../../components/ha-button"; |
| 6 | +import type { HaMdDialog } from "../../../components/ha-md-dialog"; |
| 7 | +import "../../../components/ha-md-dialog"; |
| 8 | +import "../../../components/ha-md-list"; |
| 9 | +import "../../../components/ha-md-list-item"; |
| 10 | +import type { HaSwitch } from "../../../components/ha-switch"; |
| 11 | +import "../../../components/ha-switch"; |
| 12 | +import type { BackupConfig } from "../../../data/backup"; |
| 13 | +import { fetchBackupConfig } from "../../../data/backup"; |
| 14 | +import type { HassDialog } from "../../../dialogs/make-dialog-manager"; |
| 15 | +import type { HomeAssistant } from "../../../types"; |
| 16 | +import type { LabsPreviewFeatureEnableDialogParams } from "./show-dialog-labs-preview-feature-enable"; |
| 17 | + |
| 18 | +@customElement("dialog-labs-preview-feature-enable") |
| 19 | +export class DialogLabsPreviewFeatureEnable |
| 20 | + extends LitElement |
| 21 | + implements HassDialog<LabsPreviewFeatureEnableDialogParams> |
| 22 | +{ |
| 23 | + @property({ attribute: false }) public hass!: HomeAssistant; |
| 24 | + |
| 25 | + @state() private _params?: LabsPreviewFeatureEnableDialogParams; |
| 26 | + |
| 27 | + @state() private _backupConfig?: BackupConfig; |
| 28 | + |
| 29 | + @state() private _createBackup = false; |
| 30 | + |
| 31 | + @query("ha-md-dialog") private _dialog?: HaMdDialog; |
| 32 | + |
| 33 | + public async showDialog( |
| 34 | + params: LabsPreviewFeatureEnableDialogParams |
| 35 | + ): Promise<void> { |
| 36 | + this._params = params; |
| 37 | + this._createBackup = false; |
| 38 | + await this._fetchBackupConfig(); |
| 39 | + } |
| 40 | + |
| 41 | + public closeDialog(): boolean { |
| 42 | + this._dialog?.close(); |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + private _dialogClosed(): void { |
| 47 | + this._params = undefined; |
| 48 | + this._backupConfig = undefined; |
| 49 | + this._createBackup = false; |
| 50 | + fireEvent(this, "dialog-closed", { dialog: this.localName }); |
| 51 | + } |
| 52 | + |
| 53 | + private async _fetchBackupConfig() { |
| 54 | + try { |
| 55 | + const { config } = await fetchBackupConfig(this.hass); |
| 56 | + this._backupConfig = config; |
| 57 | + |
| 58 | + // Default to enabled if automatic backups are configured, disabled otherwise |
| 59 | + this._createBackup = |
| 60 | + config.automatic_backups_configured && |
| 61 | + !!config.create_backup.password && |
| 62 | + config.create_backup.agent_ids.length > 0; |
| 63 | + } catch { |
| 64 | + // User will get manual backup option if fetch fails |
| 65 | + this._createBackup = false; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private _computeCreateBackupTexts(): |
| 70 | + | { title: string; description?: string } |
| 71 | + | undefined { |
| 72 | + if ( |
| 73 | + !this._backupConfig || |
| 74 | + !this._backupConfig.automatic_backups_configured || |
| 75 | + !this._backupConfig.create_backup.password || |
| 76 | + this._backupConfig.create_backup.agent_ids.length === 0 |
| 77 | + ) { |
| 78 | + return { |
| 79 | + title: this.hass.localize("ui.panel.config.labs.create_backup.manual"), |
| 80 | + description: this.hass.localize( |
| 81 | + "ui.panel.config.labs.create_backup.manual_description" |
| 82 | + ), |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + const lastAutomaticBackupDate = this._backupConfig |
| 87 | + .last_completed_automatic_backup |
| 88 | + ? new Date(this._backupConfig.last_completed_automatic_backup) |
| 89 | + : null; |
| 90 | + const now = new Date(); |
| 91 | + |
| 92 | + return { |
| 93 | + title: this.hass.localize("ui.panel.config.labs.create_backup.automatic"), |
| 94 | + description: lastAutomaticBackupDate |
| 95 | + ? this.hass.localize( |
| 96 | + "ui.panel.config.labs.create_backup.automatic_description_last", |
| 97 | + { |
| 98 | + relative_time: relativeTime( |
| 99 | + lastAutomaticBackupDate, |
| 100 | + this.hass.locale, |
| 101 | + now, |
| 102 | + true |
| 103 | + ), |
| 104 | + } |
| 105 | + ) |
| 106 | + : this.hass.localize( |
| 107 | + "ui.panel.config.labs.create_backup.automatic_description_none" |
| 108 | + ), |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + private _createBackupChanged(ev: Event): void { |
| 113 | + this._createBackup = (ev.target as HaSwitch).checked; |
| 114 | + } |
| 115 | + |
| 116 | + private _handleCancel(): void { |
| 117 | + this.closeDialog(); |
| 118 | + } |
| 119 | + |
| 120 | + private _handleConfirm(): void { |
| 121 | + if (this._params) { |
| 122 | + this._params.onConfirm(this._createBackup); |
| 123 | + } |
| 124 | + this.closeDialog(); |
| 125 | + } |
| 126 | + |
| 127 | + protected render() { |
| 128 | + if (!this._params) { |
| 129 | + return nothing; |
| 130 | + } |
| 131 | + |
| 132 | + const createBackupTexts = this._computeCreateBackupTexts(); |
| 133 | + |
| 134 | + return html` |
| 135 | + <ha-md-dialog open @closed=${this._dialogClosed}> |
| 136 | + <span slot="headline"> |
| 137 | + ${this.hass.localize("ui.panel.config.labs.enable_title")} |
| 138 | + </span> |
| 139 | + <div slot="content"> |
| 140 | + <p> |
| 141 | + ${this.hass.localize( |
| 142 | + `component.${this._params.preview_feature.domain}.preview_features.${this._params.preview_feature.preview_feature}.enable_confirmation` |
| 143 | + ) || this.hass.localize("ui.panel.config.labs.enable_confirmation")} |
| 144 | + </p> |
| 145 | + </div> |
| 146 | + <div slot="actions"> |
| 147 | + ${createBackupTexts |
| 148 | + ? html` |
| 149 | + <ha-md-list> |
| 150 | + <ha-md-list-item> |
| 151 | + <span slot="headline">${createBackupTexts.title}</span> |
| 152 | + ${createBackupTexts.description |
| 153 | + ? html` |
| 154 | + <span slot="supporting-text"> |
| 155 | + ${createBackupTexts.description} |
| 156 | + </span> |
| 157 | + ` |
| 158 | + : nothing} |
| 159 | + <ha-switch |
| 160 | + slot="end" |
| 161 | + .checked=${this._createBackup} |
| 162 | + @change=${this._createBackupChanged} |
| 163 | + ></ha-switch> |
| 164 | + </ha-md-list-item> |
| 165 | + </ha-md-list> |
| 166 | + ` |
| 167 | + : nothing} |
| 168 | + <div> |
| 169 | + <ha-button appearance="plain" @click=${this._handleCancel}> |
| 170 | + ${this.hass.localize("ui.common.cancel")} |
| 171 | + </ha-button> |
| 172 | + <ha-button |
| 173 | + appearance="filled" |
| 174 | + variant="brand" |
| 175 | + @click=${this._handleConfirm} |
| 176 | + > |
| 177 | + ${this.hass.localize("ui.panel.config.labs.enable")} |
| 178 | + </ha-button> |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + </ha-md-dialog> |
| 182 | + `; |
| 183 | + } |
| 184 | + |
| 185 | + static readonly styles = css` |
| 186 | + ha-md-dialog { |
| 187 | + --dialog-content-padding: var(--ha-space-6); |
| 188 | + } |
| 189 | +
|
| 190 | + p { |
| 191 | + margin: 0; |
| 192 | + color: var(--secondary-text-color); |
| 193 | + } |
| 194 | +
|
| 195 | + div[slot="actions"] { |
| 196 | + display: flex; |
| 197 | + flex-direction: column; |
| 198 | + padding: 0; |
| 199 | + } |
| 200 | +
|
| 201 | + ha-md-list { |
| 202 | + background: none; |
| 203 | + --md-list-item-leading-space: var(--ha-space-6); |
| 204 | + --md-list-item-trailing-space: var(--ha-space-6); |
| 205 | + margin: 0; |
| 206 | + padding: 0; |
| 207 | + border-top: 1px solid var(--divider-color); |
| 208 | + } |
| 209 | +
|
| 210 | + div[slot="actions"] > div { |
| 211 | + display: flex; |
| 212 | + justify-content: flex-end; |
| 213 | + gap: var(--ha-space-2); |
| 214 | + padding: var(--ha-space-4) var(--ha-space-6); |
| 215 | + } |
| 216 | + `; |
| 217 | +} |
| 218 | + |
| 219 | +declare global { |
| 220 | + interface HTMLElementTagNameMap { |
| 221 | + "dialog-labs-preview-feature-enable": DialogLabsPreviewFeatureEnable; |
| 222 | + } |
| 223 | +} |
0 commit comments