@willbooster/shared-lib-react 3.3.11 → 3.3.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"useInterval.cjs","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\n/**\n * A custom React hook that sets up an interval and returns a function to clear it.\n *\n * This hook ensures that the latest callback is always used, even if it changes between renders.\n * The interval can be dynamically started, stopped, or adjusted by changing the `milliseconds` parameter.\n *\n * @param callback - The function to be called at each interval. It is guaranteed to be the most recent version.\n * @param milliseconds - The interval duration in milliseconds. If undefined, the interval is cleared.\n * @returns A function to manually clear the interval.\n *\n * @example\n * const clearInterval = useInterval(() => {\n * console.log('This will run every 1000ms');\n * }, 1000);\n *\n * // To manually stop the interval:\n * clearInterval();\n */\nexport function useInterval(callback: () => void, milliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.Timer>(undefined);\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n\n const clearInterval = useCallback(() => {\n globalThis.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n if (milliseconds !== undefined) {\n timerId.current = globalThis.setInterval(() => callbackRef.current(), milliseconds);\n }\n return clearInterval;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [milliseconds]);\n\n return clearInterval;\n}\n"],"names":["callback","milliseconds","timerId","useRef","undefined","callbackRef","current","clearInterval","useCallback","globalThis","useEffect","setInterval"],"mappings":"wDAoBO,SAAqBA,EAAsBC,GAChD,MAAMC,EAAUC,EAAMA,YAAwBC,GACxCC,EAAcF,EAAMA,OAACH,GAC3BK,EAAYC,QAAUN,EAEtB,MAAMO,EAAgBC,EAAAA,aAAY,KAChCC,WAAWF,cAAcL,EAAQI,QAAQ,GACxC,IAUH,OARAI,EAAAA,WAAU,UACaN,IAAjBH,IACFC,EAAQI,QAAUG,WAAWE,aAAY,IAAMN,EAAYC,WAAWL,IAEjEM,IAEN,CAACN,IAEGM,CACT"}
1
+ {"version":3,"file":"useInterval.cjs","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\n/**\n * A custom React hook that sets up an interval and returns a function to clear it.\n *\n * This hook ensures that the latest callback is always used, even if it changes between renders.\n * The interval can be dynamically started, stopped, or adjusted by changing the `milliseconds` parameter.\n *\n * @param callback - The function to be called at each interval. It is guaranteed to be the most recent version.\n * @param milliseconds - The interval duration in milliseconds. If undefined, the interval is cleared.\n * @returns A function to manually clear the interval.\n *\n * @example\n * const clearInterval = useInterval(() => {\n * console.log('This will run every 1000ms');\n * }, 1000);\n *\n * // To manually stop the interval:\n * clearInterval();\n */\nexport function useInterval(callback: () => void, milliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.Timer>(undefined);\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n\n const clearInterval = useCallback(() => {\n globalThis.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n if (milliseconds !== undefined) {\n timerId.current = globalThis.setInterval(() => callbackRef.current(), milliseconds);\n }\n return clearInterval;\n }, [milliseconds]);\n\n return clearInterval;\n}\n"],"names":["callback","milliseconds","timerId","useRef","undefined","callbackRef","current","clearInterval","useCallback","globalThis","useEffect","setInterval"],"mappings":"wDAoBO,SAAqBA,EAAsBC,GAChD,MAAMC,EAAUC,EAAMA,YAAwBC,GACxCC,EAAcF,EAAMA,OAACH,GAC3BK,EAAYC,QAAUN,EAEtB,MAAMO,EAAgBC,EAAAA,aAAY,KAChCC,WAAWF,cAAcL,EAAQI,QAAQ,GACxC,IASH,OAPAI,EAAAA,WAAU,UACaN,IAAjBH,IACFC,EAAQI,QAAUG,WAAWE,aAAY,IAAMN,EAAYC,WAAWL,IAEjEM,IACN,CAACN,IAEGM,CACT"}
@@ -1 +1 @@
1
- {"version":3,"file":"useInterval.js","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\n/**\n * A custom React hook that sets up an interval and returns a function to clear it.\n *\n * This hook ensures that the latest callback is always used, even if it changes between renders.\n * The interval can be dynamically started, stopped, or adjusted by changing the `milliseconds` parameter.\n *\n * @param callback - The function to be called at each interval. It is guaranteed to be the most recent version.\n * @param milliseconds - The interval duration in milliseconds. If undefined, the interval is cleared.\n * @returns A function to manually clear the interval.\n *\n * @example\n * const clearInterval = useInterval(() => {\n * console.log('This will run every 1000ms');\n * }, 1000);\n *\n * // To manually stop the interval:\n * clearInterval();\n */\nexport function useInterval(callback: () => void, milliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.Timer>(undefined);\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n\n const clearInterval = useCallback(() => {\n globalThis.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n if (milliseconds !== undefined) {\n timerId.current = globalThis.setInterval(() => callbackRef.current(), milliseconds);\n }\n return clearInterval;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [milliseconds]);\n\n return clearInterval;\n}\n"],"names":["useInterval","callback","milliseconds","timerId","useRef","undefined","callbackRef","current","clearInterval","useCallback","globalThis","useEffect","setInterval"],"mappings":"+DAoBO,SAASA,EAAYC,EAAsBC,GAChD,MAAMC,EAAUC,OAA8BC,GACxCC,EAAcF,EAAOH,GAC3BK,EAAYC,QAAUN,EAEtB,MAAMO,EAAgBC,GAAY,KAChCC,WAAWF,cAAcL,EAAQI,QAAQ,GACxC,IAUH,OARAI,GAAU,UACaN,IAAjBH,IACFC,EAAQI,QAAUG,WAAWE,aAAY,IAAMN,EAAYC,WAAWL,IAEjEM,IAEN,CAACN,IAEGM,CACT"}
1
+ {"version":3,"file":"useInterval.js","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\n/**\n * A custom React hook that sets up an interval and returns a function to clear it.\n *\n * This hook ensures that the latest callback is always used, even if it changes between renders.\n * The interval can be dynamically started, stopped, or adjusted by changing the `milliseconds` parameter.\n *\n * @param callback - The function to be called at each interval. It is guaranteed to be the most recent version.\n * @param milliseconds - The interval duration in milliseconds. If undefined, the interval is cleared.\n * @returns A function to manually clear the interval.\n *\n * @example\n * const clearInterval = useInterval(() => {\n * console.log('This will run every 1000ms');\n * }, 1000);\n *\n * // To manually stop the interval:\n * clearInterval();\n */\nexport function useInterval(callback: () => void, milliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.Timer>(undefined);\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n\n const clearInterval = useCallback(() => {\n globalThis.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n if (milliseconds !== undefined) {\n timerId.current = globalThis.setInterval(() => callbackRef.current(), milliseconds);\n }\n return clearInterval;\n }, [milliseconds]);\n\n return clearInterval;\n}\n"],"names":["useInterval","callback","milliseconds","timerId","useRef","undefined","callbackRef","current","clearInterval","useCallback","globalThis","useEffect","setInterval"],"mappings":"+DAoBO,SAASA,EAAYC,EAAsBC,GAChD,MAAMC,EAAUC,OAA8BC,GACxCC,EAAcF,EAAOH,GAC3BK,EAAYC,QAAUN,EAEtB,MAAMO,EAAgBC,GAAY,KAChCC,WAAWF,cAAcL,EAAQI,QAAQ,GACxC,IASH,OAPAI,GAAU,UACaN,IAAjBH,IACFC,EAAQI,QAAUG,WAAWE,aAAY,IAAMN,EAAYC,WAAWL,IAEjEM,IACN,CAACN,IAEGM,CACT"}
@@ -1 +1 @@
1
- {"version":3,"file":"useStorage.cjs","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport type UseStorageOptions<T> =\n | {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText: string;\n }\n | { parseAfterJsonParse?: (value: unknown) => T };\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValue: T,\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n (callback) => {\n const newCallback = (event: StorageEvent): void => {\n if (event.key === key) callback();\n };\n globalThis.addEventListener('storage', newCallback);\n return () => globalThis.removeEventListener('storage', newCallback);\n },\n () => window[nonReactiveStorageType].getItem(key),\n () => 'ssrJsonText' in nonReactiveOptions && nonReactiveOptions.ssrJsonText\n );\n const value = useMemo(() => {\n try {\n if (jsonText) {\n const json = JSON.parse(jsonText);\n return nonReactiveOptions?.parseAfterJsonParse ? nonReactiveOptions?.parseAfterJsonParse(json) : json;\n }\n } catch {\n // do nothing\n }\n return initialValue;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [jsonText, initialValue]);\n\n const setState = useCallback(\n (valueOrFunc: T | ((prevState: T) => T)) => {\n const nextState = typeof valueOrFunc === 'function' ? (valueOrFunc as (prevState: T) => T)(value) : valueOrFunc;\n\n // eslint-disable-next-line unicorn/no-null\n let newValue: string | null = null;\n if (nextState === undefined || nextState === null) {\n window[nonReactiveStorageType].removeItem(key);\n } else {\n newValue = JSON.stringify(nextState);\n window[nonReactiveStorageType].setItem(key, newValue);\n }\n globalThis.dispatchEvent(new StorageEvent('storage', { key, newValue }));\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [key, value]\n );\n\n useEffect(() => {\n if (window[nonReactiveStorageType].getItem(key) === null && initialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(initialValue));\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [key, initialValue]);\n\n return [value, setState];\n}\n"],"names":["nonReactiveStorageType","key","initialValue","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","globalThis","addEventListener","removeEventListener","window","getItem","ssrJsonText","value","useMemo","json","JSON","parse","parseAfterJsonParse","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","undefined"],"mappings":"uDAUO,SACLA,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,EAAoBA,sBAClCC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,GAAU,EAGnC,OADAG,WAAWC,iBAAiB,UAAWH,GAChC,IAAME,WAAWE,oBAAoB,UAAWJ,EAAY,IAErE,IAAMK,OAAOZ,GAAwBa,QAAQZ,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBW,cAE5DC,EAAQC,EAAAA,SAAQ,KACpB,IACE,GAAIZ,EAAU,CACZ,MAAMa,EAAOC,KAAKC,MAAMf,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBiB,oBAAsBjB,MAAAA,OAAAA,EAAAA,EAAoBiB,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,OAAOnB,CAAY,GAElB,CAACE,EAAUF,IAERoB,EAAWC,EAAWA,aACzBC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAGpG,IAAIE,EAA0B,KAC1BD,QACFb,OAAOZ,GAAwB2B,WAAW1B,IAE1CyB,EAAWR,KAAKU,UAAUH,GAC1Bb,OAAOZ,GAAwB6B,QAAQ5B,EAAKyB,IAE9CjB,WAAWqB,cAAc,IAAIC,aAAa,UAAW,CAAE9B,MAAKyB,aAAY,GAG1E,CAACzB,EAAKc,IAUR,OAPAiB,EAAAA,WAAU,KAC4C,OAAhDpB,OAAOZ,GAAwBa,QAAQZ,SAAkCgC,IAAjB/B,GAC1DU,OAAOZ,GAAwB6B,QAAQ5B,EAAKiB,KAAKU,UAAU1B,GAC7D,GAEC,CAACD,EAAKC,IAEF,CAACa,EAAOO,EACjB"}
1
+ {"version":3,"file":"useStorage.cjs","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport type UseStorageOptions<T> =\n | {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText: string;\n }\n | { parseAfterJsonParse?: (value: unknown) => T };\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValue: T,\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n (callback) => {\n const newCallback = (event: StorageEvent): void => {\n if (event.key === key) callback();\n };\n globalThis.addEventListener('storage', newCallback);\n return () => globalThis.removeEventListener('storage', newCallback);\n },\n () => window[nonReactiveStorageType].getItem(key),\n () => 'ssrJsonText' in nonReactiveOptions && nonReactiveOptions.ssrJsonText\n );\n const value = useMemo(() => {\n try {\n if (jsonText) {\n const json = JSON.parse(jsonText) as unknown as T;\n return nonReactiveOptions?.parseAfterJsonParse ? nonReactiveOptions?.parseAfterJsonParse(json) : json;\n }\n } catch {\n // do nothing\n }\n return initialValue;\n }, [jsonText, initialValue]);\n\n const setState = useCallback(\n (valueOrFunc: T | ((prevState: T) => T)) => {\n const nextState = typeof valueOrFunc === 'function' ? (valueOrFunc as (prevState: T) => T)(value) : valueOrFunc;\n\n // eslint-disable-next-line unicorn/no-null\n let newValue: string | null = null;\n if (nextState === undefined || nextState === null) {\n window[nonReactiveStorageType].removeItem(key);\n } else {\n newValue = JSON.stringify(nextState);\n window[nonReactiveStorageType].setItem(key, newValue);\n }\n globalThis.dispatchEvent(new StorageEvent('storage', { key, newValue }));\n },\n\n [key, value]\n );\n\n useEffect(() => {\n if (window[nonReactiveStorageType].getItem(key) === null && initialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(initialValue));\n }\n }, [key, initialValue]);\n\n return [value, setState];\n}\n"],"names":["nonReactiveStorageType","key","initialValue","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","globalThis","addEventListener","removeEventListener","window","getItem","ssrJsonText","value","useMemo","json","JSON","parse","parseAfterJsonParse","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","undefined"],"mappings":"uDAUO,SACLA,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,EAAoBA,sBAClCC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,GAAU,EAGnC,OADAG,WAAWC,iBAAiB,UAAWH,GAChC,IAAME,WAAWE,oBAAoB,UAAWJ,EAAY,IAErE,IAAMK,OAAOZ,GAAwBa,QAAQZ,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBW,cAE5DC,EAAQC,EAAAA,SAAQ,KACpB,IACE,GAAIZ,EAAU,CACZ,MAAMa,EAAOC,KAAKC,MAAMf,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBiB,oBAAsBjB,MAAAA,OAAAA,EAAAA,EAAoBiB,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,OAAOnB,CAAY,GAClB,CAACE,EAAUF,IAERoB,EAAWC,EAAWA,aACzBC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAGpG,IAAIE,EAA0B,KAC1BD,QACFb,OAAOZ,GAAwB2B,WAAW1B,IAE1CyB,EAAWR,KAAKU,UAAUH,GAC1Bb,OAAOZ,GAAwB6B,QAAQ5B,EAAKyB,IAE9CjB,WAAWqB,cAAc,IAAIC,aAAa,UAAW,CAAE9B,MAAKyB,aAAY,GAG1E,CAACzB,EAAKc,IASR,OANAiB,EAAAA,WAAU,KAC4C,OAAhDpB,OAAOZ,GAAwBa,QAAQZ,SAAkCgC,IAAjB/B,GAC1DU,OAAOZ,GAAwB6B,QAAQ5B,EAAKiB,KAAKU,UAAU1B,GAC7D,GACC,CAACD,EAAKC,IAEF,CAACa,EAAOO,EACjB"}
@@ -1 +1 @@
1
- {"version":3,"file":"useStorage.js","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport type UseStorageOptions<T> =\n | {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText: string;\n }\n | { parseAfterJsonParse?: (value: unknown) => T };\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValue: T,\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n (callback) => {\n const newCallback = (event: StorageEvent): void => {\n if (event.key === key) callback();\n };\n globalThis.addEventListener('storage', newCallback);\n return () => globalThis.removeEventListener('storage', newCallback);\n },\n () => window[nonReactiveStorageType].getItem(key),\n () => 'ssrJsonText' in nonReactiveOptions && nonReactiveOptions.ssrJsonText\n );\n const value = useMemo(() => {\n try {\n if (jsonText) {\n const json = JSON.parse(jsonText);\n return nonReactiveOptions?.parseAfterJsonParse ? nonReactiveOptions?.parseAfterJsonParse(json) : json;\n }\n } catch {\n // do nothing\n }\n return initialValue;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [jsonText, initialValue]);\n\n const setState = useCallback(\n (valueOrFunc: T | ((prevState: T) => T)) => {\n const nextState = typeof valueOrFunc === 'function' ? (valueOrFunc as (prevState: T) => T)(value) : valueOrFunc;\n\n // eslint-disable-next-line unicorn/no-null\n let newValue: string | null = null;\n if (nextState === undefined || nextState === null) {\n window[nonReactiveStorageType].removeItem(key);\n } else {\n newValue = JSON.stringify(nextState);\n window[nonReactiveStorageType].setItem(key, newValue);\n }\n globalThis.dispatchEvent(new StorageEvent('storage', { key, newValue }));\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [key, value]\n );\n\n useEffect(() => {\n if (window[nonReactiveStorageType].getItem(key) === null && initialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(initialValue));\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [key, initialValue]);\n\n return [value, setState];\n}\n"],"names":["useStorage","nonReactiveStorageType","key","initialValue","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","globalThis","addEventListener","removeEventListener","window","getItem","ssrJsonText","value","useMemo","json","JSON","parse","parseAfterJsonParse","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","undefined"],"mappings":"0FAUO,SAASA,EACdC,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,GACdC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,GAAU,EAGnC,OADAG,WAAWC,iBAAiB,UAAWH,GAChC,IAAME,WAAWE,oBAAoB,UAAWJ,EAAY,IAErE,IAAMK,OAAOZ,GAAwBa,QAAQZ,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBW,cAE5DC,EAAQC,GAAQ,KACpB,IACE,GAAIZ,EAAU,CACZ,MAAMa,EAAOC,KAAKC,MAAMf,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBiB,oBAAsBjB,MAAAA,OAAAA,EAAAA,EAAoBiB,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,OAAOnB,CAAY,GAElB,CAACE,EAAUF,IAERoB,EAAWC,GACdC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAGpG,IAAIE,EAA0B,KAC1BD,QACFb,OAAOZ,GAAwB2B,WAAW1B,IAE1CyB,EAAWR,KAAKU,UAAUH,GAC1Bb,OAAOZ,GAAwB6B,QAAQ5B,EAAKyB,IAE9CjB,WAAWqB,cAAc,IAAIC,aAAa,UAAW,CAAE9B,MAAKyB,aAAY,GAG1E,CAACzB,EAAKc,IAUR,OAPAiB,GAAU,KAC4C,OAAhDpB,OAAOZ,GAAwBa,QAAQZ,SAAkCgC,IAAjB/B,GAC1DU,OAAOZ,GAAwB6B,QAAQ5B,EAAKiB,KAAKU,UAAU1B,GAC7D,GAEC,CAACD,EAAKC,IAEF,CAACa,EAAOO,EACjB"}
1
+ {"version":3,"file":"useStorage.js","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport type UseStorageOptions<T> =\n | {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText: string;\n }\n | { parseAfterJsonParse?: (value: unknown) => T };\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValue: T,\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n (callback) => {\n const newCallback = (event: StorageEvent): void => {\n if (event.key === key) callback();\n };\n globalThis.addEventListener('storage', newCallback);\n return () => globalThis.removeEventListener('storage', newCallback);\n },\n () => window[nonReactiveStorageType].getItem(key),\n () => 'ssrJsonText' in nonReactiveOptions && nonReactiveOptions.ssrJsonText\n );\n const value = useMemo(() => {\n try {\n if (jsonText) {\n const json = JSON.parse(jsonText) as unknown as T;\n return nonReactiveOptions?.parseAfterJsonParse ? nonReactiveOptions?.parseAfterJsonParse(json) : json;\n }\n } catch {\n // do nothing\n }\n return initialValue;\n }, [jsonText, initialValue]);\n\n const setState = useCallback(\n (valueOrFunc: T | ((prevState: T) => T)) => {\n const nextState = typeof valueOrFunc === 'function' ? (valueOrFunc as (prevState: T) => T)(value) : valueOrFunc;\n\n // eslint-disable-next-line unicorn/no-null\n let newValue: string | null = null;\n if (nextState === undefined || nextState === null) {\n window[nonReactiveStorageType].removeItem(key);\n } else {\n newValue = JSON.stringify(nextState);\n window[nonReactiveStorageType].setItem(key, newValue);\n }\n globalThis.dispatchEvent(new StorageEvent('storage', { key, newValue }));\n },\n\n [key, value]\n );\n\n useEffect(() => {\n if (window[nonReactiveStorageType].getItem(key) === null && initialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(initialValue));\n }\n }, [key, initialValue]);\n\n return [value, setState];\n}\n"],"names":["useStorage","nonReactiveStorageType","key","initialValue","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","globalThis","addEventListener","removeEventListener","window","getItem","ssrJsonText","value","useMemo","json","JSON","parse","parseAfterJsonParse","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","undefined"],"mappings":"0FAUO,SAASA,EACdC,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,GACdC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,GAAU,EAGnC,OADAG,WAAWC,iBAAiB,UAAWH,GAChC,IAAME,WAAWE,oBAAoB,UAAWJ,EAAY,IAErE,IAAMK,OAAOZ,GAAwBa,QAAQZ,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBW,cAE5DC,EAAQC,GAAQ,KACpB,IACE,GAAIZ,EAAU,CACZ,MAAMa,EAAOC,KAAKC,MAAMf,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBiB,oBAAsBjB,MAAAA,OAAAA,EAAAA,EAAoBiB,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,OAAOnB,CAAY,GAClB,CAACE,EAAUF,IAERoB,EAAWC,GACdC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAGpG,IAAIE,EAA0B,KAC1BD,QACFb,OAAOZ,GAAwB2B,WAAW1B,IAE1CyB,EAAWR,KAAKU,UAAUH,GAC1Bb,OAAOZ,GAAwB6B,QAAQ5B,EAAKyB,IAE9CjB,WAAWqB,cAAc,IAAIC,aAAa,UAAW,CAAE9B,MAAKyB,aAAY,GAG1E,CAACzB,EAAKc,IASR,OANAiB,GAAU,KAC4C,OAAhDpB,OAAOZ,GAAwBa,QAAQZ,SAAkCgC,IAAjB/B,GAC1DU,OAAOZ,GAAwB6B,QAAQ5B,EAAKiB,KAAKU,UAAU1B,GAC7D,GACC,CAACD,EAAKC,IAEF,CAACa,EAAOO,EACjB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/shared-lib-react",
3
- "version": "3.3.11",
3
+ "version": "3.3.13",
4
4
  "license": "Apache-2.0",
