@willbooster/shared-lib-react 3.2.8 → 3.2.10

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,2 +1,2 @@
1
- "use strict";var e=require("react");exports.useInterval=function(r,t){const n=e.useRef(),u=e.useCallback((()=>{window.clearInterval(n.current)}),[]);return e.useEffect((()=>(n.current=window.setInterval(r,t),u)),[t]),u};
1
+ "use strict";var e=require("react");exports.useInterval=function(r,t){const s=e.useRef(),a=e.useCallback((()=>{globalThis.clearInterval(s.current)}),[]);return e.useEffect((()=>(void 0!==t&&(s.current=globalThis.setInterval(r,t)),a)),[t]),a};
2
2
  //# sourceMappingURL=useInterval.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"useInterval.cjs","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\nexport function useInterval(nonReactiveCallback: () => void, milliseconds: number): () => void {\n const timerId = useRef<number>();\n\n const clearInterval = useCallback(() => {\n window.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n timerId.current = window.setInterval(nonReactiveCallback, milliseconds);\n return clearInterval;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [milliseconds]);\n\n return clearInterval;\n}\n"],"names":["nonReactiveCallback","milliseconds","timerId","useRef","clearInterval","useCallback","window","current","useEffect","setInterval"],"mappings":"wDAEO,SAAqBA,EAAiCC,GAC3D,MAAMC,EAAUC,EAAAA,SAEVC,EAAgBC,EAAAA,aAAY,KAChCC,OAAOF,cAAcF,EAAQK,QAAQ,GACpC,IAQH,OANAC,EAAAA,WAAU,KACRN,EAAQK,QAAUD,OAAOG,YAAYT,EAAqBC,GACnDG,IAEN,CAACH,IAEGG,CACT"}
1
+ {"version":3,"file":"useInterval.cjs","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\nexport function useInterval(nonReactiveCallback: () => void, reactiveMilliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.Timeout>();\n\n const clearInterval = useCallback(() => {\n globalThis.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n if (reactiveMilliseconds !== undefined) {\n timerId.current = globalThis.setInterval(nonReactiveCallback, reactiveMilliseconds);\n }\n return clearInterval;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [reactiveMilliseconds]);\n\n return clearInterval;\n}\n"],"names":["nonReactiveCallback","reactiveMilliseconds","timerId","useRef","clearInterval","useCallback","globalThis","current","useEffect","undefined","setInterval"],"mappings":"wDAEO,SAAqBA,EAAiCC,GAC3D,MAAMC,EAAUC,EAAAA,SAEVC,EAAgBC,EAAAA,aAAY,KAChCC,WAAWF,cAAcF,EAAQK,QAAQ,GACxC,IAUH,OARAC,EAAAA,WAAU,UACqBC,IAAzBR,IACFC,EAAQK,QAAUD,WAAWI,YAAYV,EAAqBC,IAEzDG,IAEN,CAACH,IAEGG,CACT"}
@@ -1 +1 @@
1
- export declare function useInterval(nonReactiveCallback: () => void, milliseconds: number): () => void;
1
+ export declare function useInterval(nonReactiveCallback: () => void, reactiveMilliseconds?: number): () => void;
@@ -1,2 +1,2 @@
1
- import{useRef as r,useCallback as t,useEffect as n}from"react";function e(e,o){const c=r(),a=t((()=>{window.clearInterval(c.current)}),[]);return n((()=>(c.current=window.setInterval(e,o),a)),[o]),a}export{e as useInterval};
1
+ import{useRef as r,useCallback as t,useEffect as e}from"react";function n(n,o){const l=r(),a=t((()=>{globalThis.clearInterval(l.current)}),[]);return e((()=>(void 0!==o&&(l.current=globalThis.setInterval(n,o)),a)),[o]),a}export{n as useInterval};
2
2
  //# sourceMappingURL=useInterval.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useInterval.js","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\nexport function useInterval(nonReactiveCallback: () => void, milliseconds: number): () => void {\n const timerId = useRef<number>();\n\n const clearInterval = useCallback(() => {\n window.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n timerId.current = window.setInterval(nonReactiveCallback, milliseconds);\n return clearInterval;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [milliseconds]);\n\n return clearInterval;\n}\n"],"names":["useInterval","nonReactiveCallback","milliseconds","timerId","useRef","clearInterval","useCallback","window","current","useEffect","setInterval"],"mappings":"+DAEO,SAASA,EAAYC,EAAiCC,GAC3D,MAAMC,EAAUC,IAEVC,EAAgBC,GAAY,KAChCC,OAAOF,cAAcF,EAAQK,QAAQ,GACpC,IAQH,OANAC,GAAU,KACRN,EAAQK,QAAUD,OAAOG,YAAYT,EAAqBC,GACnDG,IAEN,CAACH,IAEGG,CACT"}
