@geee-be/react-utils 1.1.0 → 1.1.2
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.
- package/CHANGELOG.md +6 -0
- package/dist/hooks/use-history-state.js +17 -4
- package/package.json +2 -1
- package/src/hooks/use-history-state.ts +27 -7
- package/tsconfig.json +15 -2
package/CHANGELOG.md
CHANGED
@@ -1,13 +1,19 @@
|
|
1
1
|
'use client';
|
2
2
|
import { useEffect, useState } from 'react';
|
3
3
|
import { deserialize, getInitialValue, serialize } from './state.util.js';
|
4
|
+
const LOADING = Symbol('loading');
|
4
5
|
export const useHistoryState = (key, initialValue, replace = true, options) => {
|
5
6
|
if (typeof history === 'undefined') {
|
6
|
-
return [undefined, () => { }];
|
7
|
+
return [undefined, () => { }, false];
|
7
8
|
}
|
8
|
-
const [value, setValue] = useState(
|
9
|
-
getInitialValue(initialValue));
|
9
|
+
const [value, setValue] = useState(LOADING);
|
10
10
|
useEffect(() => {
|
11
|
+
setValue(deserialize(history.state?.[key], options?.fromSerializable) ??
|
12
|
+
getInitialValue(initialValue));
|
13
|
+
}, [key, options?.fromSerializable, initialValue]);
|
14
|
+
useEffect(() => {
|
15
|
+
if (value === LOADING)
|
16
|
+
return;
|
11
17
|
if (replace) {
|
12
18
|
history.replaceState({ ...history.state, [key]: serialize(value, options?.toSerializable) }, '');
|
13
19
|
}
|
@@ -15,5 +21,12 @@ export const useHistoryState = (key, initialValue, replace = true, options) => {
|
|
15
21
|
history.pushState({ ...history.state, [key]: serialize(value, options?.toSerializable) }, '');
|
16
22
|
}
|
17
23
|
}, [key, value, options?.toSerializable, replace]);
|
18
|
-
|
24
|
+
if (value === LOADING) {
|
25
|
+
return [undefined, () => { }, false];
|
26
|
+
}
|
27
|
+
return [
|
28
|
+
value,
|
29
|
+
(v) => setValue(v),
|
30
|
+
true,
|
31
|
+
];
|
19
32
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@geee-be/react-utils",
|
3
|
-
"version": "1.1.
|
3
|
+
"version": "1.1.2",
|
4
4
|
"description": "",
|
5
5
|
"keywords": [],
|
6
6
|
"license": "MIT",
|
@@ -34,6 +34,7 @@
|
|
34
34
|
"build-storybook": "storybook build",
|
35
35
|
"lint": "npm run lint:biome && npm run lint:types && npm run lint:unused-exports",
|
36
36
|
"lint:biome": "biome ci src/",
|
37
|
+
"lint:biome:fix": "biome check src/ --apply",
|
37
38
|
"lint:types": "tsc --noEmit",
|
38
39
|
"lint:unused-exports": "ts-unused-exports tsconfig.unused-exports.json --excludePathsFromReport=src/index.ts",
|
39
40
|
"prestart:dev": "rimraf dist/*",
|
@@ -5,22 +5,34 @@ import { useEffect, useState } from 'react';
|
|
5
5
|
import type { InitialValue, SerializationOptions } from './state.util.js';
|
6
6
|
import { deserialize, getInitialValue, serialize } from './state.util.js';
|
7
7
|
|
8
|
+
type Response<T> =
|
9
|
+
| [T, Dispatch<SetStateAction<T>>, true]
|
10
|
+
| [undefined, Dispatch<SetStateAction<T>>, false];
|
11
|
+
|
12
|
+
const LOADING = Symbol('loading');
|
13
|
+
|
8
14
|
export const useHistoryState = <T, S = T>(
|
9
15
|
key: string,
|
10
16
|
initialValue: InitialValue<T>,
|
11
17
|
replace = true,
|
12
18
|
options?: SerializationOptions<T, S>,
|
13
|
-
):
|
19
|
+
): Response<T> => {
|
14
20
|
if (typeof history === 'undefined') {
|
15
|
-
return [undefined
|
21
|
+
return [undefined, () => {}, false];
|
16
22
|
}
|
17
|
-
|
18
|
-
|
23
|
+
|
24
|
+
const [value, setValue] = useState<T | typeof LOADING>(LOADING);
|
25
|
+
|
26
|
+
useEffect(() => {
|
27
|
+
setValue(
|
19
28
|
deserialize(history.state?.[key], options?.fromSerializable) ??
|
20
|
-
|
21
|
-
|
29
|
+
getInitialValue(initialValue),
|
30
|
+
);
|
31
|
+
}, [key, options?.fromSerializable, initialValue]);
|
22
32
|
|
23
33
|
useEffect(() => {
|
34
|
+
if (value === LOADING) return;
|
35
|
+
|
24
36
|
if (replace) {
|
25
37
|
history.replaceState(
|
26
38
|
{ ...history.state, [key]: serialize(value, options?.toSerializable) },
|
@@ -34,5 +46,13 @@ export const useHistoryState = <T, S = T>(
|
|
34
46
|
}
|
35
47
|
}, [key, value, options?.toSerializable, replace]);
|
36
48
|
|
37
|
-
|
49
|
+
if (value === LOADING) {
|
50
|
+
return [undefined, () => {}, false];
|
51
|
+
}
|
52
|
+
|
53
|
+
return [
|
54
|
+
value,
|
55
|
+
(v) => setValue(v as SetStateAction<T | typeof LOADING>),
|
56
|
+
true,
|
57
|
+
];
|
38
58
|
};
|
package/tsconfig.json
CHANGED
@@ -1,8 +1,21 @@
|
|
1
1
|
{
|
2
|
-
"extends": "../create/tsconfig.web.json",
|
3
2
|
"compilerOptions": {
|
3
|
+
"allowJs": false,
|
4
|
+
"allowSyntheticDefaultImports": true,
|
5
|
+
"esModuleInterop": false,
|
6
|
+
"forceConsistentCasingInFileNames": true,
|
7
|
+
"isolatedModules": true,
|
8
|
+
"jsx": "react-jsx",
|
9
|
+
"lib": ["DOM", "DOM.Iterable", "ESNext"],
|
10
|
+
"module": "ESNext",
|
11
|
+
"moduleResolution": "Node",
|
4
12
|
"noEmit": false,
|
5
|
-
"outDir": "./dist"
|
13
|
+
"outDir": "./dist",
|
14
|
+
"resolveJsonModule": true,
|
15
|
+
"skipLibCheck": true,
|
16
|
+
"strict": true,
|
17
|
+
"target": "ESNext",
|
18
|
+
"useDefineForClassFields": true
|
6
19
|
},
|
7
20
|
"include": ["./src"]
|
8
21
|
}
|