mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-02 03:28:49 -05:00
e76b7696da
GitOrigin-RevId: d40f942164403a31ffbb10336227eef59a57febf
55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
import { useState, useCallback, useEffect } from 'react'
|
|
import localStorage from '../../infrastructure/local-storage'
|
|
|
|
/**
|
|
* @param {string} key
|
|
* @param {any} [defaultValue]
|
|
* @param {boolean} [listen]
|
|
*
|
|
* @returns {[any, function]}
|
|
*/
|
|
function usePersistedState(key, defaultValue, listen = false) {
|
|
const [value, setValue] = useState(() => {
|
|
return localStorage.getItem(key) ?? defaultValue
|
|
})
|
|
|
|
const updateFunction = useCallback(
|
|
newValue => {
|
|
setValue(value => {
|
|
const actualNewValue =
|
|
typeof newValue === 'function' ? newValue(value) : newValue
|
|
|
|
if (actualNewValue === defaultValue) {
|
|
localStorage.removeItem(key)
|
|
} else {
|
|
localStorage.setItem(key, actualNewValue)
|
|
}
|
|
|
|
return actualNewValue
|
|
})
|
|
},
|
|
[key, defaultValue]
|
|
)
|
|
|
|
useEffect(() => {
|
|
if (listen) {
|
|
const listener = event => {
|
|
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))
|
|
}
|
|
}
|
|
|
|
window.addEventListener('storage', listener)
|
|
|
|
return () => {
|
|
window.removeEventListener('storage', listener)
|
|
}
|
|
}
|
|
}, [key, listen])
|
|
|
|
return [value, updateFunction]
|
|
}
|
|
|
|
export default usePersistedState
|