mirror of
https://github.com/Qortal/qapp-core.git
synced 2025-06-14 01:21:21 +00:00
added localstorage helper
This commit is contained in:
parent
f2a30ac9ad
commit
36e00390e7
@ -9,6 +9,7 @@ import { base64ToObject } from "../utils/publish";
|
||||
import { generateBloomFilterBase64, isInsideBloom } from "../utils/bloomFilter";
|
||||
import { formatTimestamp } from "../utils/time";
|
||||
import { Toaster } from "react-hot-toast";
|
||||
import { useLocalStorage } from "../hooks/useLocalStorage";
|
||||
|
||||
|
||||
const utils = {
|
||||
@ -29,6 +30,7 @@ interface GlobalContextType {
|
||||
lists: ReturnType<typeof useResources>;
|
||||
appInfo: ReturnType<typeof useAppInfo>;
|
||||
identifierOperations: ReturnType<typeof useIdentifiers>
|
||||
localStorageOperations: ReturnType<typeof useLocalStorage>
|
||||
utils: typeof utils
|
||||
}
|
||||
|
||||
@ -57,9 +59,9 @@ export const GlobalProvider = ({ children, config, toastStyle = {} }: GlobalProv
|
||||
const appInfo = useAppInfo(config.appName, config?.publicSalt)
|
||||
const lists = useResources()
|
||||
const identifierOperations = useIdentifiers(config.publicSalt, config.appName)
|
||||
|
||||
const localStorageOperations = useLocalStorage(config.publicSalt, config.appName)
|
||||
// ✅ Merge all hooks into a single `contextValue`
|
||||
const contextValue = useMemo(() => ({ auth, lists, appInfo, identifierOperations, utils }), [auth, lists, appInfo, identifierOperations]);
|
||||
const contextValue = useMemo(() => ({ auth, lists, appInfo, identifierOperations, utils, localStorageOperations }), [auth, lists, appInfo, identifierOperations, localStorageOperations]);
|
||||
return (
|
||||
<GlobalContext.Provider value={contextValue}>
|
||||
<Toaster
|
||||
|
37
src/hooks/useLocalStorage.tsx
Normal file
37
src/hooks/useLocalStorage.tsx
Normal file
@ -0,0 +1,37 @@
|
||||
import React, { useCallback, useEffect, useMemo } from "react";
|
||||
import { useAppStore } from "../state/app";
|
||||
import { EnumCollisionStrength, hashWord } from "../utils/encryption";
|
||||
|
||||
|
||||
export const useLocalStorage = (publicSalt: string, appName: string) => {
|
||||
|
||||
|
||||
const setTimestamp = useCallback(async (timestamp: number, storageId: string)=> {
|
||||
const hashedString = await hashWord(`${appName}-${storageId}`, EnumCollisionStrength.HIGH, publicSalt)
|
||||
localStorage.setItem(hashedString, JSON.stringify(timestamp));
|
||||
return true
|
||||
}, [appName, publicSalt])
|
||||
|
||||
const getTimestamp = useCallback(async ( storageId: string)=> {
|
||||
const hashedString = await hashWord(`${appName}-${storageId}`, EnumCollisionStrength.HIGH, publicSalt)
|
||||
const stored = localStorage.getItem(hashedString);
|
||||
if(stored){
|
||||
return JSON.parse(stored)
|
||||
} else return null
|
||||
}, [appName, publicSalt])
|
||||
|
||||
const isNewTimestamp = useCallback(async( storageId: string, differenceTimestamp: number)=> {
|
||||
const hashedString = await hashWord(`${appName}-${storageId}`, EnumCollisionStrength.HIGH, publicSalt)
|
||||
const stored = localStorage.getItem(hashedString);
|
||||
if(stored){
|
||||
const storedTimestamp = JSON.parse(stored)
|
||||
return (Date.now() - storedTimestamp) > differenceTimestamp
|
||||
} else return true
|
||||
}, [appName, publicSalt])
|
||||
return {
|
||||
setTimestamp,
|
||||
getTimestamp,
|
||||
isNewTimestamp
|
||||
|
||||
};
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user