imported useNotifications, new memory storage hook and fixed but in localstorage
This commit is contained in:
parent
38c65cc84a
commit
35cc13e229
@ -1,5 +1,11 @@
|
|||||||
export { useLang } from "./useLang.js";
|
export { useLang } from "./useLang.js";
|
||||||
export { useLocalStorage } from "./useLocalStorage.js";
|
export { useLocalStorage } from "./useLocalStorage.js";
|
||||||
|
export { useMemoryStorage } from "./useMemoryStorage.js";
|
||||||
|
export {
|
||||||
|
useNotifications,
|
||||||
|
notifyError,
|
||||||
|
notifyInfo,
|
||||||
|
} from "./useNotifications.js";
|
||||||
export {
|
export {
|
||||||
useAsyncAsHook,
|
useAsyncAsHook,
|
||||||
HookError,
|
HookError,
|
||||||
|
@ -70,7 +70,7 @@ export function useLocalStorage(
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const setValue = (value?: string): void => {
|
const setValue = (value?: string): void => {
|
||||||
if (!value) {
|
if (value === undefined) {
|
||||||
storage.delete(key);
|
storage.delete(key);
|
||||||
} else {
|
} else {
|
||||||
storage.set(key, value);
|
storage.set(key, value);
|
||||||
|
75
packages/web-util/src/hooks/useMemoryStorage.ts
Normal file
75
packages/web-util/src/hooks/useMemoryStorage.ts
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
This file is part of GNU Anastasis
|
||||||
|
(C) 2021-2022 Anastasis SARL
|
||||||
|
|
||||||
|
GNU Anastasis is free software; you can redistribute it and/or modify it under the
|
||||||
|
terms of the GNU Affero General Public License as published by the Free Software
|
||||||
|
Foundation; either version 3, or (at your option) any later version.
|
||||||
|
|
||||||
|
GNU Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License along with
|
||||||
|
GNU Anastasis; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sebastian Javier Marchano (sebasjm)
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { useEffect, useState } from "preact/hooks";
|
||||||
|
import {
|
||||||
|
ObservableMap,
|
||||||
|
browserStorageMap,
|
||||||
|
localStorageMap,
|
||||||
|
memoryMap,
|
||||||
|
} from "../utils/observable.js";
|
||||||
|
|
||||||
|
export interface LocalStorageState {
|
||||||
|
value?: string;
|
||||||
|
update: (s: string) => void;
|
||||||
|
reset: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const storage: ObservableMap<string, string> = memoryMap<string>();
|
||||||
|
|
||||||
|
export function useMemoryStorage(
|
||||||
|
key: string,
|
||||||
|
initialValue: string,
|
||||||
|
): Required<LocalStorageState>;
|
||||||
|
export function useMemoryStorage(key: string): LocalStorageState;
|
||||||
|
export function useMemoryStorage(
|
||||||
|
key: string,
|
||||||
|
initialValue?: string,
|
||||||
|
): LocalStorageState {
|
||||||
|
const [storedValue, setStoredValue] = useState<string | undefined>(
|
||||||
|
(): string | undefined => {
|
||||||
|
return storage.get(key) ?? initialValue;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return storage.onUpdate(key, () => {
|
||||||
|
const newValue = storage.get(key);
|
||||||
|
setStoredValue(newValue ?? initialValue);
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const setValue = (value?: string): void => {
|
||||||
|
if (value === undefined) {
|
||||||
|
storage.delete(key);
|
||||||
|
} else {
|
||||||
|
storage.set(key, value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
value: storedValue,
|
||||||
|
update: setValue,
|
||||||
|
reset: () => {
|
||||||
|
setValue(initialValue);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
67
packages/web-util/src/hooks/useNotifications.ts
Normal file
67
packages/web-util/src/hooks/useNotifications.ts
Normal file
@ -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);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user