@willbooster/shared-lib-react 5.0.0 → 5.0.1
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":"useStorage.cjs","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport
|
|
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 interface UseStorageOptions<T> {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText?: string;\n validValues?: Set<T>;\n}\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValueOrFunc: T | (() => 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 () => {\n globalThis.removeEventListener('storage', newCallback);\n };\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 const value = nonReactiveOptions.parseAfterJsonParse?.(json) ?? json;\n if (!nonReactiveOptions.validValues || nonReactiveOptions.validValues.has(value)) return value;\n }\n } catch {\n // do nothing\n }\n return typeof initialValueOrFunc === 'function' ? (initialValueOrFunc as () => T)() : initialValueOrFunc;\n }, [jsonText, initialValueOrFunc]); // Don't add nonReactiveOptions to deps because it's not allowed to change.\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] // Don't add nonReactiveStorageType to deps because it's not allowed to change.\n );\n\n useEffect(() => {\n const resolvedInitialValue =\n typeof initialValueOrFunc === 'function' ? (initialValueOrFunc as () => T)() : initialValueOrFunc;\n if (window[nonReactiveStorageType].getItem(key) === null && resolvedInitialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(resolvedInitialValue));\n }\n }, [key, initialValueOrFunc]); // Don't add nonReactiveStorageType to deps because it's not allowed to change.\n\n return [value, setState];\n}\n"],"names":["nonReactiveStorageType","key","initialValueOrFunc","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","globalThis","addEventListener","removeEventListener","window","getItem","ssrJsonText","value","useMemo","_nonReactiveOptions$p","_nonReactiveOptions$p2","json","JSON","parse","parseAfterJsonParse","call","validValues","has","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","resolvedInitialValue","undefined"],"mappings":"uDASO,SACLA,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,EAAAA,qBACdC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,KAGzB,OADAG,WAAWC,iBAAiB,UAAWH,GAChC,KACLE,WAAWE,oBAAoB,UAAWJ,KAG9C,IAAMK,OAAOZ,GAAwBa,QAAQZ,GAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBW,aAE5DC,EAAQC,EAAAA,QAAQ,KACpB,IACE,GAAIZ,EAAU,CAAA,IAAAa,EAAAC,EACZ,MAAMC,EAAOC,KAAKC,MAAMjB,GAClBW,EAAsD,OAAjDE,EAAyC,OAAzCC,EAAGf,EAAmBmB,0BAAmB,EAAtCJ,EAAAK,KAAApB,EAAyCgB,IAAKF,EAAIE,EAChE,IAAKhB,EAAmBqB,aAAerB,EAAmBqB,YAAYC,IAAIV,GAAQ,OAAOA,CAC3F,CACF,CAAE,MAAAW,GACA,CAEF,MAAqC,mBAAvBxB,EAAqCA,IAAmCA,GACrF,CAACE,EAAUF,IAERyB,EAAWC,EAAAA,YACdC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCd,GAASc,EAGpG,IAAIE,EAA0B,KAC1BD,QACFlB,OAAOZ,GAAwBgC,WAAW/B,IAE1C8B,EAAWX,KAAKa,UAAUH,GAC1BlB,OAAOZ,GAAwBkC,QAAQjC,EAAK8B,IAE9CtB,WAAW0B,cAAc,IAAIC,aAAa,UAAW,CAAEnC,MAAK8B,eAG9D,CAAC9B,EAAKc,IAWR,OARAsB,EAAAA,UAAU,KACR,MAAMC,EAC0B,mBAAvBpC,EAAqCA,IAAmCA,EAC7B,OAAhDU,OAAOZ,GAAwBa,QAAQZ,SAA0CsC,IAAzBD,GAC1D1B,OAAOZ,GAAwBkC,QAAQjC,EAAKmB,KAAKa,UAAUK,KAE5D,CAACrC,EAAKC,IAEF,CAACa,EAAOY,EACjB"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type React from 'react';
|
|
2
|
-
export
|
|
2
|
+
export interface UseStorageOptions<T> {
|
|
3
3
|
parseAfterJsonParse?: (value: unknown) => T;
|
|
4
4
|
ssrJsonText?: string;
|
|
5
5
|
validValues?: Set<T>;
|
|
6
|
-
}
|
|
6
|
+
}
|
|
7
7
|
export declare function useStorage<T>(nonReactiveStorageType: 'localStorage' | 'sessionStorage', key: string, initialValueOrFunc: T | (() => T), nonReactiveOptions?: UseStorageOptions<T>): [T, React.Dispatch<React.SetStateAction<T>>];
|
|
@@ -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
|
|
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 interface UseStorageOptions<T> {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText?: string;\n validValues?: Set<T>;\n}\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValueOrFunc: T | (() => 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 () => {\n globalThis.removeEventListener('storage', newCallback);\n };\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 const value = nonReactiveOptions.parseAfterJsonParse?.(json) ?? json;\n if (!nonReactiveOptions.validValues || nonReactiveOptions.validValues.has(value)) return value;\n }\n } catch {\n // do nothing\n }\n return typeof initialValueOrFunc === 'function' ? (initialValueOrFunc as () => T)() : initialValueOrFunc;\n }, [jsonText, initialValueOrFunc]); // Don't add nonReactiveOptions to deps because it's not allowed to change.\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] // Don't add nonReactiveStorageType to deps because it's not allowed to change.\n );\n\n useEffect(() => {\n const resolvedInitialValue =\n typeof initialValueOrFunc === 'function' ? (initialValueOrFunc as () => T)() : initialValueOrFunc;\n if (window[nonReactiveStorageType].getItem(key) === null && resolvedInitialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(resolvedInitialValue));\n }\n }, [key, initialValueOrFunc]); // Don't add nonReactiveStorageType to deps because it's not allowed to change.\n\n return [value, setState];\n}\n"],"names":["useStorage","nonReactiveStorageType","key","initialValueOrFunc","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","globalThis","addEventListener","removeEventListener","window","getItem","ssrJsonText","value","useMemo","_nonReactiveOptions$p","_nonReactiveOptions$p2","json","JSON","parse","parseAfterJsonParse","call","validValues","has","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","resolvedInitialValue","undefined"],"mappings":"0FASO,SAASA,EACdC,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,EACdC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,KAGzB,OADAG,WAAWC,iBAAiB,UAAWH,GAChC,KACLE,WAAWE,oBAAoB,UAAWJ,KAG9C,IAAMK,OAAOZ,GAAwBa,QAAQZ,GAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBW,aAE5DC,EAAQC,EAAQ,KACpB,IACE,GAAIZ,EAAU,CAAA,IAAAa,EAAAC,EACZ,MAAMC,EAAOC,KAAKC,MAAMjB,GAClBW,EAAsD,OAAjDE,EAAyC,OAAzCC,EAAGf,EAAmBmB,0BAAmB,EAAtCJ,EAAAK,KAAApB,EAAyCgB,IAAKF,EAAIE,EAChE,IAAKhB,EAAmBqB,aAAerB,EAAmBqB,YAAYC,IAAIV,GAAQ,OAAOA,CAC3F,CACF,CAAE,MAAAW,GACA,CAEF,MAAqC,mBAAvBxB,EAAqCA,IAAmCA,GACrF,CAACE,EAAUF,IAERyB,EAAWC,EACdC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCd,GAASc,EAGpG,IAAIE,EAA0B,KAC1BD,QACFlB,OAAOZ,GAAwBgC,WAAW/B,IAE1C8B,EAAWX,KAAKa,UAAUH,GAC1BlB,OAAOZ,GAAwBkC,QAAQjC,EAAK8B,IAE9CtB,WAAW0B,cAAc,IAAIC,aAAa,UAAW,CAAEnC,MAAK8B,eAG9D,CAAC9B,EAAKc,IAWR,OARAsB,EAAU,KACR,MAAMC,EAC0B,mBAAvBpC,EAAqCA,IAAmCA,EAC7B,OAAhDU,OAAOZ,GAAwBa,QAAQZ,SAA0CsC,IAAzBD,GAC1D1B,OAAOZ,GAAwBkC,QAAQjC,EAAKmB,KAAKa,UAAUK,KAE5D,CAACrC,EAAKC,IAEF,CAACa,EAAOY,EACjB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/shared-lib-react",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"author": "WillBooster Inc.",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -35,48 +35,48 @@
|
|
|
35
35
|
},
|
|
36
36
|
"prettier": "@willbooster/prettier-config",
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@babel/core": "7.28.
|
|
38
|
+
"@babel/core": "7.28.5",
|
|
39
39
|
"@mdx-js/react": "3.1.1",
|
|
40
40
|
"@storybook/addon-actions": "9.0.8",
|
|
41
|
-
"@storybook/addon-docs": "9.1.
|
|
41
|
+
"@storybook/addon-docs": "9.1.15",
|
|
42
42
|
"@storybook/addon-essentials": "8.6.14",
|
|
43
43
|
"@storybook/addon-interactions": "8.6.14",
|
|
44
|
-
"@storybook/addon-links": "9.1.
|
|
44
|
+
"@storybook/addon-links": "9.1.15",
|
|
45
45
|
"@storybook/builder-webpack4": "6.5.16",
|
|
46
46
|
"@storybook/manager-webpack4": "6.5.16",
|
|
47
|
-
"@storybook/react": "9.1.
|
|
47
|
+
"@storybook/react": "9.1.15",
|
|
48
48
|
"@types/eslint": "9.6.1",
|
|
49
|
-
"@types/micromatch": "4.0.
|
|
49
|
+
"@types/micromatch": "4.0.10",
|
|
50
50
|
"@types/react": "19.2.2",
|
|
51
|
-
"@types/react-dom": "19.2.
|
|
52
|
-
"@willbooster/eslint-config-ts-react": "11.5.
|
|
53
|
-
"@willbooster/prettier-config": "10.2.
|
|
51
|
+
"@types/react-dom": "19.2.2",
|
|
52
|
+
"@willbooster/eslint-config-ts-react": "11.5.11",
|
|
53
|
+
"@willbooster/prettier-config": "10.2.1",
|
|
54
54
|
"babel-loader": "10.0.0",
|
|
55
|
-
"build-ts": "16.0.
|
|
56
|
-
"eslint": "9.
|
|
55
|
+
"build-ts": "16.0.12",
|
|
56
|
+
"eslint": "9.39.1",
|
|
57
57
|
"eslint-config-flat-gitignore": "2.1.0",
|
|
58
58
|
"eslint-config-prettier": "10.1.8",
|
|
59
59
|
"eslint-import-resolver-typescript": "4.4.4",
|
|
60
60
|
"eslint-plugin-import-x": "4.16.1",
|
|
61
61
|
"eslint-plugin-react": "7.37.5",
|
|
62
62
|
"eslint-plugin-react-compiler": "19.1.0-rc.2",
|
|
63
|
-
"eslint-plugin-react-hooks": "7.0.
|
|
63
|
+
"eslint-plugin-react-hooks": "7.0.1",
|
|
64
64
|
"eslint-plugin-sort-class-members": "1.21.0",
|
|
65
65
|
"eslint-plugin-sort-destructure-keys": "2.0.0",
|
|
66
|
-
"eslint-plugin-storybook": "9.1.
|
|
67
|
-
"eslint-plugin-unicorn": "
|
|
68
|
-
"eslint-plugin-unused-imports": "4.
|
|
69
|
-
"globals": "16.
|
|
70
|
-
"lint-staged": "16.2.
|
|
66
|
+
"eslint-plugin-storybook": "9.1.15",
|
|
67
|
+
"eslint-plugin-unicorn": "62.0.0",
|
|
68
|
+
"eslint-plugin-unused-imports": "4.3.0",
|
|
69
|
+
"globals": "16.5.0",
|
|
70
|
+
"lint-staged": "16.2.6",
|
|
71
71
|
"micromatch": "4.0.8",
|
|
72
72
|
"prettier": "3.6.2",
|
|
73
|
-
"prettier-plugin-java": "2.7.
|
|
73
|
+
"prettier-plugin-java": "2.7.7",
|
|
74
74
|
"react": "19.2.0",
|
|
75
75
|
"react-dom": "19.2.0",
|
|
76
76
|
"sort-package-json": "3.4.0",
|
|
77
77
|
"typescript": "5.9.3",
|
|
78
|
-
"typescript-eslint": "8.46.
|
|
79
|
-
"vitest": "
|
|
78
|
+
"typescript-eslint": "8.46.3",
|
|
79
|
+
"vitest": "4.0.3"
|
|
80
80
|
},
|
|
81
81
|
"peerDependencies": {
|
|
82
82
|
"react": "~19.2.0",
|