1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
import { TranslatedString } from "@gnu-taler/taler-util";
import { StateUpdater, useEffect, useState } from "preact/hooks";
import { memoryMap } from "../index.browser.js";
export type NotificationMessage = ErrorNotification | InfoNotification;
interface ErrorNotification {
type: "error";
title: TranslatedString;
description?: TranslatedString;
debug?: string;
}
interface InfoNotification {
type: "info";
title: TranslatedString;
}
const storage = memoryMap<NotificationMessage[]>();
const NOTIFICATION_KEY = "notification";
export function notifyError(
title: TranslatedString,
description: TranslatedString | undefined,
debug?: any,
) {
const currentState: NotificationMessage[] =
storage.get(NOTIFICATION_KEY) ?? [];
const newState = currentState.concat({
type: "error",
title,
description,
debug,
});
storage.set(NOTIFICATION_KEY, newState);
}
export function notifyInfo(title: TranslatedString) {
const currentState: NotificationMessage[] =
storage.get(NOTIFICATION_KEY) ?? [];
const newState = currentState.concat({ type: "info", title });
storage.set(NOTIFICATION_KEY, newState);
}
type Notification = {
message: NotificationMessage;
remove: () => void;
};
export function useNotifications(): Notification[] {
const [value, setter] = useState<NotificationMessage[]>([]);
useEffect(() => {
return storage.onUpdate(NOTIFICATION_KEY, () => {
const mem = storage.get(NOTIFICATION_KEY) ?? [];
setter(mem);
});
});
return value.map((message, idx) => {
return {
message,
remove: () => {
const mem = storage.get(NOTIFICATION_KEY) ?? [];
const newState = Array.from(mem);
newState.splice(idx, 1);
storage.set(NOTIFICATION_KEY, newState);
},
};
});
}
|