From f214d667993cf886ebfb2d8156f65b47fe27375e Mon Sep 17 00:00:00 2001 From: enes Date: Thu, 9 Jan 2025 13:18:20 +0100 Subject: [PATCH] refactor(storage): util func moved --- src/hooks/useLocalStorage.tsx | 12 +----------- src/hooks/useSessionStorage.tsx | 12 +----------- src/utils/utils.ts | 11 +++++++++++ 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/hooks/useLocalStorage.tsx b/src/hooks/useLocalStorage.tsx index 10579cd..da1c641 100644 --- a/src/hooks/useLocalStorage.tsx +++ b/src/hooks/useLocalStorage.tsx @@ -1,6 +1,7 @@ import React, { useMemo } from 'react' import { getLocalStorageItem, + mergeWithInitialValue, removeLocalStorageItem, setLocalStorageItem } from 'utils' @@ -10,17 +11,6 @@ const useLocalStorageSubscribe = (callback: () => void) => { return () => window.removeEventListener('storage', callback) } -function mergeWithInitialValue(storedValue: T, initialValue: T): T { - if ( - !Array.isArray(storedValue) && - typeof storedValue === 'object' && - storedValue !== null - ) { - return { ...initialValue, ...storedValue } - } - return storedValue -} - export function useLocalStorage( key: string, initialValue: T diff --git a/src/hooks/useSessionStorage.tsx b/src/hooks/useSessionStorage.tsx index cc0756f..ca194e6 100644 --- a/src/hooks/useSessionStorage.tsx +++ b/src/hooks/useSessionStorage.tsx @@ -1,6 +1,7 @@ import React, { useMemo } from 'react' import { getSessionStorageItem, + mergeWithInitialValue, removeSessionStorageItem, setSessionStorageItem } from 'utils' @@ -10,17 +11,6 @@ const useSessionStorageSubscribe = (callback: () => void) => { return () => window.removeEventListener('sessionStorage', callback) } -function mergeWithInitialValue(storedValue: T, initialValue: T): T { - if ( - !Array.isArray(storedValue) && - typeof storedValue === 'object' && - storedValue !== null - ) { - return { ...initialValue, ...storedValue } - } - return storedValue -} - export function useSessionStorage( key: string, initialValue: T diff --git a/src/utils/utils.ts b/src/utils/utils.ts index e28027d..2f19e8e 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -180,3 +180,14 @@ export const getFallbackPubkey = () => { // Silently ignore } } + +export function mergeWithInitialValue(storedValue: T, initialValue: T): T { + if ( + !Array.isArray(storedValue) && + typeof storedValue === 'object' && + storedValue !== null + ) { + return { ...initialValue, ...storedValue } + } + return storedValue +}