overleaf/services/web/frontend/js/shared/hooks/use-persisted-state.ts
Alf Eaton 5e08d5f505 Merge pull request #8074 from overleaf/ii-ts-strict-fixes-1
GitOrigin-RevId: b99c5c998de9cc36cffffaab5b318331b45e931d
2022-05-27 08:04:04 +00:00

60 lines
1.4 KiB
TypeScript

import {
useState,
useCallback,
useEffect,
SetStateAction,
Dispatch,
} from 'react'
import _ from 'lodash'
import localStorage from '../../infrastructure/local-storage'
function usePersistedState<T>(
key: string,
defaultValue?: T,
listen = false
): [T, Dispatch<SetStateAction<T>>] {
const [value, setValue] = useState<T>(() => {
return localStorage.getItem(key) ?? defaultValue
})
const updateFunction = useCallback(
(newValue: SetStateAction<T>) => {
setValue(value => {
const actualNewValue = _.isFunction(newValue)
? newValue(value)
: newValue
if (actualNewValue === defaultValue) {
localStorage.removeItem(key)
} else {
localStorage.setItem(key, actualNewValue)
}
return actualNewValue
})
},
[key, defaultValue]
)
useEffect(() => {
if (listen) {
const listener = (event: StorageEvent) => {
if (event.key === key) {
// note: this value is read via getItem rather than from event.newValue
// because getItem handles deserializing the JSON that's stored in localStorage.
setValue(localStorage.getItem(key) ?? defaultValue)
}
}
window.addEventListener('storage', listener)
return () => {
window.removeEventListener('storage', listener)
}
}
}, [key, listen, defaultValue])
return [value, updateFunction]
}
export default usePersistedState