refactor(storage): util func moved

This commit is contained in:
enes 2025-01-09 13:18:20 +01:00
parent ed3585f9c8
commit f214d66799
3 changed files with 13 additions and 22 deletions

View File

@ -1,6 +1,7 @@
import React, { useMemo } from 'react' import React, { useMemo } from 'react'
import { import {
getLocalStorageItem, getLocalStorageItem,
mergeWithInitialValue,
removeLocalStorageItem, removeLocalStorageItem,
setLocalStorageItem setLocalStorageItem
} from 'utils' } from 'utils'
@ -10,17 +11,6 @@ const useLocalStorageSubscribe = (callback: () => void) => {
return () => window.removeEventListener('storage', callback) return () => window.removeEventListener('storage', callback)
} }
function mergeWithInitialValue<T>(storedValue: T, initialValue: T): T {
if (
!Array.isArray(storedValue) &&
typeof storedValue === 'object' &&
storedValue !== null
) {
return { ...initialValue, ...storedValue }
}
return storedValue
}
export function useLocalStorage<T>( export function useLocalStorage<T>(
key: string, key: string,
initialValue: T initialValue: T

View File

@ -1,6 +1,7 @@
import React, { useMemo } from 'react' import React, { useMemo } from 'react'
import { import {
getSessionStorageItem, getSessionStorageItem,
mergeWithInitialValue,
removeSessionStorageItem, removeSessionStorageItem,
setSessionStorageItem setSessionStorageItem
} from 'utils' } from 'utils'
@ -10,17 +11,6 @@ const useSessionStorageSubscribe = (callback: () => void) => {
return () => window.removeEventListener('sessionStorage', callback) return () => window.removeEventListener('sessionStorage', callback)
} }
function mergeWithInitialValue<T>(storedValue: T, initialValue: T): T {
if (
!Array.isArray(storedValue) &&
typeof storedValue === 'object' &&
storedValue !== null
) {
return { ...initialValue, ...storedValue }
}
return storedValue
}
export function useSessionStorage<T>( export function useSessionStorage<T>(
key: string, key: string,
initialValue: T initialValue: T

View File

@ -180,3 +180,14 @@ export const getFallbackPubkey = () => {
// Silently ignore // Silently ignore
} }
} }
export function mergeWithInitialValue<T>(storedValue: T, initialValue: T): T {
if (
!Array.isArray(storedValue) &&
typeof storedValue === 'object' &&
storedValue !== null
) {
return { ...initialValue, ...storedValue }
}
return storedValue
}