Implement 'history state' idea

This commit is contained in:
Marshall Polaris 2022-08-28 20:40:01 -07:00
parent 3b446565cb
commit f643636822

View File

@ -1,4 +1,4 @@
import { useLayoutEffect, useEffect } from 'react' import { useLayoutEffect, useEffect, useState } from 'react'
import { useStateCheckEquality } from './use-state-check-equality' import { useStateCheckEquality } from './use-state-check-equality'
export type PersistenceOptions = { export type PersistenceOptions = {
@ -30,6 +30,38 @@ export const loadState = (key: string, store: Storage) => {
} }
} }
const STATE_KEY = '__manifold'
const getHistoryState = <T>(k: string) => {
if (typeof window !== 'undefined') {
return window.history.state?.options?.[STATE_KEY]?.[k] as T | undefined
} else {
return undefined
}
}
const setHistoryState = (k: string, v: any) => {
if (typeof window !== 'undefined') {
const state = window.history.state ?? {}
const options = state.options ?? {}
const inner = options[STATE_KEY] ?? {}
window.history.replaceState(
{ ...state, options: { ...options, [STATE_KEY]: { ...inner, [k]: v } } },
''
)
}
}
export const useHistoryState = <T>(key: string, initialValue: T) => {
const [state, setState] = useState(getHistoryState<T>(key) ?? initialValue)
const setter = (val: T) => {
console.log('Setting state: ', val)
setHistoryState(key, val)
setState(val)
}
return [state, setter] as const
}
export const usePersistentState = <T>( export const usePersistentState = <T>(
initial: T, initial: T,
persist?: PersistenceOptions persist?: PersistenceOptions