2022-04-26 07:26:37 -04:00
|
|
|
import {
|
|
|
|
useState,
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
SetStateAction,
|
|
|
|
Dispatch,
|
|
|
|
} from 'react'
|
2022-05-25 03:57:57 -04:00
|
|
|
import _ from 'lodash'
|
2021-05-18 06:56:56 -04:00
|
|
|
import localStorage from '../../infrastructure/local-storage'
|
2023-11-24 09:54:58 -05:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
|
|
|
|
|
|
|
const safeStringify = (value: unknown) => {
|
|
|
|
try {
|
|
|
|
return JSON.stringify(value)
|
|
|
|
} catch (e) {
|
|
|
|
debugConsole.error('double stringify exception', e)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const safeParse = (value: string) => {
|
|
|
|
try {
|
|
|
|
return JSON.parse(value)
|
|
|
|
} catch (e) {
|
|
|
|
debugConsole.error('double parse exception', e)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
2021-01-07 09:22:40 -05:00
|
|
|
|
2022-05-27 08:32:27 -04:00
|
|
|
function usePersistedState<T = any>(
|
2022-04-26 07:26:37 -04:00
|
|
|
key: string,
|
|
|
|
defaultValue?: T,
|
2023-11-24 09:54:58 -05:00
|
|
|
listen = false,
|
|
|
|
// The option below is for backward compatibility with Angular
|
|
|
|
// which sometimes stringifies the values twice
|
|
|
|
doubleStringifyAndParse = false
|
2022-04-26 07:26:37 -04:00
|
|
|
): [T, Dispatch<SetStateAction<T>>] {
|
2023-11-24 09:54:58 -05:00
|
|
|
const getItem = useCallback(
|
|
|
|
(key: string) => {
|
|
|
|
const item = localStorage.getItem(key)
|
|
|
|
return doubleStringifyAndParse ? safeParse(item) : item
|
|
|
|
},
|
|
|
|
[doubleStringifyAndParse]
|
|
|
|
)
|
|
|
|
const setItem = useCallback(
|
|
|
|
(key: string, value: unknown) => {
|
|
|
|
const val = doubleStringifyAndParse ? safeStringify(value) : value
|
|
|
|
localStorage.setItem(key, val)
|
|
|
|
},
|
|
|
|
[doubleStringifyAndParse]
|
|
|
|
)
|
|
|
|
|
2022-04-26 07:26:37 -04:00
|
|
|
const [value, setValue] = useState<T>(() => {
|
2023-11-24 09:54:58 -05:00
|
|
|
return getItem(key) ?? defaultValue
|
2021-01-07 09:22:40 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
const updateFunction = useCallback(
|
2022-05-25 03:57:57 -04:00
|
|
|
(newValue: SetStateAction<T>) => {
|
2021-09-23 06:35:50 -04:00
|
|
|
setValue(value => {
|
2022-05-25 03:57:57 -04:00
|
|
|
const actualNewValue = _.isFunction(newValue)
|
|
|
|
? newValue(value)
|
|
|
|
: newValue
|
2021-09-23 06:35:50 -04:00
|
|
|
|
|
|
|
if (actualNewValue === defaultValue) {
|
|
|
|
localStorage.removeItem(key)
|
|
|
|
} else {
|
2023-11-24 09:54:58 -05:00
|
|
|
setItem(key, actualNewValue)
|
2021-09-23 06:35:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return actualNewValue
|
|
|
|
})
|
2021-01-07 09:22:40 -05:00
|
|
|
},
|
2023-11-24 09:54:58 -05:00
|
|
|
[key, defaultValue, setItem]
|
2021-01-07 09:22:40 -05:00
|
|
|
)
|
|
|
|
|
2021-09-29 07:26:00 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (listen) {
|
2022-05-26 05:39:07 -04:00
|
|
|
const listener = (event: StorageEvent) => {
|
2021-09-29 07:26:00 -04:00
|
|
|
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.
|
2023-11-24 09:54:58 -05:00
|
|
|
setValue(getItem(key) ?? defaultValue)
|
2021-09-29 07:26:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('storage', listener)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('storage', listener)
|
|
|
|
}
|
|
|
|
}
|
2023-11-24 09:54:58 -05:00
|
|
|
}, [defaultValue, key, listen, getItem])
|
2021-09-29 07:26:00 -04:00
|
|
|
|
2021-01-07 09:22:40 -05:00
|
|
|
return [value, updateFunction]
|
|
|
|
}
|
|
|
|
|
|
|
|
export default usePersistedState
|