Cache forms #187

Merged
enes merged 9 commits from 166-caching-fields into staging 2025-01-09 13:45:45 +00:00
3 changed files with 13 additions and 22 deletions
Showing only changes of commit f214d66799 - Show all commits

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
}