aboutsummaryrefslogtreecommitdiff
path: root/packages/demobank-ui/src/hooks/notification.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-04-14 14:07:15 -0300
committerSebastian <sebasjm@gmail.com>2023-04-14 14:16:25 -0300
commit5ea22a325c069fe497b2dc8a73d4de69fd8cc27b (patch)
tree7783c8a47c5f645a2c277bb0251863e4d6165dde /packages/demobank-ui/src/hooks/notification.ts
parentc3e1a0bb519bf5012781891c15c433841203bce2 (diff)
using new localStorage api, pageState => settings, notifcation using observer api
Diffstat (limited to 'packages/demobank-ui/src/hooks/notification.ts')
-rw-r--r--packages/demobank-ui/src/hooks/notification.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/packages/demobank-ui/src/hooks/notification.ts b/packages/demobank-ui/src/hooks/notification.ts
new file mode 100644
index 000000000..7cdece515
--- /dev/null
+++ b/packages/demobank-ui/src/hooks/notification.ts
@@ -0,0 +1,54 @@
+import { TranslatedString } from "@gnu-taler/taler-util";
+import { memoryMap } from "@gnu-taler/web-util/lib/index.browser";
+import { StateUpdater, useEffect, useState } from "preact/hooks";
+
+export type NotificationMessage = ErrorNotification | InfoNotification;
+
+//FIXME: this should not be exported since every notification
+// goes throw notify function
+export interface ErrorMessage {
+ description?: string;
+ title: TranslatedString;
+ debug?: string;
+}
+
+interface ErrorNotification {
+ type: "error";
+ error: ErrorMessage;
+}
+interface InfoNotification {
+ type: "info";
+ info: TranslatedString;
+}
+
+const storage = memoryMap<NotificationMessage>();
+const NOTIFICATION_KEY = "notification";
+
+export function onNotificationUpdate(
+ handler: (newValue: NotificationMessage | undefined) => void,
+) {
+ return storage.onUpdate(NOTIFICATION_KEY, () => {
+ const newValue = storage.get(NOTIFICATION_KEY);
+ handler(newValue);
+ });
+}
+
+export function notifyError(error: ErrorMessage) {
+ storage.set(NOTIFICATION_KEY, { type: "error", error });
+}
+export function notifyInfo(info: TranslatedString) {
+ storage.set(NOTIFICATION_KEY, { type: "info", info });
+}
+
+export function useNotifications(): [
+ NotificationMessage | undefined,
+ StateUpdater<NotificationMessage | undefined>,
+] {
+ const [value, setter] = useState<NotificationMessage | undefined>();
+ useEffect(() => {
+ return storage.onUpdate(NOTIFICATION_KEY, () => {
+ setter(storage.get(NOTIFICATION_KEY));
+ });
+ });
+ return [value, setter];
+}