[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-wallet-core] branch master updated: imported useNotifications, n
From: |
gnunet |
Subject: |
[taler-wallet-core] branch master updated: imported useNotifications, new memory storage hook and fixed but in localstorage |
Date: |
Fri, 19 May 2023 18:24:36 +0200 |
This is an automated email from the git hooks/post-receive script.
sebasjm pushed a commit to branch master
in repository wallet-core.
The following commit(s) were added to refs/heads/master by this push:
new 35cc13e22 imported useNotifications, new memory storage hook and fixed
but in localstorage
35cc13e22 is described below
commit 35cc13e229ddc4953a1e68b6b7ea18c54eb9a70b
Author: Sebastian <sebasjm@gmail.com>
AuthorDate: Fri May 19 13:24:30 2023 -0300
imported useNotifications, new memory storage hook and fixed but in
localstorage
---
packages/web-util/src/hooks/index.ts | 6 ++
packages/web-util/src/hooks/useLocalStorage.ts | 2 +-
.../{useLocalStorage.ts => useMemoryStorage.ts} | 22 ++-----
packages/web-util/src/hooks/useNotifications.ts | 67 ++++++++++++++++++++++
4 files changed, 79 insertions(+), 18 deletions(-)
diff --git a/packages/web-util/src/hooks/index.ts
b/packages/web-util/src/hooks/index.ts
index e5cb54e21..ae8872497 100644
--- a/packages/web-util/src/hooks/index.ts
+++ b/packages/web-util/src/hooks/index.ts
@@ -1,5 +1,11 @@
export { useLang } from "./useLang.js";
export { useLocalStorage } from "./useLocalStorage.js";
+export { useMemoryStorage } from "./useMemoryStorage.js";
+export {
+ useNotifications,
+ notifyError,
+ notifyInfo,
+} from "./useNotifications.js";
export {
useAsyncAsHook,
HookError,
diff --git a/packages/web-util/src/hooks/useLocalStorage.ts
b/packages/web-util/src/hooks/useLocalStorage.ts
index 495c9b0f8..131825736 100644
--- a/packages/web-util/src/hooks/useLocalStorage.ts
+++ b/packages/web-util/src/hooks/useLocalStorage.ts
@@ -70,7 +70,7 @@ export function useLocalStorage(
}, []);
const setValue = (value?: string): void => {
- if (!value) {
+ if (value === undefined) {
storage.delete(key);
} else {
storage.set(key, value);
diff --git a/packages/web-util/src/hooks/useLocalStorage.ts
b/packages/web-util/src/hooks/useMemoryStorage.ts
similarity index 75%
copy from packages/web-util/src/hooks/useLocalStorage.ts
copy to packages/web-util/src/hooks/useMemoryStorage.ts
index 495c9b0f8..7160b035e 100644
--- a/packages/web-util/src/hooks/useLocalStorage.ts
+++ b/packages/web-util/src/hooks/useMemoryStorage.ts
@@ -33,26 +33,14 @@ export interface LocalStorageState {
reset: () => void;
}
-const supportLocalStorage = typeof window !== "undefined";
-const supportBrowserStorage =
- typeof chrome !== "undefined" && typeof chrome.storage !== "undefined";
+const storage: ObservableMap<string, string> = memoryMap<string>();
-const storage: ObservableMap<string, string> = (function buildStorage() {
- if (supportBrowserStorage) {
- return browserStorageMap(memoryMap<string>());
- } else if (supportLocalStorage) {
- return localStorageMap();
- } else {
- return memoryMap<string>();
- }
-})();
-
-export function useLocalStorage(
+export function useMemoryStorage(
key: string,
initialValue: string,
): Required<LocalStorageState>;
-export function useLocalStorage(key: string): LocalStorageState;
-export function useLocalStorage(
+export function useMemoryStorage(key: string): LocalStorageState;
+export function useMemoryStorage(
key: string,
initialValue?: string,
): LocalStorageState {
@@ -70,7 +58,7 @@ export function useLocalStorage(
}, []);
const setValue = (value?: string): void => {
- if (!value) {
+ if (value === undefined) {
storage.delete(key);
} else {
storage.set(key, value);
diff --git a/packages/web-util/src/hooks/useNotifications.ts
b/packages/web-util/src/hooks/useNotifications.ts
new file mode 100644
index 000000000..deaa7a7c1
--- /dev/null
+++ b/packages/web-util/src/hooks/useNotifications.ts
@@ -0,0 +1,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);
+ },
+ };
+ });
+}
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [taler-wallet-core] branch master updated: imported useNotifications, new memory storage hook and fixed but in localstorage,
gnunet <=