@wzyjs/hooks 0.3.32 → 0.3.36

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.
@@ -2,3 +2,4 @@ export * from './useElement';
2
2
  export * from './useElementScrollVisible';
3
3
  export * from './useCacheState';
4
4
  export * from './useHovered';
5
+ export * from './useProjectLocalStorage';
package/dist/dom/index.js CHANGED
@@ -2,3 +2,4 @@ export * from './useElement';
2
2
  export * from './useElementScrollVisible';
3
3
  export * from './useCacheState';
4
4
  export * from './useHovered';
5
+ export * from './useProjectLocalStorage';
@@ -0,0 +1,11 @@
1
+ type ProjectLocalStorageStore = Record<string, unknown>;
2
+ interface UseProjectLocalStorageOptions<TStore extends ProjectLocalStorageStore> {
3
+ defaultValue?: TStore;
4
+ }
5
+ export declare const useProjectLocalStorage: <TStore extends ProjectLocalStorageStore = ProjectLocalStorageStore>(storageKey: string, options?: UseProjectLocalStorageOptions<TStore>) => {
6
+ getStore: () => TStore;
7
+ setStore: (store: TStore) => TStore;
8
+ getValue: <TValue = unknown>(path: string, fallbackValue?: TValue) => TValue;
9
+ setValue: <TValue = unknown>(path: string, value: TValue) => TStore;
10
+ };
11
+ export {};
@@ -0,0 +1,62 @@
1
+ import { useCallback } from 'react';
2
+ import { cloneDeep, get, set } from 'lodash-es';
3
+ const getBrowserLocalStorage = () => {
4
+ if (typeof window === 'undefined') {
5
+ return null;
6
+ }
7
+ return window.localStorage;
8
+ };
9
+ const isObjectStore = (value) => {
10
+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
11
+ };
12
+ // 功能:用一个项目级 localStorage key 集中保存对象,并通过 path 读写对象内部字段。
13
+ export const useProjectLocalStorage = (storageKey, options = {}) => {
14
+ const { defaultValue } = options;
15
+ const getStore = useCallback(() => {
16
+ const storage = getBrowserLocalStorage();
17
+ if (!storage) {
18
+ return cloneDeep(defaultValue || {});
19
+ }
20
+ try {
21
+ const rawValue = storage.getItem(storageKey);
22
+ if (!rawValue) {
23
+ return cloneDeep(defaultValue || {});
24
+ }
25
+ const parsedValue = JSON.parse(rawValue);
26
+ if (!isObjectStore(parsedValue)) {
27
+ return cloneDeep(defaultValue || {});
28
+ }
29
+ return parsedValue;
30
+ }
31
+ catch {
32
+ return cloneDeep(defaultValue || {});
33
+ }
34
+ }, [defaultValue, storageKey]);
35
+ const setStore = useCallback((store) => {
36
+ const storage = getBrowserLocalStorage();
37
+ if (!storage) {
38
+ return store;
39
+ }
40
+ try {
41
+ storage.setItem(storageKey, JSON.stringify(store));
42
+ }
43
+ catch {
44
+ return store;
45
+ }
46
+ return store;
47
+ }, [storageKey]);
48
+ const getValue = useCallback((path, fallbackValue) => {
49
+ return get(getStore(), path, fallbackValue);
50
+ }, [getStore]);
51
+ const setValue = useCallback((path, value) => {
52
+ const nextStore = cloneDeep(getStore());
53
+ set(nextStore, path, value);
54
+ return setStore(nextStore);
55
+ }, [getStore, setStore]);
56
+ return {
57
+ getStore,
58
+ setStore,
59
+ getValue,
60
+ setValue,
61
+ };
62
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wzyjs/hooks",
3
- "version": "0.3.32",
3
+ "version": "0.3.36",
4
4
  "description": "description",
5
5
  "author": "wzy",
6
6
  "type": "module",
@@ -8,7 +8,8 @@
8
8
  "types": "dist/index.d.ts",
9
9
  "sideEffects": false,
10
10
  "scripts": {
11
- "build": "rm -rf dist && tsc"
11
+ "build": "rm -rf dist && tsc",
12
+ "typecheck": "tsc --noEmit"
12
13
  },
13
14
  "files": [
14
15
  "dist"
@@ -51,5 +52,5 @@
51
52
  "publishConfig": {
52
53
  "access": "public"
53
54
  },
54
- "gitHead": "260d8290106ef447ec42375d4656af2e89675b15"
55
+ "gitHead": "7db0faf741993d729a9e1c0742f480f364816222"
55
56
  }