From 165d5c0e7f5b032dea64ceb5215dc1be6bede0d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Tue, 21 Dec 2021 16:51:25 +0100 Subject: [PATCH] fix notification weirdness, tidy up api --- ui/src/components/Address.svelte | 26 +++++++++++++++---------- ui/src/components/Notifications.svelte | 27 ++++++++++++++++---------- ui/src/notifications.ts | 21 +++++++++++++++++--- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/ui/src/components/Address.svelte b/ui/src/components/Address.svelte index dca0793..7220a19 100644 --- a/ui/src/components/Address.svelte +++ b/ui/src/components/Address.svelte @@ -7,7 +7,7 @@ import { useEntity } from "../lib/entity"; import type { UpObject } from "upend"; import { readable } from "svelte/store"; - import { notify } from "../notifications"; + import { notify, UpNotification } from "../notifications"; const dispatch = createEventDispatcher(); export let address: string; @@ -36,11 +36,14 @@ // Native open function nativeOpen() { - notify.emit("notification", { - content: `Opening ${ - inferredIds[0] || address - } in a default native application...`, - }); + notify.emit( + "notification", + new UpNotification( + `Opening ${ + inferredIds[0] || address + } in a default native application...` + ) + ); fetch(`/api/raw/${address}?native=1`) .then((response) => { if (!response.ok) { @@ -48,10 +51,13 @@ } }) .catch((err) => { - notify.emit("notification", { - content: `Failed to open in native application! (${err})`, - level: "error", - }); + notify.emit( + "notification", + new UpNotification( + `Failed to open in native application! (${err})`, + "error" + ) + ); }); } diff --git a/ui/src/components/Notifications.svelte b/ui/src/components/Notifications.svelte index 5e5dd35..88de91a 100644 --- a/ui/src/components/Notifications.svelte +++ b/ui/src/components/Notifications.svelte @@ -1,26 +1,33 @@ -{#each Object.values(notifications) as notification} -
+{#each notifications as notification (notification.id)} +
{notification.content}
diff --git a/ui/src/notifications.ts b/ui/src/notifications.ts index c9ff317..1137308 100644 --- a/ui/src/notifications.ts +++ b/ui/src/notifications.ts @@ -1,12 +1,27 @@ import mitt from "mitt"; type NotifyEvents = { - notification: Notification; + notification: UpNotification; }; -export interface Notification { +export type NotificationLevel = "info" | "error"; + +export interface INotification { + id: string; content: string; - level?: "info" | "error"; + level: NotificationLevel; +} + +export class UpNotification implements INotification { + id: string; + content: string; + level: NotificationLevel; + + constructor(content: string, level?: NotificationLevel) { + this.id = String(Math.random()); + this.content = content; + this.level = level || "info"; + } } export const notify = mitt();