Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import { safeJsonParse } from 'common/util/json'
|
|
import { useEffect, useState } from 'react'
|
|
import { safeLocalStorage } from 'web/lib/util/local'
|
|
import { isFunction } from 'client-common/hooks/use-persistent-in-memory-state'
|
|
import { useEvent } from 'client-common/hooks/use-event'
|
|
import { useIsClient } from 'web/hooks/use-is-client'
|
|
|
|
export const usePersistentLocalState = <T>(initialValue: T, key: string) => {
|
|
const isClient = useIsClient()
|
|
const [state, setState] = useState<T>(
|
|
(isClient && safeJsonParse(safeLocalStorage?.getItem(key))) || initialValue
|
|
)
|
|
const [ready, setReady] = useState(false)
|
|
const saveState = useEvent((newState: T | ((prevState: T) => T)) => {
|
|
setState((prevState: T) => {
|
|
const updatedState = isFunction(newState) ? newState(prevState) : newState
|
|
setPersistentLocalState(key, updatedState)
|
|
return updatedState
|
|
})
|
|
})
|
|
|
|
useEffect(() => {
|
|
// Set initial state.
|
|
if (safeLocalStorage) {
|
|
const storedJson = getPersistentLocalState(key)
|
|
const storedValue = storedJson == null ? initialValue : storedJson
|
|
setState(storedValue as T)
|
|
setReady(true)
|
|
}
|
|
}, [key])
|
|
|
|
return [state, saveState, ready] as const
|
|
}
|
|
|
|
export const getPersistentLocalState = (key: string) => {
|
|
if (safeLocalStorage) {
|
|
return safeJsonParse(safeLocalStorage.getItem(key))
|
|
} else {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export const setPersistentLocalState = <T>(key: string, value: T) => {
|
|
safeLocalStorage?.setItem(key, JSON.stringify(value))
|
|
}
|