tssentials 1.1.0 → 1.3.0
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/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/use-stored-data/RawWrapper.d.ts +4 -0
- package/dist/use-stored-data/RawWrapper.js +1 -0
- package/dist/use-stored-data/isUpdater.d.ts +2 -0
- package/dist/use-stored-data/isUpdater.js +1 -0
- package/dist/use-stored-data/load.d.ts +3 -0
- package/dist/use-stored-data/load.js +7 -0
- package/dist/use-stored-data/use-version/DataVersionContext.d.ts +9 -0
- package/dist/use-stored-data/use-version/DataVersionContext.js +11 -0
- package/dist/use-stored-data/use-version/DataVersionContextProvider.d.ts +4 -0
- package/dist/use-stored-data/use-version/DataVersionContextProvider.js +8 -0
- package/dist/use-stored-data/use-version/useVersion.d.ts +8 -0
- package/dist/use-stored-data/use-version/useVersion.js +27 -0
- package/dist/use-stored-data/useStoredData.d.ts +7 -1
- package/dist/use-stored-data/useStoredData.js +13 -36
- package/package.json +6 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { processIfPresent, ifPresent, apply, notEmpty, ifTruthy, } from './util.js';
|
|
2
2
|
export { useStoredData } from './use-stored-data/useStoredData.js';
|
|
3
|
-
export { DataVersionContextProvider as StoredDataVersionContextProvider } from './use-stored-data/version/DataVersionContextProvider.js';
|
|
3
|
+
export { DataVersionContextProvider as StoredDataVersionContextProvider } from './use-stored-data/use-version/DataVersionContextProvider.js';
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { processIfPresent, ifPresent, apply, notEmpty, ifTruthy, } from './util.js';
|
|
2
2
|
export { useStoredData } from './use-stored-data/useStoredData.js';
|
|
3
|
-
export { DataVersionContextProvider as StoredDataVersionContextProvider } from './use-stored-data/version/DataVersionContextProvider.js';
|
|
3
|
+
export { DataVersionContextProvider as StoredDataVersionContextProvider } from './use-stored-data/use-version/DataVersionContextProvider.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const isUpdater = (value) => typeof value === 'function';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type DataVersionContextType = Record<string, number>;
|
|
2
|
+
export declare const DataVersionContext: import("react").Context<{
|
|
3
|
+
state: DataVersionContextType;
|
|
4
|
+
addVersion: (key: string) => void;
|
|
5
|
+
}>;
|
|
6
|
+
export declare const useDataVersionContext: () => {
|
|
7
|
+
state: DataVersionContextType;
|
|
8
|
+
addVersion: (key: string) => void;
|
|
9
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createContext, useContext } from "react";
|
|
2
|
+
export const DataVersionContext = createContext(undefined);
|
|
3
|
+
export const useDataVersionContext = () => {
|
|
4
|
+
const context = useContext(DataVersionContext);
|
|
5
|
+
if (context) {
|
|
6
|
+
return context;
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
throw new Error('DataVersionContext is missing');
|
|
10
|
+
}
|
|
11
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { apply } from "tssentials";
|
|
4
|
+
import { DataVersionContext } from "./DataVersionContext.js";
|
|
5
|
+
export const DataVersionContextProvider = (props) => {
|
|
6
|
+
const [state, setState] = useState({});
|
|
7
|
+
return (_jsx(DataVersionContext.Provider, { value: { state, addVersion: (k) => setState(s => (Object.assign(Object.assign({}, s), { [k]: apply(s[k], v => v ? v + 1 : 1) }))) }, children: props.children }));
|
|
8
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { useDataVersionContext } from "./DataVersionContext.js";
|
|
3
|
+
export const useVersion = (props) => {
|
|
4
|
+
var _a;
|
|
5
|
+
const { value, key } = props;
|
|
6
|
+
const { state: versionContext, addVersion: addContextVersion } = useDataVersionContext();
|
|
7
|
+
const contextVersion = key ? (_a = versionContext[key]) !== null && _a !== void 0 ? _a : 0 : 0;
|
|
8
|
+
const [version, setVersion] = useState(contextVersion);
|
|
9
|
+
const addVersion = () => {
|
|
10
|
+
setVersion(v => v + 1);
|
|
11
|
+
if (key)
|
|
12
|
+
addContextVersion(key);
|
|
13
|
+
};
|
|
14
|
+
const loadVersion = () => {
|
|
15
|
+
if (key && contextVersion > version) {
|
|
16
|
+
props.load();
|
|
17
|
+
setVersion(contextVersion);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (key && value.raw !== localStorage.getItem(key)) {
|
|
22
|
+
localStorage.setItem(key, JSON.stringify(value.obj));
|
|
23
|
+
addVersion();
|
|
24
|
+
}
|
|
25
|
+
}, [value]);
|
|
26
|
+
useEffect(loadVersion, [key, contextVersion]);
|
|
27
|
+
};
|
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { Dispatch, SetStateAction } from "react";
|
|
3
|
-
|
|
3
|
+
type StoredState<Schema extends z.ZodTypeAny> = [
|
|
4
|
+
z.infer<Schema>,
|
|
5
|
+
Dispatch<SetStateAction<z.infer<Schema>>>,
|
|
6
|
+
() => void
|
|
7
|
+
];
|
|
8
|
+
export declare const useStoredData: (key?: string) => <SCHEMA extends z.ZodType>(schema: SCHEMA, defaultValue: z.infer<SCHEMA>) => StoredState<SCHEMA>;
|
|
9
|
+
export {};
|
|
@@ -1,42 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
const load = (key, schema, defaultValue) => apply(localStorage.getItem(key), v => ({
|
|
6
|
-
obj: schema.parse(v !== null ? JSON.parse(v) : defaultValue),
|
|
7
|
-
raw: v !== null && v !== void 0 ? v : ''
|
|
8
|
-
}));
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { load } from "./load.js";
|
|
3
|
+
import { isUpdater } from "./isUpdater.js";
|
|
4
|
+
import { useVersion } from "./use-version/useVersion.js";
|
|
9
5
|
export const useStoredData = (key) => (schema, defaultValue) => {
|
|
10
|
-
var _a;
|
|
11
6
|
const [state, setState] = useState(key
|
|
12
7
|
? load(key, schema, defaultValue)
|
|
13
8
|
: { obj: defaultValue, raw: '' });
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
9
|
+
useVersion({ key, value: state, load: () => setState(load(key, schema, defaultValue)) });
|
|
10
|
+
const setter = (a) => {
|
|
11
|
+
return setState(s => {
|
|
12
|
+
const next = isUpdater(a) ? a(s.obj) : a;
|
|
13
|
+
const result = schema.safeParse(next);
|
|
14
|
+
return { obj: result.success ? result.data : s.obj, raw: '' };
|
|
15
|
+
});
|
|
21
16
|
};
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
setState(load(key, schema, defaultValue));
|
|
25
|
-
setVersion(contextVersion);
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
useEffect(() => {
|
|
29
|
-
if (key && state.raw !== localStorage.getItem(key)) {
|
|
30
|
-
localStorage.setItem(key, JSON.stringify(state.obj));
|
|
31
|
-
addVersion();
|
|
32
|
-
}
|
|
33
|
-
}, [state]);
|
|
34
|
-
useEffect(loadVersion, [key, contextVersion]);
|
|
35
|
-
return [
|
|
36
|
-
state.obj, // state
|
|
37
|
-
(a) => isUpdater(a) // setState
|
|
38
|
-
? setState(s => ({ obj: a(s.obj), raw: '' }))
|
|
39
|
-
: setState(() => ({ obj: a, raw: '' })),
|
|
40
|
-
() => setState({ obj: defaultValue, raw: '' }), // reset
|
|
41
|
-
];
|
|
17
|
+
const resetter = () => setState({ obj: defaultValue, raw: '' });
|
|
18
|
+
return [state.obj, setter, resetter];
|
|
42
19
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tssentials",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Essential functions and types for typescript projects.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,22 +23,23 @@
|
|
|
23
23
|
"scripts": {
|
|
24
24
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
25
25
|
"build": "tsc",
|
|
26
|
+
"prepare": "npm run build",
|
|
26
27
|
"lint": "eslint ."
|
|
27
28
|
},
|
|
28
29
|
"author": "Timo Bühlmann",
|
|
29
30
|
"license": "MIT",
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@eslint/js": "^9.23.0",
|
|
32
|
-
"@types/react": "^
|
|
33
|
-
"@types/react-dom": "^
|
|
33
|
+
"@types/react": "^19.2.7",
|
|
34
|
+
"@types/react-dom": "^19.2.3",
|
|
34
35
|
"eslint": "^9.23.0",
|
|
35
36
|
"eslint-plugin-react-hooks": "^7.0.1",
|
|
36
37
|
"typescript": "^5.8.2",
|
|
37
38
|
"typescript-eslint": "^8.28.0"
|
|
38
39
|
},
|
|
39
40
|
"peerDependencies": {
|
|
40
|
-
"react": "^
|
|
41
|
-
"react-dom": "^
|
|
41
|
+
"react": "^19.2.3",
|
|
42
|
+
"react-dom": "^19.2.3",
|
|
42
43
|
"zod": "^4.3.5"
|
|
43
44
|
}
|
|
44
45
|
}
|