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>
69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import { useRouter } from 'next/router'
|
|
import { useEffect, useState } from 'react'
|
|
import { pickBy, debounce, mapValues } from 'lodash'
|
|
import { usePersistentInMemoryState } from 'client-common/hooks/use-persistent-in-memory-state'
|
|
|
|
type UrlParams = Record<string, string | undefined>
|
|
|
|
// for updating multiple query params
|
|
export const usePersistentQueriesState = <T extends UrlParams>(
|
|
defaultValue: T,
|
|
persistPrefix: string
|
|
): [T, (newState: Partial<T>) => void, boolean] => {
|
|
const [state, setState] = usePersistentInMemoryState(
|
|
defaultValue,
|
|
`${persistPrefix}-queries-state`
|
|
)
|
|
|
|
const router = useRouter()
|
|
const [ready, setReady] = useState(false)
|
|
|
|
// On page load, initialize the state to the current query params once.
|
|
useEffect(() => {
|
|
if (router.isReady) {
|
|
setState({
|
|
...defaultValue,
|
|
...mapValues(router.query, (v) =>
|
|
typeof v === 'string' ? decodeURIComponent(v) : v
|
|
),
|
|
})
|
|
setReady(true)
|
|
}
|
|
}, [router.isReady])
|
|
|
|
const setRouteQuery = debounce((newQuery: string) => {
|
|
const { pathname } = router
|
|
const q = newQuery ? '?' + newQuery : ''
|
|
router.replace(pathname + q, undefined, { shallow: true })
|
|
}, 200)
|
|
|
|
const updateState = (update: Partial<T>) => {
|
|
// Include current query because it might have been updated by another component.
|
|
const newState = { ...state, ...router.query, ...update } as T
|
|
setState(newState)
|
|
const query = pickBy(newState, (v) => v)
|
|
const newQuery = Object.entries(query)
|
|
.map(([key, val]) => `${key}=${encodeURIComponent(val!)}`)
|
|
.join('&')
|
|
setRouteQuery(newQuery)
|
|
}
|
|
|
|
return [state, updateState, ready]
|
|
}
|
|
|
|
export const usePersistentQueryState = <K extends string>(
|
|
key: K,
|
|
defaultValue: string
|
|
): [string | undefined, (newState: string) => void] => {
|
|
const [state, updateState] = usePersistentQueriesState(
|
|
{
|
|
[key]: defaultValue,
|
|
},
|
|
''
|
|
)
|
|
return [
|
|
state ? state[key] : undefined,
|
|
(newState: string) => updateState({ [key]: newState }),
|
|
]
|
|
}
|