Fix notifications

This commit is contained in:
Paul Bottein 2024-10-17 20:16:49 +02:00
parent 3bea09f161
commit 97c69e620c
No known key found for this signature in database
6 changed files with 32 additions and 41 deletions

View File

@ -1,11 +1,11 @@
import { html, LitElement, nothing } from "lit";
import { property, state, query } from "lit/decorators";
import { mdiClose } from "@mdi/js"; import { mdiClose } from "@mdi/js";
import { html, LitElement, nothing } from "lit";
import { property, query, state } from "lit/decorators";
import { computeRTL } from "../common/util/compute_rtl"; import { computeRTL } from "../common/util/compute_rtl";
import "../components/ha-button";
import "../components/ha-toast"; import "../components/ha-toast";
import type { HaToast } from "../components/ha-toast"; import type { HaToast } from "../components/ha-toast";
import type { HomeAssistant } from "../types"; import type { HomeAssistant } from "../types";
import "../components/ha-button";
export interface ShowToastParams { export interface ShowToastParams {
message: string; message: string;
@ -27,12 +27,10 @@ class NotificationManager extends LitElement {
@query("ha-toast") private _toast!: HaToast | undefined; @query("ha-toast") private _toast!: HaToast | undefined;
public async showDialog(parameters: ShowToastParams) { public async showDialog(parameters: ShowToastParams) {
if (this._parameters && this._parameters.message !== parameters.message) { this._toast?.close();
this._parameters = undefined;
await this.updateComplete;
}
if (!parameters || parameters.duration === 0) { if (!parameters || parameters.duration === 0) {
this._parameters = undefined;
return; return;
} }
@ -44,10 +42,9 @@ class NotificationManager extends LitElement {
) { ) {
this._parameters.duration = 4000; this._parameters.duration = 4000;
} }
}
public shouldUpdate(changedProperties) { await this.updateComplete;
return !this._toast || changedProperties.has("_parameters"); this._toast?.show();
} }
private _toastClosed() { private _toastClosed() {
@ -61,7 +58,6 @@ class NotificationManager extends LitElement {
return html` return html`
<ha-toast <ha-toast
leading leading
open
dir=${computeRTL(this.hass) ? "rtl" : "ltr"} dir=${computeRTL(this.hass) ? "rtl" : "ltr"}
.labelText=${this._parameters.message} .labelText=${this._parameters.message}
.timeoutMs=${this._parameters.duration!} .timeoutMs=${this._parameters.duration!}
@ -77,12 +73,14 @@ class NotificationManager extends LitElement {
` `
: nothing} : nothing}
${this._parameters?.dismissable ${this._parameters?.dismissable
? html`<ha-icon-button ? html`
<ha-icon-button
.label=${this.hass.localize("ui.common.close")} .label=${this.hass.localize("ui.common.close")}
.path=${mdiClose} .path=${mdiClose}
dialogAction="close" dialogAction="close"
slot="dismiss" slot="dismiss"
></ha-icon-button>` ></ha-icon-button>
`
: nothing} : nothing}
</ha-toast> </ha-toast>
`; `;

View File

@ -1,6 +1,5 @@
import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box"; import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { showDeletedToastWithUndo } from "../../../util/toast-deleted-success";
import { Lovelace } from "../types"; import { Lovelace } from "../types";
import { deleteBadge } from "./config-util"; import { deleteBadge } from "./config-util";
import { LovelaceCardPath } from "./lovelace-path"; import { LovelaceCardPath } from "./lovelace-path";
@ -18,7 +17,11 @@ export async function deleteBadgeWithUndo(
lovelace.saveConfig(oldConfig); lovelace.saveConfig(oldConfig);
}; };
await lovelace.saveConfig(newConfig); await lovelace.saveConfig(newConfig);
showDeletedToastWithUndo(element, hass, action); lovelace.showToast({
message: hass.localize("ui.common.successfully_deleted"),
duration: 8000,
action: { action, text: hass.localize("ui.common.undo") },
});
} catch (err: any) { } catch (err: any) {
showAlertDialog(element, { showAlertDialog(element, {
text: `Deleting failed: ${err.message}`, text: `Deleting failed: ${err.message}`,

View File

@ -1,6 +1,5 @@
import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box"; import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { showDeletedToastWithUndo } from "../../../util/toast-deleted-success";
import { Lovelace } from "../types"; import { Lovelace } from "../types";
import { deleteCard } from "./config-util"; import { deleteCard } from "./config-util";
import { LovelaceCardPath } from "./lovelace-path"; import { LovelaceCardPath } from "./lovelace-path";
@ -18,7 +17,11 @@ export async function deleteCardWithUndo(
await lovelace.saveConfig(oldConfig); await lovelace.saveConfig(oldConfig);
}; };
await lovelace.saveConfig(newConfig); await lovelace.saveConfig(newConfig);
showDeletedToastWithUndo(element, hass, action); lovelace.showToast({
message: hass.localize("ui.common.successfully_deleted"),
duration: 8000,
action: { action, text: hass.localize("ui.common.undo") },
});
} catch (err: any) { } catch (err: any) {
showAlertDialog(element, { showAlertDialog(element, {
text: `Deleting failed: ${err.message}`, text: `Deleting failed: ${err.message}`,

View File

@ -31,6 +31,8 @@ import { showSaveDialog } from "./editor/show-save-config-dialog";
import "./hui-root"; import "./hui-root";
import { generateLovelaceDashboardStrategy } from "./strategies/get-strategy"; import { generateLovelaceDashboardStrategy } from "./strategies/get-strategy";
import { Lovelace } from "./types"; import { Lovelace } from "./types";
import { fireEvent } from "../../common/dom/fire_event";
import { ShowToastParams } from "../../managers/notification-manager";
(window as any).loadCardHelpers = () => import("./custom-card-helpers"); (window as any).loadCardHelpers = () => import("./custom-card-helpers");
@ -434,6 +436,9 @@ export class LovelacePanel extends LitElement {
throw err; throw err;
} }
}, },
showToast: (params: ShowToastParams) => {
fireEvent(this, "hass-notification", params);
},
}; };
} }

View File

@ -14,6 +14,7 @@ import { LovelaceHeaderFooterConfig } from "./header-footer/types";
import { LovelaceCardFeatureConfig } from "./card-features/types"; import { LovelaceCardFeatureConfig } from "./card-features/types";
import { LovelaceElement, LovelaceElementConfig } from "./elements/types"; import { LovelaceElement, LovelaceElementConfig } from "./elements/types";
import { LovelaceHeadingBadgeConfig } from "./heading-badges/types"; import { LovelaceHeadingBadgeConfig } from "./heading-badges/types";
import { ShowToastParams } from "../../managers/notification-manager";
declare global { declare global {
// eslint-disable-next-line // eslint-disable-next-line
@ -35,6 +36,7 @@ export interface Lovelace {
setEditMode: (editMode: boolean) => void; setEditMode: (editMode: boolean) => void;
saveConfig: (newConfig: LovelaceRawConfig) => Promise<void>; saveConfig: (newConfig: LovelaceRawConfig) => Promise<void>;
deleteConfig: () => Promise<void>; deleteConfig: () => Promise<void>;
showToast: (params: ShowToastParams) => void;
} }
export interface LovelaceBadge extends HTMLElement { export interface LovelaceBadge extends HTMLElement {

View File

@ -1,20 +0,0 @@
import { ShowToastParams } from "../managers/notification-manager";
import { HomeAssistant } from "../types";
import { showToast } from "./toast";
export const showDeletedToastWithUndo = (
el: HTMLElement,
hass: HomeAssistant,
action?: () => void
) => {
const toastParams: ShowToastParams = {
message: hass!.localize("ui.common.successfully_deleted"),
duration: 8000,
};
if (action) {
toastParams.action = { action, text: hass!.localize("ui.common.undo") };
}
showToast(el, toastParams);
};