1
+ {"version":3,"file":"useInterval.js","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\nexport function useInterval(nonReactiveCallback: () => void, reactiveMilliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.Timeout>();\n\n const clearInterval = useCallback(() => {\n globalThis.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n if (reactiveMilliseconds !== undefined) {\n timerId.current = globalThis.setInterval(nonReactiveCallback, reactiveMilliseconds);\n }\n return clearInterval;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [reactiveMilliseconds]);\n\n return clearInterval;\n}\n"],"names":["useInterval","nonReactiveCallback","reactiveMilliseconds","timerId","useRef","clearInterval","useCallback","globalThis","current","useEffect","undefined","setInterval"],"mappings":"+DAEO,SAASA,EAAYC,EAAiCC,GAC3D,MAAMC,EAAUC,IAEVC,EAAgBC,GAAY,KAChCC,WAAWF,cAAcF,EAAQK,QAAQ,GACxC,IAUH,OARAC,GAAU,UACqBC,IAAzBR,IACFC,EAAQK,QAAUD,WAAWI,YAAYV,EAAqBC,IAEzDG,IAEN,CAACH,IAEGG,CACT"}
@@ -1,2 +1,2 @@
1
- "use strict";var e=require("react");exports.useStorage=function(t,n,r,s={}){const o=e.useSyncExternalStore((e=>{const t=t=>{t.key===n&&e()};return window.addEventListener("storage",t),()=>window.removeEventListener("storage",t)}),(()=>window[t].getItem(n)),(()=>"ssrJsonText"in s&&s.ssrJsonText)),i=e.useMemo((()=>{try{if(o){const e=JSON.parse(o);return null!=s&&s.parseAfterJsonParse?null==s?void 0:s.parseAfterJsonParse(e):e}}catch(e){}return r}),[o,r]),a=e.useCallback((e=>{const r="function"==typeof e?e(i):e;let s=null;null==r?window[t].removeItem(n):(s=JSON.stringify(r),window[t].setItem(n,s)),window.dispatchEvent(new StorageEvent("storage",{key:n,newValue:s}))}),[n,i]);return e.useEffect((()=>{null===window[t].getItem(n)&&void 0!==r&&window[t].setItem(n,JSON.stringify(r))}),[n,r]),[i,a]};
1
+ "use strict";var e=require("react");exports.useStorage=function(t,n,s,r={}){const o=e.useSyncExternalStore((e=>{const t=t=>{t.key===n&&e()};return globalThis.addEventListener("storage",t),()=>globalThis.removeEventListener("storage",t)}),(()=>window[t].getItem(n)),(()=>"ssrJsonText"in r&&r.ssrJsonText)),i=e.useMemo((()=>{try{if(o){const e=JSON.parse(o);return null!=r&&r.parseAfterJsonParse?null==r?void 0:r.parseAfterJsonParse(e):e}}catch(e){}return s}),[o,s]),a=e.useCallback((e=>{const s="function"==typeof e?e(i):e;let r=null;null==s?window[t].removeItem(n):(r=JSON.stringify(s),window[t].setItem(n,r)),globalThis.dispatchEvent(new StorageEvent("storage",{key:n,newValue:r}))}),[n,i]);return e.useEffect((()=>{null===window[t].getItem(n)&&void 0!==s&&window[t].setItem(n,JSON.stringify(s))}),[n,s]),[i,a]};
2
2
  //# sourceMappingURL=useStorage.cjs.map
