@trackunit/react-components 0.1.177-alpha-5bf259599b.0 → 0.1.178
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/index.cjs.js +31 -43
- package/index.esm.js +31 -43
- package/package.json +6 -6
- package/src/hooks/localStorage/initLocalStorageState.d.ts +4 -4
- package/src/hooks/localStorage/useLocalStorage.d.ts +4 -4
- package/src/hooks/localStorage/useLocalStorageEffect.d.ts +4 -4
- package/src/hooks/localStorage/useLocalStorageReducer.d.ts +7 -7
- package/src/hooks/localStorage/validateState.d.ts +4 -4
package/index.cjs.js
CHANGED
|
@@ -21647,55 +21647,42 @@ const setLocalStorage = (key, value) => {
|
|
|
21647
21647
|
/**
|
|
21648
21648
|
* Validates the state object using a Zod schema.
|
|
21649
21649
|
*
|
|
21650
|
-
* @template
|
|
21651
|
-
* @param {ValidateStateOptions<
|
|
21652
|
-
* @returns {boolean} - `true` if the state is valid according to the schema, `false` otherwise.
|
|
21650
|
+
* @template TState - The type of the state.
|
|
21651
|
+
* @param {ValidateStateOptions<TState>} params - The parameters for the validateState function.
|
|
21653
21652
|
*/
|
|
21654
|
-
const validateState = ({ state, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
21653
|
+
const validateState = ({ state, schema, onValidationFailed, onValidationSuccessful, defaultState, }) => {
|
|
21655
21654
|
try {
|
|
21656
|
-
schema.parse(state);
|
|
21657
|
-
|
|
21658
|
-
|
|
21659
|
-
}
|
|
21660
|
-
return true;
|
|
21655
|
+
const parsedState = schema.parse(state);
|
|
21656
|
+
onValidationSuccessful === null || onValidationSuccessful === void 0 ? void 0 : onValidationSuccessful();
|
|
21657
|
+
return parsedState;
|
|
21661
21658
|
}
|
|
21662
21659
|
catch (error) {
|
|
21663
21660
|
// eslint-disable-next-line no-console
|
|
21664
|
-
console.error("
|
|
21665
|
-
|
|
21666
|
-
|
|
21667
|
-
}
|
|
21668
|
-
return false;
|
|
21661
|
+
console.error("Failed to parse and validate the state from local storage.", error);
|
|
21662
|
+
onValidationFailed === null || onValidationFailed === void 0 ? void 0 : onValidationFailed(error);
|
|
21663
|
+
return defaultState;
|
|
21669
21664
|
}
|
|
21670
21665
|
};
|
|
21671
21666
|
|
|
21672
21667
|
/**
|
|
21673
21668
|
* Initializes the state from local storage, parsing and validating it if a Zod schema is provided.
|
|
21674
21669
|
*
|
|
21675
|
-
* @template
|
|
21676
|
-
* @param {Omit<LocalStorageParams<
|
|
21677
|
-
* @returns {
|
|
21670
|
+
* @template TState - The type of the state stored in local storage.
|
|
21671
|
+
* @param {Omit<LocalStorageParams<TState>, "state">} params - The parameters for initializing the local storage state.
|
|
21672
|
+
* @returns {TState} - The initialized state.
|
|
21678
21673
|
*/
|
|
21679
21674
|
const initLocalStorageState = ({ key, defaultState, schema, }) => {
|
|
21680
21675
|
const localStorageItem = localStorage.getItem(key);
|
|
21681
|
-
if (localStorageItem) {
|
|
21682
|
-
|
|
21683
|
-
|
|
21684
|
-
|
|
21685
|
-
|
|
21686
|
-
|
|
21687
|
-
|
|
21688
|
-
|
|
21689
|
-
|
|
21690
|
-
return parsedState;
|
|
21691
|
-
}
|
|
21692
|
-
}
|
|
21693
|
-
catch (error) {
|
|
21694
|
-
// eslint-disable-next-line no-console
|
|
21695
|
-
console.error("Failed to parse and validate the state from local storage.", error);
|
|
21696
|
-
}
|
|
21676
|
+
if (!localStorageItem) {
|
|
21677
|
+
return defaultState;
|
|
21678
|
+
}
|
|
21679
|
+
const parsedState = JSON.parse(localStorageItem);
|
|
21680
|
+
if (schema) {
|
|
21681
|
+
return validateState({ state: parsedState, defaultState, schema });
|
|
21682
|
+
}
|
|
21683
|
+
else {
|
|
21684
|
+
return parsedState;
|
|
21697
21685
|
}
|
|
21698
|
-
return defaultState;
|
|
21699
21686
|
};
|
|
21700
21687
|
|
|
21701
21688
|
var lodash = {exports: {}};
|
|
@@ -38903,8 +38890,8 @@ var lodash = {exports: {}};
|
|
|
38903
38890
|
* Custom hook for synchronizing a state variable with local storage,
|
|
38904
38891
|
* with optional schema validation and callbacks.
|
|
38905
38892
|
*
|
|
38906
|
-
* @template
|
|
38907
|
-
* @param {LocalStorageParams<
|
|
38893
|
+
* @template TState - The type of the state variable.
|
|
38894
|
+
* @param {LocalStorageParams<TState> & LocalStorageCallbacks & { state: TState }} params - The parameters for the useLocalStorageEffect hook.
|
|
38908
38895
|
*/
|
|
38909
38896
|
const useLocalStorageEffect = ({ key, state, defaultState, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
38910
38897
|
const prevState = usePrevious(state);
|
|
@@ -38916,6 +38903,7 @@ const useLocalStorageEffect = ({ key, state, defaultState, schema, onValidationF
|
|
|
38916
38903
|
validateState({
|
|
38917
38904
|
state,
|
|
38918
38905
|
schema,
|
|
38906
|
+
defaultState,
|
|
38919
38907
|
onValidationFailed: error => {
|
|
38920
38908
|
// eslint-disable-next-line no-console
|
|
38921
38909
|
console.error(`State validation failed. Resetting local storage to default state: ${defaultState}.`, error);
|
|
@@ -38938,9 +38926,9 @@ const useLocalStorageEffect = ({ key, state, defaultState, schema, onValidationF
|
|
|
38938
38926
|
/**
|
|
38939
38927
|
* Works like a normal useState, but saves to local storage and has optional schema validation.
|
|
38940
38928
|
*
|
|
38941
|
-
* @template
|
|
38942
|
-
* @param {Omit<LocalStorageParams<
|
|
38943
|
-
* @returns {[
|
|
38929
|
+
* @template TState - The type of the value stored in local storage.
|
|
38930
|
+
* @param {Omit<LocalStorageParams<TState>, "state"> & LocalStorageCallbacks} options - The options for useLocalStorage.
|
|
38931
|
+
* @returns {[TState, Dispatch<SetStateAction<TState>>, () => void]} - A tuple containing the current value, a function to update the value, and a function to remove the value from local storage.
|
|
38944
38932
|
*/
|
|
38945
38933
|
const useLocalStorage = ({ key, defaultState, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
38946
38934
|
if (!key) {
|
|
@@ -38957,10 +38945,10 @@ const useLocalStorage = ({ key, defaultState, schema, onValidationFailed, onVali
|
|
|
38957
38945
|
/**
|
|
38958
38946
|
* Works like a normal useReducer, but saves to local storage and has optional schema validation.
|
|
38959
38947
|
*
|
|
38960
|
-
* @template
|
|
38961
|
-
* @template
|
|
38962
|
-
* @param {LocalStorageParams<
|
|
38963
|
-
* @returns {[
|
|
38948
|
+
* @template TState - The type of the state.
|
|
38949
|
+
* @template TAction - The type of the action.
|
|
38950
|
+
* @param {LocalStorageParams<TState> & LocalStorageCallbacks} params - The parameters for the useLocalStorageReducer function.
|
|
38951
|
+
* @returns {[TState, Dispatch<TAction>]} - A tuple containing the state and the dispatch function.
|
|
38964
38952
|
*/
|
|
38965
38953
|
const useLocalStorageReducer = ({ key, defaultState, reducer, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
38966
38954
|
if (!key) {
|
package/index.esm.js
CHANGED
|
@@ -21618,55 +21618,42 @@ const setLocalStorage = (key, value) => {
|
|
|
21618
21618
|
/**
|
|
21619
21619
|
* Validates the state object using a Zod schema.
|
|
21620
21620
|
*
|
|
21621
|
-
* @template
|
|
21622
|
-
* @param {ValidateStateOptions<
|
|
21623
|
-
* @returns {boolean} - `true` if the state is valid according to the schema, `false` otherwise.
|
|
21621
|
+
* @template TState - The type of the state.
|
|
21622
|
+
* @param {ValidateStateOptions<TState>} params - The parameters for the validateState function.
|
|
21624
21623
|
*/
|
|
21625
|
-
const validateState = ({ state, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
21624
|
+
const validateState = ({ state, schema, onValidationFailed, onValidationSuccessful, defaultState, }) => {
|
|
21626
21625
|
try {
|
|
21627
|
-
schema.parse(state);
|
|
21628
|
-
|
|
21629
|
-
|
|
21630
|
-
}
|
|
21631
|
-
return true;
|
|
21626
|
+
const parsedState = schema.parse(state);
|
|
21627
|
+
onValidationSuccessful === null || onValidationSuccessful === void 0 ? void 0 : onValidationSuccessful();
|
|
21628
|
+
return parsedState;
|
|
21632
21629
|
}
|
|
21633
21630
|
catch (error) {
|
|
21634
21631
|
// eslint-disable-next-line no-console
|
|
21635
|
-
console.error("
|
|
21636
|
-
|
|
21637
|
-
|
|
21638
|
-
}
|
|
21639
|
-
return false;
|
|
21632
|
+
console.error("Failed to parse and validate the state from local storage.", error);
|
|
21633
|
+
onValidationFailed === null || onValidationFailed === void 0 ? void 0 : onValidationFailed(error);
|
|
21634
|
+
return defaultState;
|
|
21640
21635
|
}
|
|
21641
21636
|
};
|
|
21642
21637
|
|
|
21643
21638
|
/**
|
|
21644
21639
|
* Initializes the state from local storage, parsing and validating it if a Zod schema is provided.
|
|
21645
21640
|
*
|
|
21646
|
-
* @template
|
|
21647
|
-
* @param {Omit<LocalStorageParams<
|
|
21648
|
-
* @returns {
|
|
21641
|
+
* @template TState - The type of the state stored in local storage.
|
|
21642
|
+
* @param {Omit<LocalStorageParams<TState>, "state">} params - The parameters for initializing the local storage state.
|
|
21643
|
+
* @returns {TState} - The initialized state.
|
|
21649
21644
|
*/
|
|
21650
21645
|
const initLocalStorageState = ({ key, defaultState, schema, }) => {
|
|
21651
21646
|
const localStorageItem = localStorage.getItem(key);
|
|
21652
|
-
if (localStorageItem) {
|
|
21653
|
-
|
|
21654
|
-
|
|
21655
|
-
|
|
21656
|
-
|
|
21657
|
-
|
|
21658
|
-
|
|
21659
|
-
|
|
21660
|
-
|
|
21661
|
-
return parsedState;
|
|
21662
|
-
}
|
|
21663
|
-
}
|
|
21664
|
-
catch (error) {
|
|
21665
|
-
// eslint-disable-next-line no-console
|
|
21666
|
-
console.error("Failed to parse and validate the state from local storage.", error);
|
|
21667
|
-
}
|
|
21647
|
+
if (!localStorageItem) {
|
|
21648
|
+
return defaultState;
|
|
21649
|
+
}
|
|
21650
|
+
const parsedState = JSON.parse(localStorageItem);
|
|
21651
|
+
if (schema) {
|
|
21652
|
+
return validateState({ state: parsedState, defaultState, schema });
|
|
21653
|
+
}
|
|
21654
|
+
else {
|
|
21655
|
+
return parsedState;
|
|
21668
21656
|
}
|
|
21669
|
-
return defaultState;
|
|
21670
21657
|
};
|
|
21671
21658
|
|
|
21672
21659
|
var lodash = {exports: {}};
|
|
@@ -38874,8 +38861,8 @@ var lodash = {exports: {}};
|
|
|
38874
38861
|
* Custom hook for synchronizing a state variable with local storage,
|
|
38875
38862
|
* with optional schema validation and callbacks.
|
|
38876
38863
|
*
|
|
38877
|
-
* @template
|
|
38878
|
-
* @param {LocalStorageParams<
|
|
38864
|
+
* @template TState - The type of the state variable.
|
|
38865
|
+
* @param {LocalStorageParams<TState> & LocalStorageCallbacks & { state: TState }} params - The parameters for the useLocalStorageEffect hook.
|
|
38879
38866
|
*/
|
|
38880
38867
|
const useLocalStorageEffect = ({ key, state, defaultState, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
38881
38868
|
const prevState = usePrevious(state);
|
|
@@ -38887,6 +38874,7 @@ const useLocalStorageEffect = ({ key, state, defaultState, schema, onValidationF
|
|
|
38887
38874
|
validateState({
|
|
38888
38875
|
state,
|
|
38889
38876
|
schema,
|
|
38877
|
+
defaultState,
|
|
38890
38878
|
onValidationFailed: error => {
|
|
38891
38879
|
// eslint-disable-next-line no-console
|
|
38892
38880
|
console.error(`State validation failed. Resetting local storage to default state: ${defaultState}.`, error);
|
|
@@ -38909,9 +38897,9 @@ const useLocalStorageEffect = ({ key, state, defaultState, schema, onValidationF
|
|
|
38909
38897
|
/**
|
|
38910
38898
|
* Works like a normal useState, but saves to local storage and has optional schema validation.
|
|
38911
38899
|
*
|
|
38912
|
-
* @template
|
|
38913
|
-
* @param {Omit<LocalStorageParams<
|
|
38914
|
-
* @returns {[
|
|
38900
|
+
* @template TState - The type of the value stored in local storage.
|
|
38901
|
+
* @param {Omit<LocalStorageParams<TState>, "state"> & LocalStorageCallbacks} options - The options for useLocalStorage.
|
|
38902
|
+
* @returns {[TState, Dispatch<SetStateAction<TState>>, () => void]} - A tuple containing the current value, a function to update the value, and a function to remove the value from local storage.
|
|
38915
38903
|
*/
|
|
38916
38904
|
const useLocalStorage = ({ key, defaultState, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
38917
38905
|
if (!key) {
|
|
@@ -38928,10 +38916,10 @@ const useLocalStorage = ({ key, defaultState, schema, onValidationFailed, onVali
|
|
|
38928
38916
|
/**
|
|
38929
38917
|
* Works like a normal useReducer, but saves to local storage and has optional schema validation.
|
|
38930
38918
|
*
|
|
38931
|
-
* @template
|
|
38932
|
-
* @template
|
|
38933
|
-
* @param {LocalStorageParams<
|
|
38934
|
-
* @returns {[
|
|
38919
|
+
* @template TState - The type of the state.
|
|
38920
|
+
* @template TAction - The type of the action.
|
|
38921
|
+
* @param {LocalStorageParams<TState> & LocalStorageCallbacks} params - The parameters for the useLocalStorageReducer function.
|
|
38922
|
+
* @returns {[TState, Dispatch<TAction>]} - A tuple containing the state and the dispatch function.
|
|
38935
38923
|
*/
|
|
38936
38924
|
const useLocalStorageReducer = ({ key, defaultState, reducer, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
38937
38925
|
if (!key) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-components",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.178",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@floating-ui/react": "0.25.4",
|
|
11
|
-
"@trackunit/css-class-variance-utilities": "0.0.
|
|
12
|
-
"@trackunit/css-core": "0.0.
|
|
13
|
-
"@trackunit/shared-utils": "0.0.
|
|
14
|
-
"@trackunit/ui-design-tokens": "0.0.
|
|
15
|
-
"@trackunit/ui-icons": "0.0.
|
|
11
|
+
"@trackunit/css-class-variance-utilities": "0.0.14",
|
|
12
|
+
"@trackunit/css-core": "0.0.97",
|
|
13
|
+
"@trackunit/shared-utils": "0.0.11",
|
|
14
|
+
"@trackunit/ui-design-tokens": "0.0.78",
|
|
15
|
+
"@trackunit/ui-icons": "0.0.78",
|
|
16
16
|
"date-fns": "2.29.3",
|
|
17
17
|
"lodash": "4.17.21",
|
|
18
18
|
"react": "18.2.0",
|
|
@@ -2,8 +2,8 @@ import { LocalStorageParams } from "./types";
|
|
|
2
2
|
/**
|
|
3
3
|
* Initializes the state from local storage, parsing and validating it if a Zod schema is provided.
|
|
4
4
|
*
|
|
5
|
-
* @template
|
|
6
|
-
* @param {Omit<LocalStorageParams<
|
|
7
|
-
* @returns {
|
|
5
|
+
* @template TState - The type of the state stored in local storage.
|
|
6
|
+
* @param {Omit<LocalStorageParams<TState>, "state">} params - The parameters for initializing the local storage state.
|
|
7
|
+
* @returns {TState} - The initialized state.
|
|
8
8
|
*/
|
|
9
|
-
export declare const initLocalStorageState: <
|
|
9
|
+
export declare const initLocalStorageState: <TState>({ key, defaultState, schema, }: Omit<LocalStorageParams<TState>, "state">) => any;
|
|
@@ -3,8 +3,8 @@ import { LocalStorageCallbacks, LocalStorageParams } from "./types";
|
|
|
3
3
|
/**
|
|
4
4
|
* Works like a normal useState, but saves to local storage and has optional schema validation.
|
|
5
5
|
*
|
|
6
|
-
* @template
|
|
7
|
-
* @param {Omit<LocalStorageParams<
|
|
8
|
-
* @returns {[
|
|
6
|
+
* @template TState - The type of the value stored in local storage.
|
|
7
|
+
* @param {Omit<LocalStorageParams<TState>, "state"> & LocalStorageCallbacks} options - The options for useLocalStorage.
|
|
8
|
+
* @returns {[TState, Dispatch<SetStateAction<TState>>, () => void]} - A tuple containing the current value, a function to update the value, and a function to remove the value from local storage.
|
|
9
9
|
*/
|
|
10
|
-
export declare const useLocalStorage: <
|
|
10
|
+
export declare const useLocalStorage: <TState>({ key, defaultState, schema, onValidationFailed, onValidationSuccessful, }: LocalStorageParams<TState> & LocalStorageCallbacks) => readonly [TState, Dispatch<SetStateAction<TState>>, () => void];
|
|
@@ -3,9 +3,9 @@ import { LocalStorageCallbacks, LocalStorageParams } from "./types";
|
|
|
3
3
|
* Custom hook for synchronizing a state variable with local storage,
|
|
4
4
|
* with optional schema validation and callbacks.
|
|
5
5
|
*
|
|
6
|
-
* @template
|
|
7
|
-
* @param {LocalStorageParams<
|
|
6
|
+
* @template TState - The type of the state variable.
|
|
7
|
+
* @param {LocalStorageParams<TState> & LocalStorageCallbacks & { state: TState }} params - The parameters for the useLocalStorageEffect hook.
|
|
8
8
|
*/
|
|
9
|
-
export declare const useLocalStorageEffect: <
|
|
10
|
-
state:
|
|
9
|
+
export declare const useLocalStorageEffect: <TState>({ key, state, defaultState, schema, onValidationFailed, onValidationSuccessful, }: LocalStorageParams<TState> & LocalStorageCallbacks & {
|
|
10
|
+
state: TState;
|
|
11
11
|
}) => void;
|
|
@@ -3,11 +3,11 @@ import { LocalStorageCallbacks, LocalStorageParams } from "./types";
|
|
|
3
3
|
/**
|
|
4
4
|
* Works like a normal useReducer, but saves to local storage and has optional schema validation.
|
|
5
5
|
*
|
|
6
|
-
* @template
|
|
7
|
-
* @template
|
|
8
|
-
* @param {LocalStorageParams<
|
|
9
|
-
* @returns {[
|
|
6
|
+
* @template TState - The type of the state.
|
|
7
|
+
* @template TAction - The type of the action.
|
|
8
|
+
* @param {LocalStorageParams<TState> & LocalStorageCallbacks} params - The parameters for the useLocalStorageReducer function.
|
|
9
|
+
* @returns {[TState, Dispatch<TAction>]} - A tuple containing the state and the dispatch function.
|
|
10
10
|
*/
|
|
11
|
-
export declare const useLocalStorageReducer: <
|
|
12
|
-
reducer: (state:
|
|
13
|
-
} & LocalStorageParams<
|
|
11
|
+
export declare const useLocalStorageReducer: <TState, TAction>({ key, defaultState, reducer, schema, onValidationFailed, onValidationSuccessful, }: {
|
|
12
|
+
reducer: (state: TState, action: TAction) => TState;
|
|
13
|
+
} & LocalStorageParams<TState> & LocalStorageCallbacks) => readonly [TState, Dispatch<TAction>];
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { LocalStorageCallbacks, LocalStorageParams } from "./types";
|
|
2
2
|
type ValidateStateOptions<State> = Required<Pick<LocalStorageParams<State>, "schema">> & {
|
|
3
3
|
state: State;
|
|
4
|
+
defaultState: State;
|
|
4
5
|
} & LocalStorageCallbacks;
|
|
5
6
|
/**
|
|
6
7
|
* Validates the state object using a Zod schema.
|
|
7
8
|
*
|
|
8
|
-
* @template
|
|
9
|
-
* @param {ValidateStateOptions<
|
|
10
|
-
* @returns {boolean} - `true` if the state is valid according to the schema, `false` otherwise.
|
|
9
|
+
* @template TState - The type of the state.
|
|
10
|
+
* @param {ValidateStateOptions<TState>} params - The parameters for the validateState function.
|
|
11
11
|
*/
|
|
12
|
-
export declare const validateState: <
|
|
12
|
+
export declare const validateState: <TState>({ state, schema, onValidationFailed, onValidationSuccessful, defaultState, }: ValidateStateOptions<TState>) => TState;
|
|
13
13
|
export {};
|