@trackunit/react-core-hooks 0.2.262 → 0.2.264
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 +153 -2
- package/index.esm.js +152 -4
- package/package.json +3 -2
- package/src/index.d.ts +3 -0
- package/src/localStorage/initLocalStorageState.d.ts +9 -0
- package/src/localStorage/setLocalStorage.d.ts +11 -0
- package/src/localStorage/types.d.ts +27 -0
- package/src/localStorage/useLocalStorage.d.ts +10 -0
- package/src/localStorage/useLocalStorageEffect.d.ts +11 -0
- package/src/localStorage/useLocalStorageReducer.d.ts +13 -0
- package/src/localStorage/validateState.d.ts +13 -0
- package/src/usePrevious.d.ts +8 -0
package/index.cjs.js
CHANGED
|
@@ -25,7 +25,7 @@ function _interopNamespaceDefault(e) {
|
|
|
25
25
|
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
26
26
|
|
|
27
27
|
const AnalyticsContext = React.createContext(null);
|
|
28
|
-
const AnalyticsContextProvider = AnalyticsContext.Provider;
|
|
28
|
+
const AnalyticsContextProvider = AnalyticsContext.Provider; // easy import
|
|
29
29
|
/**
|
|
30
30
|
* Hook to get the analytics context.
|
|
31
31
|
*
|
|
@@ -157,7 +157,7 @@ const useEnvironment = () => {
|
|
|
157
157
|
};
|
|
158
158
|
|
|
159
159
|
const ErrorHandlingContext = React.createContext(null);
|
|
160
|
-
const ErrorHandlingContextProvider = ErrorHandlingContext.Provider;
|
|
160
|
+
const ErrorHandlingContextProvider = ErrorHandlingContext.Provider; // easy import
|
|
161
161
|
/**
|
|
162
162
|
* Hook to get the error handling context.
|
|
163
163
|
*
|
|
@@ -882,6 +882,154 @@ const useCurrentUser = () => {
|
|
|
882
882
|
return context;
|
|
883
883
|
};
|
|
884
884
|
|
|
885
|
+
/**
|
|
886
|
+
* Validates the state object using a Zod schema.
|
|
887
|
+
*
|
|
888
|
+
* @template TState - The type of the state.
|
|
889
|
+
* @param {ValidateStateOptions<TState>} params - The parameters for the validateState function.
|
|
890
|
+
*/
|
|
891
|
+
const validateState = ({ state, schema, onValidationFailed, onValidationSuccessful, defaultState, }) => {
|
|
892
|
+
try {
|
|
893
|
+
const parsedState = schema.parse(state);
|
|
894
|
+
onValidationSuccessful === null || onValidationSuccessful === void 0 ? void 0 : onValidationSuccessful(parsedState);
|
|
895
|
+
return parsedState;
|
|
896
|
+
}
|
|
897
|
+
catch (error) {
|
|
898
|
+
// eslint-disable-next-line no-console
|
|
899
|
+
console.error("Failed to parse and validate the state from local storage.", error);
|
|
900
|
+
onValidationFailed === null || onValidationFailed === void 0 ? void 0 : onValidationFailed(error);
|
|
901
|
+
return defaultState;
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
|
|
905
|
+
/**
|
|
906
|
+
* Initializes the state from local storage, parsing and validating it if a Zod schema is provided.
|
|
907
|
+
*
|
|
908
|
+
* @template TState - The type of the state stored in local storage.
|
|
909
|
+
* @param {Omit<LocalStorageParams<TState>, "state">} params - The parameters for initializing the local storage state.
|
|
910
|
+
* @returns {TState} - The initialized state.
|
|
911
|
+
*/
|
|
912
|
+
const initLocalStorageState = ({ key, defaultState, schema, }) => {
|
|
913
|
+
const localStorageItem = localStorage.getItem(key);
|
|
914
|
+
if (!localStorageItem) {
|
|
915
|
+
return defaultState;
|
|
916
|
+
}
|
|
917
|
+
const localStorageItemJSON = JSON.parse(localStorageItem);
|
|
918
|
+
if (!schema) {
|
|
919
|
+
return localStorageItemJSON;
|
|
920
|
+
}
|
|
921
|
+
return validateState({ state: localStorageItemJSON, defaultState, schema });
|
|
922
|
+
};
|
|
923
|
+
|
|
924
|
+
/**
|
|
925
|
+
* The usePrevious hook is a useful tool for tracking the previous value of a variable in a functional component. This can be particularly handy in scenarios where it is necessary to compare the current value with the previous one, such as triggering actions or rendering based on changes.
|
|
926
|
+
*
|
|
927
|
+
* @example
|
|
928
|
+
* const [color, setColor] = React.useState(getRandomColor());
|
|
929
|
+
const previousColor = usePrevious(color);
|
|
930
|
+
*/
|
|
931
|
+
const usePrevious = (value) => {
|
|
932
|
+
const ref = React.useRef();
|
|
933
|
+
React.useEffect(() => {
|
|
934
|
+
ref.current = value;
|
|
935
|
+
}, [value]);
|
|
936
|
+
return ref.current;
|
|
937
|
+
};
|
|
938
|
+
|
|
939
|
+
/**
|
|
940
|
+
* Sets a value in the local storage with the specified key.
|
|
941
|
+
* Thin wrapper around localStorage.setItem() that is slightly more type safe
|
|
942
|
+
* Stringifies value automatically.
|
|
943
|
+
* Useful if you for some reason can't use the useLocalStorage hook for React lifecycle reasons
|
|
944
|
+
*
|
|
945
|
+
* @template TValue - The type of value to be stored.
|
|
946
|
+
* @param {string} key - The key under which to store the value.
|
|
947
|
+
* @param {TValue} value - The value to store in the local storage.
|
|
948
|
+
*/
|
|
949
|
+
const setLocalStorage = (key, value) => {
|
|
950
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
951
|
+
};
|
|
952
|
+
|
|
953
|
+
/**
|
|
954
|
+
* Custom hook for synchronizing a state variable with local storage,
|
|
955
|
+
* with optional schema validation and callbacks.
|
|
956
|
+
*
|
|
957
|
+
* @template TState - The type of the state variable.
|
|
958
|
+
* @param {LocalStorageParams<TState> & LocalStorageCallbacks & { state: TState }} params - The parameters for the useLocalStorageEffect hook.
|
|
959
|
+
*/
|
|
960
|
+
const useLocalStorageEffect = ({ key, state, defaultState, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
961
|
+
const prevState = usePrevious(state);
|
|
962
|
+
React.useEffect(() => {
|
|
963
|
+
if (JSON.stringify(prevState) === JSON.stringify(state)) {
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
if (schema) {
|
|
967
|
+
validateState({
|
|
968
|
+
state,
|
|
969
|
+
schema,
|
|
970
|
+
defaultState,
|
|
971
|
+
onValidationFailed: error => {
|
|
972
|
+
// eslint-disable-next-line no-console
|
|
973
|
+
console.error(`State validation failed. Resetting local storage to default state: ${defaultState}.`, error);
|
|
974
|
+
localStorage.removeItem(key);
|
|
975
|
+
onValidationFailed === null || onValidationFailed === void 0 ? void 0 : onValidationFailed(error);
|
|
976
|
+
},
|
|
977
|
+
onValidationSuccessful: data => {
|
|
978
|
+
setLocalStorage(key, data);
|
|
979
|
+
onValidationSuccessful === null || onValidationSuccessful === void 0 ? void 0 : onValidationSuccessful(data);
|
|
980
|
+
},
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
else {
|
|
984
|
+
const stringifiedState = JSON.stringify(state);
|
|
985
|
+
localStorage.setItem(key, stringifiedState);
|
|
986
|
+
}
|
|
987
|
+
}, [state, key, schema, defaultState, prevState, onValidationFailed, onValidationSuccessful]);
|
|
988
|
+
};
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* Works like a normal useState, but saves to local storage and has optional schema validation.
|
|
992
|
+
*
|
|
993
|
+
* @template TState - The type of the value stored in local storage.
|
|
994
|
+
* @param {Omit<LocalStorageParams<TState>, "state"> & LocalStorageCallbacks} options - The options for useLocalStorage.
|
|
995
|
+
* @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.
|
|
996
|
+
*/
|
|
997
|
+
const useLocalStorage = ({ key, defaultState, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
998
|
+
if (!key) {
|
|
999
|
+
throw new Error("useLocalStorage key may not be falsy");
|
|
1000
|
+
}
|
|
1001
|
+
const [state, setState] = React.useState(initLocalStorageState({ key, defaultState, schema }));
|
|
1002
|
+
useLocalStorageEffect({
|
|
1003
|
+
key,
|
|
1004
|
+
state,
|
|
1005
|
+
defaultState,
|
|
1006
|
+
schema,
|
|
1007
|
+
onValidationFailed,
|
|
1008
|
+
onValidationSuccessful,
|
|
1009
|
+
});
|
|
1010
|
+
const reset = () => {
|
|
1011
|
+
setState(defaultState);
|
|
1012
|
+
};
|
|
1013
|
+
return [state, setState, reset];
|
|
1014
|
+
};
|
|
1015
|
+
|
|
1016
|
+
/**
|
|
1017
|
+
* Works like a normal useReducer, but saves to local storage and has optional schema validation.
|
|
1018
|
+
*
|
|
1019
|
+
* @template TState - The type of the state.
|
|
1020
|
+
* @template TAction - The type of the action.
|
|
1021
|
+
* @param {LocalStorageParams<TState> & LocalStorageCallbacks} params - The parameters for the useLocalStorageReducer function.
|
|
1022
|
+
* @returns {[TState, Dispatch<TAction>]} - A tuple containing the state and the dispatch function.
|
|
1023
|
+
*/
|
|
1024
|
+
const useLocalStorageReducer = ({ key, defaultState, reducer, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
1025
|
+
if (!key) {
|
|
1026
|
+
throw new Error("useLocalStorage key may not be falsy");
|
|
1027
|
+
}
|
|
1028
|
+
const [state, dispatch] = React.useReducer(reducer, defaultState, () => initLocalStorageState({ key, defaultState, schema }));
|
|
1029
|
+
useLocalStorageEffect({ key, state, defaultState, schema, onValidationFailed, onValidationSuccessful });
|
|
1030
|
+
return [state, dispatch];
|
|
1031
|
+
};
|
|
1032
|
+
|
|
885
1033
|
exports.AnalyticsContext = AnalyticsContext;
|
|
886
1034
|
exports.AnalyticsContextProvider = AnalyticsContextProvider;
|
|
887
1035
|
exports.AssetSortingProvider = AssetSortingProvider;
|
|
@@ -918,9 +1066,12 @@ exports.useImageUploader = useImageUploader;
|
|
|
918
1066
|
exports.useIrisAppId = useIrisAppId;
|
|
919
1067
|
exports.useIrisAppImage = useIrisAppImage;
|
|
920
1068
|
exports.useIrisAppName = useIrisAppName;
|
|
1069
|
+
exports.useLocalStorage = useLocalStorage;
|
|
1070
|
+
exports.useLocalStorageReducer = useLocalStorageReducer;
|
|
921
1071
|
exports.useModalDialogContext = useModalDialogContext;
|
|
922
1072
|
exports.useNavigateInHost = useNavigateInHost;
|
|
923
1073
|
exports.useOemBrandingContext = useOemBrandingContext;
|
|
1074
|
+
exports.usePrevious = usePrevious;
|
|
924
1075
|
exports.useSiteRuntime = useSiteRuntime;
|
|
925
1076
|
exports.useTextSearch = useTextSearch;
|
|
926
1077
|
exports.useToast = useToast;
|
package/index.esm.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import { createContext, useContext, useMemo, useState, useCallback, useEffect } from 'react';
|
|
2
|
+
import { createContext, useContext, useMemo, useState, useCallback, useEffect, useRef, useReducer } from 'react';
|
|
3
3
|
import { jsx } from 'react/jsx-runtime';
|
|
4
4
|
import { AssetRuntime, CustomerRuntime, EventRuntime, ParamsRuntime, SiteRuntime } from '@trackunit/iris-app-runtime-core';
|
|
5
5
|
import { filterByMultiple } from '@trackunit/shared-utils';
|
|
6
6
|
|
|
7
7
|
const AnalyticsContext = createContext(null);
|
|
8
|
-
const AnalyticsContextProvider = AnalyticsContext.Provider;
|
|
8
|
+
const AnalyticsContextProvider = AnalyticsContext.Provider; // easy import
|
|
9
9
|
/**
|
|
10
10
|
* Hook to get the analytics context.
|
|
11
11
|
*
|
|
@@ -137,7 +137,7 @@ const useEnvironment = () => {
|
|
|
137
137
|
};
|
|
138
138
|
|
|
139
139
|
const ErrorHandlingContext = createContext(null);
|
|
140
|
-
const ErrorHandlingContextProvider = ErrorHandlingContext.Provider;
|
|
140
|
+
const ErrorHandlingContextProvider = ErrorHandlingContext.Provider; // easy import
|
|
141
141
|
/**
|
|
142
142
|
* Hook to get the error handling context.
|
|
143
143
|
*
|
|
@@ -862,4 +862,152 @@ const useCurrentUser = () => {
|
|
|
862
862
|
return context;
|
|
863
863
|
};
|
|
864
864
|
|
|
865
|
-
|
|
865
|
+
/**
|
|
866
|
+
* Validates the state object using a Zod schema.
|
|
867
|
+
*
|
|
868
|
+
* @template TState - The type of the state.
|
|
869
|
+
* @param {ValidateStateOptions<TState>} params - The parameters for the validateState function.
|
|
870
|
+
*/
|
|
871
|
+
const validateState = ({ state, schema, onValidationFailed, onValidationSuccessful, defaultState, }) => {
|
|
872
|
+
try {
|
|
873
|
+
const parsedState = schema.parse(state);
|
|
874
|
+
onValidationSuccessful === null || onValidationSuccessful === void 0 ? void 0 : onValidationSuccessful(parsedState);
|
|
875
|
+
return parsedState;
|
|
876
|
+
}
|
|
877
|
+
catch (error) {
|
|
878
|
+
// eslint-disable-next-line no-console
|
|
879
|
+
console.error("Failed to parse and validate the state from local storage.", error);
|
|
880
|
+
onValidationFailed === null || onValidationFailed === void 0 ? void 0 : onValidationFailed(error);
|
|
881
|
+
return defaultState;
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
/**
|
|
886
|
+
* Initializes the state from local storage, parsing and validating it if a Zod schema is provided.
|
|
887
|
+
*
|
|
888
|
+
* @template TState - The type of the state stored in local storage.
|
|
889
|
+
* @param {Omit<LocalStorageParams<TState>, "state">} params - The parameters for initializing the local storage state.
|
|
890
|
+
* @returns {TState} - The initialized state.
|
|
891
|
+
*/
|
|
892
|
+
const initLocalStorageState = ({ key, defaultState, schema, }) => {
|
|
893
|
+
const localStorageItem = localStorage.getItem(key);
|
|
894
|
+
if (!localStorageItem) {
|
|
895
|
+
return defaultState;
|
|
896
|
+
}
|
|
897
|
+
const localStorageItemJSON = JSON.parse(localStorageItem);
|
|
898
|
+
if (!schema) {
|
|
899
|
+
return localStorageItemJSON;
|
|
900
|
+
}
|
|
901
|
+
return validateState({ state: localStorageItemJSON, defaultState, schema });
|
|
902
|
+
};
|
|
903
|
+
|
|
904
|
+
/**
|
|
905
|
+
* The usePrevious hook is a useful tool for tracking the previous value of a variable in a functional component. This can be particularly handy in scenarios where it is necessary to compare the current value with the previous one, such as triggering actions or rendering based on changes.
|
|
906
|
+
*
|
|
907
|
+
* @example
|
|
908
|
+
* const [color, setColor] = React.useState(getRandomColor());
|
|
909
|
+
const previousColor = usePrevious(color);
|
|
910
|
+
*/
|
|
911
|
+
const usePrevious = (value) => {
|
|
912
|
+
const ref = useRef();
|
|
913
|
+
useEffect(() => {
|
|
914
|
+
ref.current = value;
|
|
915
|
+
}, [value]);
|
|
916
|
+
return ref.current;
|
|
917
|
+
};
|
|
918
|
+
|
|
919
|
+
/**
|
|
920
|
+
* Sets a value in the local storage with the specified key.
|
|
921
|
+
* Thin wrapper around localStorage.setItem() that is slightly more type safe
|
|
922
|
+
* Stringifies value automatically.
|
|
923
|
+
* Useful if you for some reason can't use the useLocalStorage hook for React lifecycle reasons
|
|
924
|
+
*
|
|
925
|
+
* @template TValue - The type of value to be stored.
|
|
926
|
+
* @param {string} key - The key under which to store the value.
|
|
927
|
+
* @param {TValue} value - The value to store in the local storage.
|
|
928
|
+
*/
|
|
929
|
+
const setLocalStorage = (key, value) => {
|
|
930
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
931
|
+
};
|
|
932
|
+
|
|
933
|
+
/**
|
|
934
|
+
* Custom hook for synchronizing a state variable with local storage,
|
|
935
|
+
* with optional schema validation and callbacks.
|
|
936
|
+
*
|
|
937
|
+
* @template TState - The type of the state variable.
|
|
938
|
+
* @param {LocalStorageParams<TState> & LocalStorageCallbacks & { state: TState }} params - The parameters for the useLocalStorageEffect hook.
|
|
939
|
+
*/
|
|
940
|
+
const useLocalStorageEffect = ({ key, state, defaultState, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
941
|
+
const prevState = usePrevious(state);
|
|
942
|
+
useEffect(() => {
|
|
943
|
+
if (JSON.stringify(prevState) === JSON.stringify(state)) {
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
if (schema) {
|
|
947
|
+
validateState({
|
|
948
|
+
state,
|
|
949
|
+
schema,
|
|
950
|
+
defaultState,
|
|
951
|
+
onValidationFailed: error => {
|
|
952
|
+
// eslint-disable-next-line no-console
|
|
953
|
+
console.error(`State validation failed. Resetting local storage to default state: ${defaultState}.`, error);
|
|
954
|
+
localStorage.removeItem(key);
|
|
955
|
+
onValidationFailed === null || onValidationFailed === void 0 ? void 0 : onValidationFailed(error);
|
|
956
|
+
},
|
|
957
|
+
onValidationSuccessful: data => {
|
|
958
|
+
setLocalStorage(key, data);
|
|
959
|
+
onValidationSuccessful === null || onValidationSuccessful === void 0 ? void 0 : onValidationSuccessful(data);
|
|
960
|
+
},
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
else {
|
|
964
|
+
const stringifiedState = JSON.stringify(state);
|
|
965
|
+
localStorage.setItem(key, stringifiedState);
|
|
966
|
+
}
|
|
967
|
+
}, [state, key, schema, defaultState, prevState, onValidationFailed, onValidationSuccessful]);
|
|
968
|
+
};
|
|
969
|
+
|
|
970
|
+
/**
|
|
971
|
+
* Works like a normal useState, but saves to local storage and has optional schema validation.
|
|
972
|
+
*
|
|
973
|
+
* @template TState - The type of the value stored in local storage.
|
|
974
|
+
* @param {Omit<LocalStorageParams<TState>, "state"> & LocalStorageCallbacks} options - The options for useLocalStorage.
|
|
975
|
+
* @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.
|
|
976
|
+
*/
|
|
977
|
+
const useLocalStorage = ({ key, defaultState, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
978
|
+
if (!key) {
|
|
979
|
+
throw new Error("useLocalStorage key may not be falsy");
|
|
980
|
+
}
|
|
981
|
+
const [state, setState] = useState(initLocalStorageState({ key, defaultState, schema }));
|
|
982
|
+
useLocalStorageEffect({
|
|
983
|
+
key,
|
|
984
|
+
state,
|
|
985
|
+
defaultState,
|
|
986
|
+
schema,
|
|
987
|
+
onValidationFailed,
|
|
988
|
+
onValidationSuccessful,
|
|
989
|
+
});
|
|
990
|
+
const reset = () => {
|
|
991
|
+
setState(defaultState);
|
|
992
|
+
};
|
|
993
|
+
return [state, setState, reset];
|
|
994
|
+
};
|
|
995
|
+
|
|
996
|
+
/**
|
|
997
|
+
* Works like a normal useReducer, but saves to local storage and has optional schema validation.
|
|
998
|
+
*
|
|
999
|
+
* @template TState - The type of the state.
|
|
1000
|
+
* @template TAction - The type of the action.
|
|
1001
|
+
* @param {LocalStorageParams<TState> & LocalStorageCallbacks} params - The parameters for the useLocalStorageReducer function.
|
|
1002
|
+
* @returns {[TState, Dispatch<TAction>]} - A tuple containing the state and the dispatch function.
|
|
1003
|
+
*/
|
|
1004
|
+
const useLocalStorageReducer = ({ key, defaultState, reducer, schema, onValidationFailed, onValidationSuccessful, }) => {
|
|
1005
|
+
if (!key) {
|
|
1006
|
+
throw new Error("useLocalStorage key may not be falsy");
|
|
1007
|
+
}
|
|
1008
|
+
const [state, dispatch] = useReducer(reducer, defaultState, () => initLocalStorageState({ key, defaultState, schema }));
|
|
1009
|
+
useLocalStorageEffect({ key, state, defaultState, schema, onValidationFailed, onValidationSuccessful });
|
|
1010
|
+
return [state, dispatch];
|
|
1011
|
+
};
|
|
1012
|
+
|
|
1013
|
+
export { AnalyticsContext, AnalyticsContextProvider, AssetSortingProvider, ConfirmationDialogProvider, CurrentUserPreferenceProvider, CurrentUserProvider, EnvironmentContextProvider, ErrorHandlingContext, ErrorHandlingContextProvider, FilterBarProvider, ModalDialogContextProvider, NavigationContextProvider, OemBrandingContextProvider, ToastProvider, TokenProvider, UserSubscriptionProvider, fetchAssetBlobUrl, useAnalytics, useAssetRuntime, useAssetSorting, useConfirmationDialog, useCurrentUser, useCurrentUserLanguage, useCurrentUserSystemOfMeasurement, useCurrentUserTimeZonePreference, useCustomerRuntime, useEnvironment, useErrorHandler, useEventRuntime, useFeatureBranchQueryString, useFilterBarContext, useHasAccessTo, useImageUploader, useIrisAppId, useIrisAppImage, useIrisAppName, useLocalStorage, useLocalStorageReducer, useModalDialogContext, useNavigateInHost, useOemBrandingContext, usePrevious, useSiteRuntime, useTextSearch, useToast, useToken, useUserSubscription };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-core-hooks",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.264",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"react": "18.3.1",
|
|
12
12
|
"@trackunit/iris-app-runtime-core": "*",
|
|
13
13
|
"jest-fetch-mock": "^3.0.3",
|
|
14
|
-
"@trackunit/shared-utils": "*"
|
|
14
|
+
"@trackunit/shared-utils": "*",
|
|
15
|
+
"zod": "3.22.4"
|
|
15
16
|
},
|
|
16
17
|
"module": "./index.esm.js",
|
|
17
18
|
"main": "./index.cjs.js",
|
package/src/index.d.ts
CHANGED
|
@@ -22,3 +22,6 @@ export * from "./useFeatureBranchQueryString";
|
|
|
22
22
|
export * from "./useTextSearch";
|
|
23
23
|
export * from "./user/CurrentUserPreferenceProvider";
|
|
24
24
|
export * from "./user/CurrentUserProvider";
|
|
25
|
+
export * from "./localStorage/useLocalStorage";
|
|
26
|
+
export * from "./localStorage/useLocalStorageReducer";
|
|
27
|
+
export * from "./usePrevious";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { LocalStorageParams } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Initializes the state from local storage, parsing and validating it if a Zod schema is provided.
|
|
4
|
+
*
|
|
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
|
+
*/
|
|
9
|
+
export declare const initLocalStorageState: <TState>({ key, defaultState, schema, }: Omit<LocalStorageParams<TState>, "state">) => any;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets a value in the local storage with the specified key.
|
|
3
|
+
* Thin wrapper around localStorage.setItem() that is slightly more type safe
|
|
4
|
+
* Stringifies value automatically.
|
|
5
|
+
* Useful if you for some reason can't use the useLocalStorage hook for React lifecycle reasons
|
|
6
|
+
*
|
|
7
|
+
* @template TValue - The type of value to be stored.
|
|
8
|
+
* @param {string} key - The key under which to store the value.
|
|
9
|
+
* @param {TValue} value - The value to store in the local storage.
|
|
10
|
+
*/
|
|
11
|
+
export declare const setLocalStorage: <TValue>(key: string, value: TValue) => void;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export interface LocalStorageParams<TState> {
|
|
3
|
+
/**
|
|
4
|
+
* The key used to store the value in local storage.
|
|
5
|
+
*/
|
|
6
|
+
key: string;
|
|
7
|
+
/**
|
|
8
|
+
* The default state value.
|
|
9
|
+
*/
|
|
10
|
+
defaultState: TState;
|
|
11
|
+
/**
|
|
12
|
+
* Optional schema for validating the state.
|
|
13
|
+
*/
|
|
14
|
+
schema?: z.Schema<TState>;
|
|
15
|
+
}
|
|
16
|
+
export interface LocalStorageCallbacks<TState> {
|
|
17
|
+
/**
|
|
18
|
+
* Optional callback function called when validation fails.
|
|
19
|
+
*
|
|
20
|
+
* @param error - The validation error object.
|
|
21
|
+
*/
|
|
22
|
+
onValidationFailed?: (error: unknown) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Optional callback function called when validation succeeds.
|
|
25
|
+
*/
|
|
26
|
+
onValidationSuccessful?: (data: TState) => void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Dispatch, SetStateAction } from "react";
|
|
2
|
+
import { LocalStorageCallbacks, LocalStorageParams } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Works like a normal useState, but saves to local storage and has optional schema validation.
|
|
5
|
+
*
|
|
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
|
+
*/
|
|
10
|
+
export declare const useLocalStorage: <TState>({ key, defaultState, schema, onValidationFailed, onValidationSuccessful, }: LocalStorageParams<TState> & LocalStorageCallbacks<TState>) => readonly [TState, Dispatch<SetStateAction<TState>>, () => void];
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { LocalStorageCallbacks, LocalStorageParams } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Custom hook for synchronizing a state variable with local storage,
|
|
4
|
+
* with optional schema validation and callbacks.
|
|
5
|
+
*
|
|
6
|
+
* @template TState - The type of the state variable.
|
|
7
|
+
* @param {LocalStorageParams<TState> & LocalStorageCallbacks & { state: TState }} params - The parameters for the useLocalStorageEffect hook.
|
|
8
|
+
*/
|
|
9
|
+
export declare const useLocalStorageEffect: <TState>({ key, state, defaultState, schema, onValidationFailed, onValidationSuccessful, }: LocalStorageParams<TState> & LocalStorageCallbacks<TState> & {
|
|
10
|
+
state: TState;
|
|
11
|
+
}) => void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Dispatch } from "react";
|
|
2
|
+
import { LocalStorageCallbacks, LocalStorageParams } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Works like a normal useReducer, but saves to local storage and has optional schema validation.
|
|
5
|
+
*
|
|
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
|
+
*/
|
|
11
|
+
export declare const useLocalStorageReducer: <TState, TAction>({ key, defaultState, reducer, schema, onValidationFailed, onValidationSuccessful, }: {
|
|
12
|
+
reducer: (state: TState, action: TAction) => TState;
|
|
13
|
+
} & LocalStorageParams<TState> & LocalStorageCallbacks<TState>) => readonly [TState, Dispatch<TAction>];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { LocalStorageCallbacks, LocalStorageParams } from "./types";
|
|
2
|
+
type ValidateStateOptions<TState> = Required<Pick<LocalStorageParams<TState>, "schema">> & {
|
|
3
|
+
state: TState;
|
|
4
|
+
defaultState: TState;
|
|
5
|
+
} & LocalStorageCallbacks<TState>;
|
|
6
|
+
/**
|
|
7
|
+
* Validates the state object using a Zod schema.
|
|
8
|
+
*
|
|
9
|
+
* @template TState - The type of the state.
|
|
10
|
+
* @param {ValidateStateOptions<TState>} params - The parameters for the validateState function.
|
|
11
|
+
*/
|
|
12
|
+
export declare const validateState: <TState>({ state, schema, onValidationFailed, onValidationSuccessful, defaultState, }: ValidateStateOptions<TState>) => TState;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The usePrevious hook is a useful tool for tracking the previous value of a variable in a functional component. This can be particularly handy in scenarios where it is necessary to compare the current value with the previous one, such as triggering actions or rendering based on changes.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* const [color, setColor] = React.useState(getRandomColor());
|
|
6
|
+
const previousColor = usePrevious(color);
|
|
7
|
+
*/
|
|
8
|
+
export declare const usePrevious: <TValue>(value: TValue) => TValue | undefined;
|