5
5
  "author": "WillBooster Inc.",
6
6
  "sideEffects": false,
@@ -22,9 +22,10 @@
22
22
  "scripts": {
23
23
  "build": "build-ts lib",
24
24
  "build-storybook": "build-storybook",
25
+ "check-all": "yarn cleanup && yarn typecheck && yarn test",
25
26
  "cleanup": "yarn format && yarn lint-fix",
26
27
  "format": "sort-package-json && yarn prettify",
27
- "lint": "eslint --color \"./{scripts,src,tests}/**/*.{cjs,cts,js,jsx,mjs,mts,ts,tsx}\"",
28
+ "lint": "eslint --color",
28
29
  "lint-fix": "yarn lint --fix --rule \"{ react-hooks/exhaustive-deps: 0 }\"",
29
30
  "prettify": "prettier --cache --color --write \"**/{.*/,}*.{cjs,css,cts,htm,html,js,json,json5,jsonc,jsx,md,mjs,mts,scss,ts,tsx,vue,yaml,yml}\" \"!**/test-fixtures/**\"",
30
31
  "storybook": "start-storybook -p 6006",
@@ -46,31 +47,33 @@
46
47
  "@storybook/testing-library": "0.2.2",
47
48
  "@types/eslint": "8.56.10",
48
49
  "@types/micromatch": "4.0.9",
49
- "@types/react": "19.1.1",
50
+ "@types/react": "19.1.2",
50
51
  "@types/react-dom": "19.1.2",
51
- "@typescript-eslint/eslint-plugin": "8.29.1",
52
- "@typescript-eslint/parser": "8.29.1",
53
- "@willbooster/eslint-config-ts-react": "10.2.1",
54
- "@willbooster/prettier-config": "9.1.3",
55
- "babel-loader": "9.2.1",
56
- "build-ts": "13.1.37",
57
- "eslint": "8.57.0",
52
+ "@willbooster/eslint-config-ts-react": "11.2.0",
53
+ "@willbooster/prettier-config": "10.0.0",
54
+ "babel-loader": "10.0.0",
55
+ "build-ts": "13.1.39",
56
+ "eslint": "9.25.0",
57
+ "eslint-config-flat-gitignore": "2.1.0",
58
58
  "eslint-config-prettier": "10.1.2",
59
- "eslint-import-resolver-typescript": "3.10.0",
60
- "eslint-plugin-import": "2.31.0",
59
+ "eslint-import-resolver-typescript": "4.3.2",
60
+ "eslint-plugin-import-x": "4.10.5",
61
61
  "eslint-plugin-react": "7.37.5",
62
62
  "eslint-plugin-react-hooks": "5.2.0",
63
63
  "eslint-plugin-sort-class-members": "1.21.0",
64
64
  "eslint-plugin-sort-destructure-keys": "2.0.0",
65
65
  "eslint-plugin-storybook": "0.12.0",
66
- "eslint-plugin-unicorn": "56.0.1",
66
+ "eslint-plugin-unicorn": "58.0.0",
67
+ "eslint-plugin-unused-imports": "4.1.4",
68
+ "globals": "16.0.0",
67
69
  "lint-staged": "15.5.1",
68
70
  "micromatch": "4.0.8",
69
71
  "prettier": "3.5.3",
70
72
  "react": "19.1.0",
71
73
  "react-dom": "19.1.0",
72
- "sort-package-json": "2.15.1",
74
+ "sort-package-json": "3.0.0",
73
75
  "typescript": "5.8.3",
76
+ "typescript-eslint": "8.30.1",
74
77
  "vitest": "3.1.1"
75
78
  },
76
79
  "peerDependencies": {