@@ -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 window.addEventListener('storage', newCallback);\n return () => window.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 window.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","window","addEventListener","removeEventListener","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,OAAOC,iBAAiB,UAAWH,GAC5B,IAAME,OAAOE,oBAAoB,UAAWJ,EAAY,IAEjE,IAAME,OAAOT,GAAwBY,QAAQX,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBU,cAE5DC,EAAQC,EAAAA,SAAQ,KACpB,IACE,GAAIX,EAAU,CACZ,MAAMY,EAAOC,KAAKC,MAAMd,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBgB,oBAAsBhB,MAAAA,OAAAA,EAAAA,EAAoBgB,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,OAAOlB,CAAY,GAElB,CAACE,EAAUF,IAERmB,EAAWC,EAAWA,aACzBC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAGpG,IAAIE,EAA0B,KAC1BD,QACFf,OAAOT,GAAwB0B,WAAWzB,IAE1CwB,EAAWR,KAAKU,UAAUH,GAC1Bf,OAAOT,GAAwB4B,QAAQ3B,EAAKwB,IAE9ChB,OAAOoB,cAAc,IAAIC,aAAa,UAAW,CAAE7B,MAAKwB,aAAY,GAGtE,CAACxB,EAAKa,IAUR,OAPAiB,EAAAA,WAAU,KAC4C,OAAhDtB,OAAOT,GAAwBY,QAAQX,SAAkC+B,IAAjB9B,GAC1DO,OAAOT,GAAwB4B,QAAQ3B,EAAKgB,KAAKU,UAAUzB,GAC7D,GAEC,CAACD,EAAKC,IAEF,CAACY,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);\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,2 +1,2 @@
1
- import{useSyncExternalStore as e,useMemo as t,useCallback as n,useEffect as r}from"react";function o(o,s,i,w={}){const a=e((e=>{const t=t=>{t.key===s&&e()};return window.addEventListener("storage",t),()=>window.removeEventListener("storage",t)}),(()=>window[o].getItem(s)),(()=>"ssrJsonText"in w&&w.ssrJsonText)),d=t((()=>{try{if(a){const e=JSON.parse(a);return null!=w&&w.parseAfterJsonParse?null==w?void 0:w.parseAfterJsonParse(e):e}}catch(e){}return i}),[a,i]),l=n((e=>{const t="function"==typeof e?e(d):e;let n=null;null==t?window[o].removeItem(s):(n=JSON.stringify(t),window[o].setItem(s,n)),window.dispatchEvent(new StorageEvent("storage",{key:s,newValue:n}))}),[s,d]);return r((()=>{null===window[o].getItem(s)&&void 0!==i&&window[o].setItem(s,JSON.stringify(i))}),[s,i]),[d,l]}export{o as useStorage};
1
+ import{useSyncExternalStore as e,useMemo as t,useCallback as n,useEffect as r}from"react";function o(o,s,i,l={}){const a=e((e=>{const t=t=>{t.key===s&&e()};return globalThis.addEventListener("storage",t),()=>globalThis.removeEventListener("storage",t)}),(()=>window[o].getItem(s)),(()=>"ssrJsonText"in l&&l.ssrJsonText)),u=t((()=>{try{if(a){const e=JSON.parse(a);return null!=l&&l.parseAfterJsonParse?null==l?void 0:l.parseAfterJsonParse(e):e}}catch(e){}return i}),[a,i]),w=n((e=>{const t="function"==typeof e?e(u):e;let n=null;null==t?window[o].removeItem(s):(n=JSON.stringify(t),window[o].setItem(s,n)),globalThis.dispatchEvent(new StorageEvent("storage",{key:s,newValue:n}))}),[s,u]);return r((()=>{null===window[o].getItem(s)&&void 0!==i&&window[o].setItem(s,JSON.stringify(i))}),[s,i]),[u,w]}export{o as useStorage};
2
2
  //# sourceMappingURL=useStorage.js.map
@@ -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 window.addEventListener('storage', newCallback);\n return () => window.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 window.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","window","addEventListener","removeEventListener","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,OAAOC,iBAAiB,UAAWH,GAC5B,IAAME,OAAOE,oBAAoB,UAAWJ,EAAY,IAEjE,IAAME,OAAOT,GAAwBY,QAAQX,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBU,cAE5DC,EAAQC,GAAQ,KACpB,IACE,GAAIX,EAAU,CACZ,MAAMY,EAAOC,KAAKC,MAAMd,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBgB,oBAAsBhB,MAAAA,OAAAA,EAAAA,EAAoBgB,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,OAAOlB,CAAY,GAElB,CAACE,EAAUF,IAERmB,EAAWC,GACdC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAGpG,IAAIE,EAA0B,KAC1BD,QACFf,OAAOT,GAAwB0B,WAAWzB,IAE1CwB,EAAWR,KAAKU,UAAUH,GAC1Bf,OAAOT,GAAwB4B,QAAQ3B,EAAKwB,IAE9ChB,OAAOoB,cAAc,IAAIC,aAAa,UAAW,CAAE7B,MAAKwB,aAAY,GAGtE,CAACxB,EAAKa,IAUR,OAPAiB,GAAU,KAC4C,OAAhDtB,OAAOT,GAAwBY,QAAQX,SAAkC+B,IAAjB9B,GAC1DO,OAAOT,GAAwB4B,QAAQ3B,EAAKgB,KAAKU,UAAUzB,GAC7D,GAEC,CAACD,EAAKC,IAEF,CAACY,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);\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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/shared-lib-react",
3
- "version": "3.2.8",
3
+ "version": "3.2.10",
4
4
  "license": "Apache-2.0",
5
5
  "author": "WillBooster Inc.",
6
6
  "sideEffects": false,
@@ -33,30 +33,30 @@
33
33
  },
34
34
  "prettier": "@willbooster/prettier-config",
35
35
  "devDependencies": {
36
- "@babel/core": "7.24.7",
36
+ "@babel/core": "7.25.2",
37
37
  "@mdx-js/react": "3.0.1",
38
- "@storybook/addon-actions": "8.1.11",
39
- "@storybook/addon-docs": "8.1.11",
40
- "@storybook/addon-essentials": "8.1.11",
41
- "@storybook/addon-interactions": "8.1.11",
42
- "@storybook/addon-links": "8.1.11",
38
+ "@storybook/addon-actions": "8.2.9",
39
+ "@storybook/addon-docs": "8.2.9",
40
+ "@storybook/addon-essentials": "8.2.9",
41
+ "@storybook/addon-interactions": "8.2.9",
42
+ "@storybook/addon-links": "8.2.9",
43
43
  "@storybook/builder-webpack4": "6.5.16",
44
44
  "@storybook/manager-webpack4": "6.5.16",
45
- "@storybook/react": "8.1.11",
45
+ "@storybook/react": "8.2.9",
46
46
  "@storybook/testing-library": "0.2.2",
47
47
  "@types/eslint": "8.56.10",
48
48
  "@types/micromatch": "4.0.9",
49
- "@types/react": "18.3.3",
49
+ "@types/react": "18.3.5",
50
50
  "@types/react-dom": "18.3.0",
51
- "@typescript-eslint/eslint-plugin": "7.18.0",
52
- "@typescript-eslint/parser": "7.18.0",
51
+ "@typescript-eslint/eslint-plugin": "8.3.0",
52
+ "@typescript-eslint/parser": "8.3.0",
53
53
  "@willbooster/eslint-config-ts-react": "10.2.0",
54
54
  "@willbooster/prettier-config": "9.1.2",
55
55
  "babel-loader": "9.1.3",
56
56
  "build-ts": "13.1.8",
57
57
  "eslint": "8.57.0",
58
58
  "eslint-config-prettier": "9.1.0",
59
- "eslint-import-resolver-typescript": "3.6.1",
59
+ "eslint-import-resolver-typescript": "3.6.3",
60
60
  "eslint-plugin-import": "2.29.1",
61
61
  "eslint-plugin-react": "7.35.0",
62
62
  "eslint-plugin-react-hooks": "4.6.2",
@@ -64,12 +64,12 @@
64
64
  "eslint-plugin-sort-destructure-keys": "2.0.0",
65
65
  "eslint-plugin-storybook": "0.8.0",
66
66
  "eslint-plugin-unicorn": "55.0.0",
67
- "lint-staged": "15.2.7",
68
- "micromatch": "4.0.7",
67
+ "lint-staged": "15.2.10",
68
+ "micromatch": "4.0.8",
69
69
  "prettier": "3.3.3",
70
70
  "react": "18.3.1",
71
71
  "react-dom": "18.3.1",
72
- "sort-package-json": "2.10.0",
72
+ "sort-package-json": "2.10.1",
73
73
  "typescript": "5.5.4",
74
74
  "vitest": "2.0.5"
75
75
  },