@monkvision/common 4.0.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/LICENSE +32 -0
- package/README/APP_UTILS.md +52 -0
- package/README/HOOKS.md +242 -0
- package/README/INTERNATIONALIZATION.md +89 -0
- package/README/STATE_MANAGEMENT.md +125 -0
- package/README/THEMING.md +70 -0
- package/README/UTILITIES.md +254 -0
- package/README.md +23 -0
- package/lib/PreventExit/hooks.d.ts +15 -0
- package/lib/PreventExit/hooks.js +27 -0
- package/lib/PreventExit/hooks.js.map +1 -0
- package/lib/PreventExit/index.d.ts +1 -0
- package/lib/PreventExit/index.js +18 -0
- package/lib/PreventExit/index.js.map +1 -0
- package/lib/PreventExit/store.d.ts +23 -0
- package/lib/PreventExit/store.js +41 -0
- package/lib/PreventExit/store.js.map +1 -0
- package/lib/apps/analytics.d.ts +2 -0
- package/lib/apps/analytics.js +16 -0
- package/lib/apps/analytics.js.map +1 -0
- package/lib/apps/appState.d.ts +61 -0
- package/lib/apps/appState.js +11 -0
- package/lib/apps/appState.js.map +1 -0
- package/lib/apps/appStateProvider.d.ts +64 -0
- package/lib/apps/appStateProvider.js +116 -0
- package/lib/apps/appStateProvider.js.map +1 -0
- package/lib/apps/index.d.ts +3 -0
- package/lib/apps/index.js +20 -0
- package/lib/apps/index.js.map +1 -0
- package/lib/apps/monitoring.d.ts +2 -0
- package/lib/apps/monitoring.js +25 -0
- package/lib/apps/monitoring.js.map +1 -0
- package/lib/apps/searchParams.d.ts +60 -0
- package/lib/apps/searchParams.js +81 -0
- package/lib/apps/searchParams.js.map +1 -0
- package/lib/hooks/index.d.ts +12 -0
- package/lib/hooks/index.js +29 -0
- package/lib/hooks/index.js.map +1 -0
- package/lib/hooks/useAsyncEffect.d.ts +7 -0
- package/lib/hooks/useAsyncEffect.js +37 -0
- package/lib/hooks/useAsyncEffect.js.map +1 -0
- package/lib/hooks/useAsyncInterval.d.ts +7 -0
- package/lib/hooks/useAsyncInterval.js +54 -0
- package/lib/hooks/useAsyncInterval.js.map +1 -0
- package/lib/hooks/useInteractiveStatus.d.ts +52 -0
- package/lib/hooks/useInteractiveStatus.js +70 -0
- package/lib/hooks/useInteractiveStatus.js.map +1 -0
- package/lib/hooks/useInterval.d.ts +5 -0
- package/lib/hooks/useInterval.js +27 -0
- package/lib/hooks/useInterval.js.map +1 -0
- package/lib/hooks/useLoadingState.d.ts +33 -0
- package/lib/hooks/useLoadingState.js +36 -0
- package/lib/hooks/useLoadingState.js.map +1 -0
- package/lib/hooks/useObjectMemo.d.ts +12 -0
- package/lib/hooks/useObjectMemo.js +20 -0
- package/lib/hooks/useObjectMemo.js.map +1 -0
- package/lib/hooks/useObjectTranslation.d.ts +15 -0
- package/lib/hooks/useObjectTranslation.js +19 -0
- package/lib/hooks/useObjectTranslation.js.map +1 -0
- package/lib/hooks/useQueue.d.ts +152 -0
- package/lib/hooks/useQueue.js +194 -0
- package/lib/hooks/useQueue.js.map +1 -0
- package/lib/hooks/useResponsiveStyle.d.ts +28 -0
- package/lib/hooks/useResponsiveStyle.js +64 -0
- package/lib/hooks/useResponsiveStyle.js.map +1 -0
- package/lib/hooks/useSearchParams.d.ts +4 -0
- package/lib/hooks/useSearchParams.js +12 -0
- package/lib/hooks/useSearchParams.js.map +1 -0
- package/lib/hooks/useSightLabel.d.ts +21 -0
- package/lib/hooks/useSightLabel.js +19 -0
- package/lib/hooks/useSightLabel.js.map +1 -0
- package/lib/hooks/useWindowDimensions.d.ts +15 -0
- package/lib/hooks/useWindowDimensions.js +27 -0
- package/lib/hooks/useWindowDimensions.js.map +1 -0
- package/lib/i18n/index.d.ts +2 -0
- package/lib/i18n/index.js +19 -0
- package/lib/i18n/index.js.map +1 -0
- package/lib/i18n/translations/image.d.ts +22 -0
- package/lib/i18n/translations/image.js +408 -0
- package/lib/i18n/translations/image.js.map +1 -0
- package/lib/i18n/translations/index.d.ts +2 -0
- package/lib/i18n/translations/index.js +19 -0
- package/lib/i18n/translations/index.js.map +1 -0
- package/lib/i18n/translations/vehicleParts.d.ts +5 -0
- package/lib/i18n/translations/vehicleParts.js +461 -0
- package/lib/i18n/translations/vehicleParts.js.map +1 -0
- package/lib/i18n/utils.d.ts +45 -0
- package/lib/i18n/utils.js +86 -0
- package/lib/i18n/utils.js.map +1 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +24 -0
- package/lib/index.js.map +1 -0
- package/lib/state/actions/createdOneImage.d.ts +44 -0
- package/lib/state/actions/createdOneImage.js +52 -0
- package/lib/state/actions/createdOneImage.js.map +1 -0
- package/lib/state/actions/gotOneInspection.d.ts +24 -0
- package/lib/state/actions/gotOneInspection.js +55 -0
- package/lib/state/actions/gotOneInspection.js.map +1 -0
- package/lib/state/actions/index.d.ts +6 -0
- package/lib/state/actions/index.js +23 -0
- package/lib/state/actions/index.js.map +1 -0
- package/lib/state/actions/monkAction.d.ts +34 -0
- package/lib/state/actions/monkAction.js +30 -0
- package/lib/state/actions/monkAction.js.map +1 -0
- package/lib/state/actions/resetState.d.ts +20 -0
- package/lib/state/actions/resetState.js +21 -0
- package/lib/state/actions/resetState.js.map +1 -0
- package/lib/state/actions/updatedManyTasks.d.ts +32 -0
- package/lib/state/actions/updatedManyTasks.js +47 -0
- package/lib/state/actions/updatedManyTasks.js.map +1 -0
- package/lib/state/actions/updatedVehicle.d.ts +38 -0
- package/lib/state/actions/updatedVehicle.js +50 -0
- package/lib/state/actions/updatedVehicle.js.map +1 -0
- package/lib/state/context.d.ts +21 -0
- package/lib/state/context.js +10 -0
- package/lib/state/context.js.map +1 -0
- package/lib/state/hooks.d.ts +8 -0
- package/lib/state/hooks.js +20 -0
- package/lib/state/hooks.js.map +1 -0
- package/lib/state/index.d.ts +6 -0
- package/lib/state/index.js +23 -0
- package/lib/state/index.js.map +1 -0
- package/lib/state/provider.d.ts +19 -0
- package/lib/state/provider.js +40 -0
- package/lib/state/provider.js.map +1 -0
- package/lib/state/reducer.d.ts +6 -0
- package/lib/state/reducer.js +24 -0
- package/lib/state/reducer.js.map +1 -0
- package/lib/state/state.d.ts +50 -0
- package/lib/state/state.js +22 -0
- package/lib/state/state.js.map +1 -0
- package/lib/theme/context.d.ts +5 -0
- package/lib/theme/context.js +10 -0
- package/lib/theme/context.js.map +1 -0
- package/lib/theme/default/index.d.ts +1 -0
- package/lib/theme/default/index.js +18 -0
- package/lib/theme/default/index.js.map +1 -0
- package/lib/theme/default/palette.d.ts +2 -0
- package/lib/theme/default/palette.js +69 -0
- package/lib/theme/default/palette.js.map +1 -0
- package/lib/theme/hooks.d.ts +5 -0
- package/lib/theme/hooks.js +13 -0
- package/lib/theme/hooks.js.map +1 -0
- package/lib/theme/index.d.ts +5 -0
- package/lib/theme/index.js +22 -0
- package/lib/theme/index.js.map +1 -0
- package/lib/theme/provider.d.ts +16 -0
- package/lib/theme/provider.js +29 -0
- package/lib/theme/provider.js.map +1 -0
- package/lib/theme/theme.d.ts +17 -0
- package/lib/theme/theme.js +54 -0
- package/lib/theme/theme.js.map +1 -0
- package/lib/utils/array.utils.d.ts +26 -0
- package/lib/utils/array.utils.js +75 -0
- package/lib/utils/array.utils.js.map +1 -0
- package/lib/utils/browser.utils.d.ts +4 -0
- package/lib/utils/browser.utils.js +16 -0
- package/lib/utils/browser.utils.js.map +1 -0
- package/lib/utils/color.utils.d.ts +51 -0
- package/lib/utils/color.utils.js +155 -0
- package/lib/utils/color.utils.js.map +1 -0
- package/lib/utils/env.utils.d.ts +5 -0
- package/lib/utils/env.utils.js +16 -0
- package/lib/utils/env.utils.js.map +1 -0
- package/lib/utils/index.d.ts +9 -0
- package/lib/utils/index.js +26 -0
- package/lib/utils/index.js.map +1 -0
- package/lib/utils/mimetype.utils.d.ts +14 -0
- package/lib/utils/mimetype.utils.js +69 -0
- package/lib/utils/mimetype.utils.js.map +1 -0
- package/lib/utils/promise.utils.d.ts +4 -0
- package/lib/utils/promise.utils.js +13 -0
- package/lib/utils/promise.utils.js.map +1 -0
- package/lib/utils/state.utils.d.ts +9 -0
- package/lib/utils/state.utils.js +35 -0
- package/lib/utils/state.utils.js.map +1 -0
- package/lib/utils/string.utils.d.ts +22 -0
- package/lib/utils/string.utils.js +48 -0
- package/lib/utils/string.utils.js.map +1 -0
- package/lib/utils/zlib.utils.d.ts +8 -0
- package/lib/utils/zlib.utils.js +30 -0
- package/lib/utils/zlib.utils.js.map +1 -0
- package/package.json +97 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useInterval = void 0;
|
|
4
|
+
var react_1 = require("react");
|
|
5
|
+
/**
|
|
6
|
+
* This custom hook creates an interval that calls the provided callback every `delay` milliseconds. If `delay` is
|
|
7
|
+
* `null` or less than 0, the callback will not be called.
|
|
8
|
+
*/
|
|
9
|
+
function useInterval(callback, delay) {
|
|
10
|
+
var callbackRef = (0, react_1.useRef)(callback);
|
|
11
|
+
(0, react_1.useLayoutEffect)(function () {
|
|
12
|
+
callbackRef.current = callback;
|
|
13
|
+
}, [callback]);
|
|
14
|
+
(0, react_1.useEffect)(function () {
|
|
15
|
+
if (delay === null || delay < 0) {
|
|
16
|
+
return function () { };
|
|
17
|
+
}
|
|
18
|
+
var intervalId = setInterval(function () {
|
|
19
|
+
callbackRef.current();
|
|
20
|
+
}, delay);
|
|
21
|
+
return function () {
|
|
22
|
+
clearInterval(intervalId);
|
|
23
|
+
};
|
|
24
|
+
}, [delay]);
|
|
25
|
+
}
|
|
26
|
+
exports.useInterval = useInterval;
|
|
27
|
+
//# sourceMappingURL=useInterval.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useInterval.js","sourceRoot":"","sources":["../../src/hooks/useInterval.ts"],"names":[],"mappings":";;;AAAA,+BAA2D;AAE3D;;;GAGG;AACH,SAAgB,WAAW,CAAC,QAAoB,EAAE,KAAoB;IACpE,IAAM,WAAW,GAAG,IAAA,cAAM,EAAC,QAAQ,CAAC,CAAC;IAErC,IAAA,uBAAe,EAAC;QACd,WAAW,CAAC,OAAO,GAAG,QAAQ,CAAC;IACjC,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,IAAA,iBAAS,EAAC;QACR,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,GAAG,CAAC,EAAE;YAC/B,OAAO,cAAO,CAAC,CAAC;SACjB;QAED,IAAM,UAAU,GAAG,WAAW,CAAC;YAC7B,WAAW,CAAC,OAAO,EAAE,CAAC;QACxB,CAAC,EAAE,KAAK,CAAC,CAAC;QAEV,OAAO;YACL,aAAa,CAAC,UAAU,CAAC,CAAC;QAC5B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;AACd,CAAC;AApBD,kCAoBC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An object containing data about a task.
|
|
3
|
+
*/
|
|
4
|
+
export interface LoadingState {
|
|
5
|
+
/**
|
|
6
|
+
* Boolean indicating if the task is running.
|
|
7
|
+
*/
|
|
8
|
+
isLoading: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* This field will contain the error that occurred during the task. If no error has occurred, or if the task has not
|
|
11
|
+
* completed yet, this field is `null`.
|
|
12
|
+
*/
|
|
13
|
+
error: unknown | null;
|
|
14
|
+
/**
|
|
15
|
+
* Callback used to indicate that the task has started.
|
|
16
|
+
*/
|
|
17
|
+
start: () => void;
|
|
18
|
+
/**
|
|
19
|
+
* Callback used to indicate that the task has completed successfully.
|
|
20
|
+
*/
|
|
21
|
+
onSuccess: () => void;
|
|
22
|
+
/**
|
|
23
|
+
* Callback used to indicate that an error occurred during the task.
|
|
24
|
+
*/
|
|
25
|
+
onError: (err?: unknown) => void;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Custom hook used to create a `LoadingState` object. This object can be used to track the processing of a task in
|
|
29
|
+
* the component. For instance, you can use this hook to handle the loading and errors of API calls in your components.
|
|
30
|
+
*
|
|
31
|
+
* @param [startsLoading] Boolean indicating if the loading state starts in loading mode or not.
|
|
32
|
+
*/
|
|
33
|
+
export declare function useLoadingState(startsLoading?: boolean): LoadingState;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useLoadingState = void 0;
|
|
4
|
+
var react_1 = require("react");
|
|
5
|
+
var useObjectMemo_1 = require("./useObjectMemo");
|
|
6
|
+
/**
|
|
7
|
+
* Custom hook used to create a `LoadingState` object. This object can be used to track the processing of a task in
|
|
8
|
+
* the component. For instance, you can use this hook to handle the loading and errors of API calls in your components.
|
|
9
|
+
*
|
|
10
|
+
* @param [startsLoading] Boolean indicating if the loading state starts in loading mode or not.
|
|
11
|
+
*/
|
|
12
|
+
function useLoadingState(startsLoading) {
|
|
13
|
+
var _a = (0, react_1.useState)(!!startsLoading), isLoading = _a[0], setIsLoading = _a[1];
|
|
14
|
+
var _b = (0, react_1.useState)(null), error = _b[0], setError = _b[1];
|
|
15
|
+
var start = (0, react_1.useCallback)(function () {
|
|
16
|
+
setError(null);
|
|
17
|
+
setIsLoading(true);
|
|
18
|
+
}, []);
|
|
19
|
+
var onSuccess = (0, react_1.useCallback)(function () {
|
|
20
|
+
setError(null);
|
|
21
|
+
setIsLoading(false);
|
|
22
|
+
}, []);
|
|
23
|
+
var onError = (0, react_1.useCallback)(function (err) {
|
|
24
|
+
setError(err);
|
|
25
|
+
setIsLoading(false);
|
|
26
|
+
}, []);
|
|
27
|
+
return (0, useObjectMemo_1.useObjectMemo)({
|
|
28
|
+
isLoading: isLoading,
|
|
29
|
+
error: error,
|
|
30
|
+
start: start,
|
|
31
|
+
onSuccess: onSuccess,
|
|
32
|
+
onError: onError,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
exports.useLoadingState = useLoadingState;
|
|
36
|
+
//# sourceMappingURL=useLoadingState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLoadingState.js","sourceRoot":"","sources":["../../src/hooks/useLoadingState.ts"],"names":[],"mappings":";;;AAAA,+BAA8C;AAC9C,iDAAgD;AA6BhD;;;;;GAKG;AACH,SAAgB,eAAe,CAAC,aAAuB;IAC/C,IAAA,KAA4B,IAAA,gBAAQ,EAAC,CAAC,CAAC,aAAa,CAAC,EAApD,SAAS,QAAA,EAAE,YAAY,QAA6B,CAAC;IACtD,IAAA,KAAoB,IAAA,gBAAQ,EAAiB,IAAI,CAAC,EAAjD,KAAK,QAAA,EAAE,QAAQ,QAAkC,CAAC;IAEzD,IAAM,KAAK,GAAG,IAAA,mBAAW,EAAC;QACxB,QAAQ,CAAC,IAAI,CAAC,CAAC;QACf,YAAY,CAAC,IAAI,CAAC,CAAC;IACrB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,IAAM,SAAS,GAAG,IAAA,mBAAW,EAAC;QAC5B,QAAQ,CAAC,IAAI,CAAC,CAAC;QACf,YAAY,CAAC,KAAK,CAAC,CAAC;IACtB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,IAAM,OAAO,GAAG,IAAA,mBAAW,EAAC,UAAC,GAAa;QACxC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACd,YAAY,CAAC,KAAK,CAAC,CAAC;IACtB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,IAAA,6BAAa,EAAC;QACnB,SAAS,WAAA;QACT,KAAK,OAAA;QACL,KAAK,OAAA;QACL,SAAS,WAAA;QACT,OAAO,SAAA;KACR,CAAC,CAAC;AACL,CAAC;AA1BD,0CA0BC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This custom hook is used to have a more handy way of memoizing a record of values.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* import { useMemo } from 'React';
|
|
6
|
+
* import { useObjectMemo } from '@monkvision/common';
|
|
7
|
+
*
|
|
8
|
+
* // These 2 lines are equivalent
|
|
9
|
+
* const foo = useMemo(() => ({ bar, baz }), [bar, baz]);
|
|
10
|
+
* const foo = useObjectMemo({ bar, baz });
|
|
11
|
+
*/
|
|
12
|
+
export declare function useObjectMemo<T extends Record<string, unknown>>(object: T): T;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useObjectMemo = void 0;
|
|
4
|
+
var react_1 = require("react");
|
|
5
|
+
/**
|
|
6
|
+
* This custom hook is used to have a more handy way of memoizing a record of values.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* import { useMemo } from 'React';
|
|
10
|
+
* import { useObjectMemo } from '@monkvision/common';
|
|
11
|
+
*
|
|
12
|
+
* // These 2 lines are equivalent
|
|
13
|
+
* const foo = useMemo(() => ({ bar, baz }), [bar, baz]);
|
|
14
|
+
* const foo = useObjectMemo({ bar, baz });
|
|
15
|
+
*/
|
|
16
|
+
function useObjectMemo(object) {
|
|
17
|
+
return (0, react_1.useMemo)(function () { return object; }, Object.values(object));
|
|
18
|
+
}
|
|
19
|
+
exports.useObjectMemo = useObjectMemo;
|
|
20
|
+
//# sourceMappingURL=useObjectMemo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useObjectMemo.js","sourceRoot":"","sources":["../../src/hooks/useObjectMemo.ts"],"names":[],"mappings":";;;AAAA,+BAAgC;AAEhC;;;;;;;;;;GAUG;AACH,SAAgB,aAAa,CAAoC,MAAS;IACxE,OAAO,IAAA,eAAO,EAAC,cAAM,OAAA,MAAM,EAAN,CAAM,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;AACtD,CAAC;AAFD,sCAEC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { TranslationObject } from '@monkvision/types';
|
|
2
|
+
/**
|
|
3
|
+
* The result of the useObjectTranslation. It contains a function which takes a LabelTranslation object and return the
|
|
4
|
+
* translated label sync with the actual selected language.
|
|
5
|
+
*/
|
|
6
|
+
export interface UseObjectTranslationResult {
|
|
7
|
+
/**
|
|
8
|
+
* Function translating a LabelTranslation object into a translated label sync with the actual selected language.
|
|
9
|
+
*/
|
|
10
|
+
tObj: (obj: TranslationObject) => string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Custom hook used to get a translation function tObj that translates TranslationObjects.
|
|
14
|
+
*/
|
|
15
|
+
export declare function useObjectTranslation(): UseObjectTranslationResult;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useObjectTranslation = void 0;
|
|
4
|
+
var react_i18next_1 = require("react-i18next");
|
|
5
|
+
var react_1 = require("react");
|
|
6
|
+
var i18n_1 = require("../i18n");
|
|
7
|
+
/**
|
|
8
|
+
* Custom hook used to get a translation function tObj that translates TranslationObjects.
|
|
9
|
+
*/
|
|
10
|
+
function useObjectTranslation() {
|
|
11
|
+
var i18n = (0, react_i18next_1.useTranslation)().i18n;
|
|
12
|
+
var tObj = (0, react_1.useCallback)(function (obj) {
|
|
13
|
+
var _a;
|
|
14
|
+
return (_a = obj[(0, i18n_1.getLanguage)(i18n.language)]) !== null && _a !== void 0 ? _a : 'translation-not-found';
|
|
15
|
+
}, [i18n.language]);
|
|
16
|
+
return { tObj: tObj };
|
|
17
|
+
}
|
|
18
|
+
exports.useObjectTranslation = useObjectTranslation;
|
|
19
|
+
//# sourceMappingURL=useObjectTranslation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useObjectTranslation.js","sourceRoot":"","sources":["../../src/hooks/useObjectTranslation.ts"],"names":[],"mappings":";;;AACA,+CAA+C;AAC/C,+BAAoC;AACpC,gCAAsC;AAatC;;GAEG;AACH,SAAgB,oBAAoB;IAC1B,IAAA,IAAI,GAAK,IAAA,8BAAc,GAAE,KAArB,CAAsB;IAClC,IAAM,IAAI,GAAG,IAAA,mBAAW,EACtB,UAAC,GAAsB;;QACrB,OAAO,MAAA,GAAG,CAAC,IAAA,kBAAW,EAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,mCAAI,uBAAuB,CAAC;IACpE,CAAC,EACD,CAAC,IAAI,CAAC,QAAQ,CAAC,CAChB,CAAC;IACF,OAAO,EAAE,IAAI,MAAA,EAAE,CAAC;AAClB,CAAC;AATD,oDASC"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definition for the processing function of a queue.
|
|
3
|
+
*/
|
|
4
|
+
export type ProcessingFunction<T> = (item: T) => Promise<unknown>;
|
|
5
|
+
/**
|
|
6
|
+
* The options that can be used to configure a processing queue using the `useQueue` hook.
|
|
7
|
+
*
|
|
8
|
+
* @see useQueue
|
|
9
|
+
* @see Queue
|
|
10
|
+
*/
|
|
11
|
+
export interface QueueOptions<T> {
|
|
12
|
+
/**
|
|
13
|
+
* If this value is set, the queue will have a maximum number of items. Trying to push an item in the queue while the
|
|
14
|
+
* queue is full will result in an error being thrown. You can know if the queue is full using the `queue.isFull`
|
|
15
|
+
* property.
|
|
16
|
+
*
|
|
17
|
+
* @see Queue.isFull
|
|
18
|
+
*/
|
|
19
|
+
maxItems?: number;
|
|
20
|
+
/**
|
|
21
|
+
* If this value is set, the queue will have a maximum number of items being processed at the same time. Trying to
|
|
22
|
+
* push an item in the queue while the queue is at max processing will result in the item being put on hold until more
|
|
23
|
+
* space is available in the processing queue. You can know if the queue is at max processing using the
|
|
24
|
+
* `queue.isAtMaxProcessing` property.
|
|
25
|
+
*
|
|
26
|
+
* @see Queue.isAtMaxProcessing
|
|
27
|
+
*/
|
|
28
|
+
maxProcessingItems?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Boolean indicating if the queue should store the items for which the processing failed (i.e. for which the Promise
|
|
31
|
+
* resulting from the processing function for this item rejected). Items will be stored in the `queue.failedItems`
|
|
32
|
+
* array, and this array can be cleared using the `queue.clearFailedItems` function.
|
|
33
|
+
*
|
|
34
|
+
* @default false
|
|
35
|
+
* @see Queue.failedItems
|
|
36
|
+
* @see Queue.clearFailedItems
|
|
37
|
+
*/
|
|
38
|
+
storeFailedItems?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Callback that will be called when the processing of an item successfully completes.
|
|
41
|
+
*/
|
|
42
|
+
onItemComplete?: (item: T) => void;
|
|
43
|
+
/**
|
|
44
|
+
* Callback that will be called when the processing of an item fails.
|
|
45
|
+
*/
|
|
46
|
+
onItemFail?: (item: T) => void;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Object containing details on an item in the processing queue for which the processing failed (the Promise resulting
|
|
50
|
+
* from the process function rejected).
|
|
51
|
+
*/
|
|
52
|
+
export interface QueueFailedItem<T> {
|
|
53
|
+
/**
|
|
54
|
+
* This item for which the processing failed.
|
|
55
|
+
*/
|
|
56
|
+
item: T;
|
|
57
|
+
/**
|
|
58
|
+
* This value contains the rejection value of the processing function that caused the item to fail.
|
|
59
|
+
*/
|
|
60
|
+
error?: unknown;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* This object is a processing queue created by the `useQueue` hook. This queue works as an aynchronous FIFO (First In,
|
|
64
|
+
* First Out) processing queue, where items are processed in parallel. You can configure this queue using the options
|
|
65
|
+
* (`QueueOptions`) passed as the second parameter of the `useQueue` hook.
|
|
66
|
+
*
|
|
67
|
+
* @see QueueOptions
|
|
68
|
+
*/
|
|
69
|
+
export interface Queue<T> {
|
|
70
|
+
/**
|
|
71
|
+
* The length of the queue. This number represents the total number of items currently in the queue (being processed
|
|
72
|
+
* or being on hold).
|
|
73
|
+
*/
|
|
74
|
+
length: number;
|
|
75
|
+
/**
|
|
76
|
+
* This number represents the total number of items currently being processed in the queue.
|
|
77
|
+
*/
|
|
78
|
+
processingCount: number;
|
|
79
|
+
/**
|
|
80
|
+
* If `options.maxProcessingItems` is set, when pushing a new item in the queue while the queue is at max processing,
|
|
81
|
+
* the item will be put on hold until the total number of items being processed goes below the maximum. This number
|
|
82
|
+
* indicates the total number of items currently on hold in the queue.
|
|
83
|
+
*
|
|
84
|
+
* @see QueueOptions.maxProcessingItems
|
|
85
|
+
* @see Queue.isAtMaxProcessing
|
|
86
|
+
*/
|
|
87
|
+
onHoldCount: number;
|
|
88
|
+
/**
|
|
89
|
+
* This number indicates the total number of items that were pushed to the queue since the queue creation or since the
|
|
90
|
+
* last queue clear.
|
|
91
|
+
*/
|
|
92
|
+
totalItems: number;
|
|
93
|
+
/**
|
|
94
|
+
* If `options.maxItems` is set, this boolean will be set to `true` if the length of the queue is at its maximum.
|
|
95
|
+
*
|
|
96
|
+
* @see QueueOptions.maxItems
|
|
97
|
+
*/
|
|
98
|
+
isFull: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* If `options.maxProcessingItems` is set, this boolean will be set to `true` if the total number of items being
|
|
101
|
+
* processed by the queue is at its maximum.
|
|
102
|
+
*
|
|
103
|
+
* @see QueueOptions.maxProcessingItems
|
|
104
|
+
*/
|
|
105
|
+
isAtMaxProcessing: boolean;
|
|
106
|
+
/**
|
|
107
|
+
* This method is used to push an item in the queue.
|
|
108
|
+
*
|
|
109
|
+
* *Notes :*
|
|
110
|
+
* - If `options.maxItems` is set and the queue is full, this method will throw an error.
|
|
111
|
+
* - If `options.maxProcessingItems` is set and the queue is at max processing, this method will push the item and put
|
|
112
|
+
* it on hold until more space in the processing queue is available.
|
|
113
|
+
*/
|
|
114
|
+
push: (item: T) => void;
|
|
115
|
+
/**
|
|
116
|
+
* If `options.storeFailedItems` is set to `true`, this array contains every item for which the processing promise
|
|
117
|
+
* rejected, along with their respective rejection values. This array can be cleared using the
|
|
118
|
+
* `queue.clearFailedItems` function.
|
|
119
|
+
*
|
|
120
|
+
* @see QueueOptions.storeFailedItems
|
|
121
|
+
* @see Queue.clearFailedItems
|
|
122
|
+
*/
|
|
123
|
+
failedItems: QueueFailedItem<T>[];
|
|
124
|
+
/**
|
|
125
|
+
* This method is used to remove some items from the `failedItems` array.
|
|
126
|
+
*
|
|
127
|
+
* @see QueueOptions.storeFailedItems
|
|
128
|
+
* @see Queue.failedItems
|
|
129
|
+
*/
|
|
130
|
+
clearFailedItems: (items: T[]) => void;
|
|
131
|
+
/**
|
|
132
|
+
* This method is used to completely clear the queue and reset its state to the initial one. The boolean passed as the
|
|
133
|
+
* parameter of this function indicates wether or not the ongoing processing should also be canceled or not :
|
|
134
|
+
*
|
|
135
|
+
* - If set to `false` (the default value), when items that were still being processed when the queue was cleared are
|
|
136
|
+
* finished being processed, they wil still be treated by the end routines (`options.onItemComplete` and
|
|
137
|
+
* `options.onItemFail` will be called, and failed items will be added to the `failedItems` array if
|
|
138
|
+
* `options.storeFailedItems` is used).
|
|
139
|
+
* - If set to `true`, even items that will be finished processing *after* the queue was cleared will not be
|
|
140
|
+
* treated by the end routines (`options.onItemComplete` and `options.onItemFail` will **not** be called etc.).
|
|
141
|
+
*/
|
|
142
|
+
clear: (cancelProcessing?: boolean) => void;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* This hook is used to create a processing queue. The `process` function passed as a parameter is an async function
|
|
146
|
+
* that is used to process items in the queue. You can find more details on how the queue works by taking a look at the
|
|
147
|
+
* TSDoc of the `Queue` interface.
|
|
148
|
+
*
|
|
149
|
+
* @see Queue
|
|
150
|
+
* @see QueueOptions
|
|
151
|
+
*/
|
|
152
|
+
export declare function useQueue<T>(process: ProcessingFunction<T>, optionsParams?: QueueOptions<T>): Queue<T>;
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
50
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
51
|
+
if (ar || !(i in from)) {
|
|
52
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
53
|
+
ar[i] = from[i];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
57
|
+
};
|
|
58
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
59
|
+
exports.useQueue = void 0;
|
|
60
|
+
var react_1 = require("react");
|
|
61
|
+
var defaultOptions = {
|
|
62
|
+
storeFailedItems: false,
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* This hook is used to create a processing queue. The `process` function passed as a parameter is an async function
|
|
66
|
+
* that is used to process items in the queue. You can find more details on how the queue works by taking a look at the
|
|
67
|
+
* TSDoc of the `Queue` interface.
|
|
68
|
+
*
|
|
69
|
+
* @see Queue
|
|
70
|
+
* @see QueueOptions
|
|
71
|
+
*/
|
|
72
|
+
function useQueue(process, optionsParams) {
|
|
73
|
+
var _this = this;
|
|
74
|
+
var _a = (0, react_1.useState)([]), processedItems = _a[0], setProcessedItems = _a[1];
|
|
75
|
+
var _b = (0, react_1.useState)([]), itemsOnHold = _b[0], setItemsOnHold = _b[1];
|
|
76
|
+
var _c = (0, react_1.useState)([]), canceledItems = _c[0], setCanceledItems = _c[1];
|
|
77
|
+
var _d = (0, react_1.useState)([]), failedItems = _d[0], setFailedItems = _d[1];
|
|
78
|
+
var _e = (0, react_1.useState)(0), totalItems = _e[0], setTotalItems = _e[1];
|
|
79
|
+
var options = (0, react_1.useMemo)(function () { return (__assign(__assign({}, defaultOptions), (optionsParams !== null && optionsParams !== void 0 ? optionsParams : {}))); }, [optionsParams]);
|
|
80
|
+
var stateRef = (0, react_1.useRef)();
|
|
81
|
+
stateRef.current = { processedItems: processedItems, itemsOnHold: itemsOnHold, canceledItems: canceledItems, options: options };
|
|
82
|
+
var clear = (0, react_1.useCallback)(function (cancelProcessing) {
|
|
83
|
+
if (cancelProcessing === void 0) { cancelProcessing = false; }
|
|
84
|
+
if (cancelProcessing) {
|
|
85
|
+
setCanceledItems(function (items) { var _a, _b; return __spreadArray(__spreadArray([], items, true), ((_b = (_a = stateRef.current) === null || _a === void 0 ? void 0 : _a.processedItems) !== null && _b !== void 0 ? _b : []), true); });
|
|
86
|
+
}
|
|
87
|
+
setProcessedItems([]);
|
|
88
|
+
setItemsOnHold([]);
|
|
89
|
+
setFailedItems([]);
|
|
90
|
+
setTotalItems(0);
|
|
91
|
+
}, []);
|
|
92
|
+
var clearFailedItems = (0, react_1.useCallback)(function (itemsToClear) {
|
|
93
|
+
setFailedItems(function (items) { return items.filter(function (i) { return !itemsToClear.includes(i.item); }); });
|
|
94
|
+
}, []);
|
|
95
|
+
var processItem = (0, react_1.useCallback)(function (item) { return __awaiter(_this, void 0, void 0, function () {
|
|
96
|
+
var error_1;
|
|
97
|
+
var _a, _b, _c, _d, _e;
|
|
98
|
+
return __generator(this, function (_f) {
|
|
99
|
+
switch (_f.label) {
|
|
100
|
+
case 0:
|
|
101
|
+
setProcessedItems(function (items) { return __spreadArray(__spreadArray([], items, true), [item], false); });
|
|
102
|
+
_f.label = 1;
|
|
103
|
+
case 1:
|
|
104
|
+
_f.trys.push([1, 3, 4, 5]);
|
|
105
|
+
return [4 /*yield*/, process(item)];
|
|
106
|
+
case 2:
|
|
107
|
+
_f.sent();
|
|
108
|
+
if ((_a = stateRef.current) === null || _a === void 0 ? void 0 : _a.canceledItems.includes(item)) {
|
|
109
|
+
setCanceledItems(function (items) { return items.filter(function (i) { return i !== item; }); });
|
|
110
|
+
return [2 /*return*/];
|
|
111
|
+
}
|
|
112
|
+
if ((_b = stateRef.current) === null || _b === void 0 ? void 0 : _b.options.onItemComplete) {
|
|
113
|
+
stateRef.current.options.onItemComplete(item);
|
|
114
|
+
}
|
|
115
|
+
return [3 /*break*/, 5];
|
|
116
|
+
case 3:
|
|
117
|
+
error_1 = _f.sent();
|
|
118
|
+
if ((_c = stateRef.current) === null || _c === void 0 ? void 0 : _c.canceledItems.includes(item)) {
|
|
119
|
+
setCanceledItems(function (items) { return items.filter(function (i) { return i !== item; }); });
|
|
120
|
+
return [2 /*return*/];
|
|
121
|
+
}
|
|
122
|
+
if ((_d = stateRef.current) === null || _d === void 0 ? void 0 : _d.options.storeFailedItems) {
|
|
123
|
+
setFailedItems(function (items) { return __spreadArray(__spreadArray([], items, true), [{ error: error_1, item: item }], false); });
|
|
124
|
+
}
|
|
125
|
+
if ((_e = stateRef.current) === null || _e === void 0 ? void 0 : _e.options.onItemFail) {
|
|
126
|
+
stateRef.current.options.onItemFail(item);
|
|
127
|
+
}
|
|
128
|
+
return [3 /*break*/, 5];
|
|
129
|
+
case 4:
|
|
130
|
+
setProcessedItems(function (items) { return items.filter(function (i) { return i !== item; }); });
|
|
131
|
+
return [7 /*endfinally*/];
|
|
132
|
+
case 5: return [2 /*return*/];
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}); }, [process]);
|
|
136
|
+
var shiftQueue = (0, react_1.useCallback)(function () {
|
|
137
|
+
if (stateRef.current) {
|
|
138
|
+
if (stateRef.current.itemsOnHold.length === 0) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
var numberOfItemsToShift = stateRef.current.options.maxProcessingItems === undefined
|
|
142
|
+
? stateRef.current.itemsOnHold.length
|
|
143
|
+
: Math.min(stateRef.current.options.maxProcessingItems - stateRef.current.processedItems.length, stateRef.current.itemsOnHold.length);
|
|
144
|
+
if (numberOfItemsToShift === 0) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
var itemsToShift_1 = stateRef.current.itemsOnHold.slice(0, numberOfItemsToShift);
|
|
148
|
+
itemsToShift_1.forEach(function (item) {
|
|
149
|
+
processItem(item)
|
|
150
|
+
.catch(function () { })
|
|
151
|
+
.finally(function () { return shiftQueue(); });
|
|
152
|
+
});
|
|
153
|
+
setItemsOnHold(function (items) { return items.filter(function (item) { return !itemsToShift_1.includes(item); }); });
|
|
154
|
+
}
|
|
155
|
+
}, [processItem]);
|
|
156
|
+
var push = (0, react_1.useCallback)(function (item) {
|
|
157
|
+
if (options.maxItems !== undefined &&
|
|
158
|
+
processedItems.length + itemsOnHold.length >= options.maxItems) {
|
|
159
|
+
throw new Error('Queue is full.');
|
|
160
|
+
}
|
|
161
|
+
setTotalItems(function (total) { return total + 1; });
|
|
162
|
+
if (options.maxProcessingItems !== undefined &&
|
|
163
|
+
processedItems.length >= options.maxProcessingItems) {
|
|
164
|
+
setItemsOnHold(function (items) { return __spreadArray(__spreadArray([], items, true), [item], false); });
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
processItem(item)
|
|
168
|
+
.catch(function () { })
|
|
169
|
+
.finally(function () { return shiftQueue(); });
|
|
170
|
+
}, [
|
|
171
|
+
options.maxItems,
|
|
172
|
+
processedItems.length,
|
|
173
|
+
itemsOnHold.length,
|
|
174
|
+
options.maxProcessingItems,
|
|
175
|
+
processItem,
|
|
176
|
+
shiftQueue,
|
|
177
|
+
]);
|
|
178
|
+
return {
|
|
179
|
+
length: processedItems.length + itemsOnHold.length,
|
|
180
|
+
processingCount: processedItems.length,
|
|
181
|
+
onHoldCount: itemsOnHold.length,
|
|
182
|
+
totalItems: totalItems,
|
|
183
|
+
isFull: options.maxItems !== undefined &&
|
|
184
|
+
processedItems.length + itemsOnHold.length >= options.maxItems,
|
|
185
|
+
isAtMaxProcessing: options.maxProcessingItems !== undefined &&
|
|
186
|
+
processedItems.length >= options.maxProcessingItems,
|
|
187
|
+
push: push,
|
|
188
|
+
failedItems: failedItems,
|
|
189
|
+
clearFailedItems: clearFailedItems,
|
|
190
|
+
clear: clear,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
exports.useQueue = useQueue;
|
|
194
|
+
//# sourceMappingURL=useQueue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useQueue.js","sourceRoot":"","sources":["../../src/hooks/useQueue.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+BAA+D;AA4J/D,IAAM,cAAc,GAA0B;IAC5C,gBAAgB,EAAE,KAAK;CACxB,CAAC;AAEF;;;;;;;GAOG;AACH,SAAgB,QAAQ,CACtB,OAA8B,EAC9B,aAA+B;IAFjC,iBAgIC;IA5HO,IAAA,KAAsC,IAAA,gBAAQ,EAAM,EAAE,CAAC,EAAtD,cAAc,QAAA,EAAE,iBAAiB,QAAqB,CAAC;IACxD,IAAA,KAAgC,IAAA,gBAAQ,EAAM,EAAE,CAAC,EAAhD,WAAW,QAAA,EAAE,cAAc,QAAqB,CAAC;IAClD,IAAA,KAAoC,IAAA,gBAAQ,EAAM,EAAE,CAAC,EAApD,aAAa,QAAA,EAAE,gBAAgB,QAAqB,CAAC;IACtD,IAAA,KAAgC,IAAA,gBAAQ,EAAuB,EAAE,CAAC,EAAjE,WAAW,QAAA,EAAE,cAAc,QAAsC,CAAC;IACnE,IAAA,KAA8B,IAAA,gBAAQ,EAAC,CAAC,CAAC,EAAx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QACJ,WAAW,aAAA;QACX,gBAAgB,kBAAA;QAChB,KAAK,OAAA;KACN,CAAC;AACJ,CAAC;AAhID,4BAgIC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { CSSProperties } from 'react';
|
|
2
|
+
import { ResponsiveStyleProperties } from '@monkvision/types';
|
|
3
|
+
/**
|
|
4
|
+
* Custom hook used to render the given style only when its media query confitions are met.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* import { useResponsiveStyle } from '@monkvision/common';
|
|
8
|
+
* import { Styles } from '@monkvision/types';
|
|
9
|
+
*
|
|
10
|
+
* const styles: Styles = {
|
|
11
|
+
* div: {
|
|
12
|
+
* width: 100,
|
|
13
|
+
* height: 100,
|
|
14
|
+
* },
|
|
15
|
+
* divMobile: {
|
|
16
|
+
* __media: { maxWidth: 500 },
|
|
17
|
+
* backgroundColor: '#ff0000',
|
|
18
|
+
* },
|
|
19
|
+
* };
|
|
20
|
+
*
|
|
21
|
+
* function TestComponent() {
|
|
22
|
+
* const { responsive } = useResponsiveStyle();
|
|
23
|
+
* return <div style={{...styles.div, ...responsive(styles.divMobile)}}>Hello</div>
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
export declare function useResponsiveStyle(): {
|
|
27
|
+
responsive: (style: ResponsiveStyleProperties | null) => CSSProperties | null;
|
|
28
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useResponsiveStyle = void 0;
|
|
4
|
+
var react_1 = require("react");
|
|
5
|
+
var useWindowDimensions_1 = require("./useWindowDimensions");
|
|
6
|
+
function areQueryConditionsMet(query, dimensions) {
|
|
7
|
+
if (!query) {
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
if (query.maxWidth !== undefined && dimensions.width > query.maxWidth) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
if (query.minWidth !== undefined && dimensions.width < query.minWidth) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (query.maxHeight !== undefined && dimensions.height > query.maxHeight) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
if (query.minHeight !== undefined && dimensions.height < query.minHeight) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
if (query.portrait !== undefined && dimensions.isPortrait !== query.portrait) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
if (query.landscape !== undefined && dimensions.isPortrait !== !query.landscape) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Custom hook used to render the given style only when its media query confitions are met.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* import { useResponsiveStyle } from '@monkvision/common';
|
|
35
|
+
* import { Styles } from '@monkvision/types';
|
|
36
|
+
*
|
|
37
|
+
* const styles: Styles = {
|
|
38
|
+
* div: {
|
|
39
|
+
* width: 100,
|
|
40
|
+
* height: 100,
|
|
41
|
+
* },
|
|
42
|
+
* divMobile: {
|
|
43
|
+
* __media: { maxWidth: 500 },
|
|
44
|
+
* backgroundColor: '#ff0000',
|
|
45
|
+
* },
|
|
46
|
+
* };
|
|
47
|
+
*
|
|
48
|
+
* function TestComponent() {
|
|
49
|
+
* const { responsive } = useResponsiveStyle();
|
|
50
|
+
* return <div style={{...styles.div, ...responsive(styles.divMobile)}}>Hello</div>
|
|
51
|
+
* }
|
|
52
|
+
*/
|
|
53
|
+
function useResponsiveStyle() {
|
|
54
|
+
var dimensions = (0, useWindowDimensions_1.useWindowDimensions)();
|
|
55
|
+
var responsive = (0, react_1.useCallback)(function (style) {
|
|
56
|
+
if (areQueryConditionsMet(style === null || style === void 0 ? void 0 : style.__media, dimensions)) {
|
|
57
|
+
return style;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}, [dimensions]);
|
|
61
|
+
return { responsive: responsive };
|
|
62
|
+
}
|
|
63
|
+
exports.useResponsiveStyle = useResponsiveStyle;
|
|
64
|
+
//# sourceMappingURL=useResponsiveStyle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useResponsiveStyle.js","sourceRoot":"","sources":["../../src/hooks/useResponsiveStyle.ts"],"names":[],"mappings":";;;AAAA,+BAAmD;AAEnD,6DAA8E;AAE9E,SAAS,qBAAqB,CAC5B,KAA2C,EAC3C,UAA4B;IAE5B,IAAI,CAAC,KAAK,EAAE;QACV,OAAO,IAAI,CAAC;KACb;IACD,IAAI,KAAK,CAAC,QAAQ,KAAK,SAAS,IAAI,UAAU,CAAC,KAAK,GAAG,KAAK,CAAC,QAAQ,EAAE;QACrE,OAAO,KAAK,CAAC;KACd;IACD,IAAI,KAAK,CAAC,QAAQ,KAAK,SAAS,IAAI,UAAU,CAAC,KAAK,GAAG,KAAK,CAAC,QAAQ,EAAE;QACrE,OAAO,KAAK,CAAC;KACd;IACD,IAAI,KAAK,CAAC,SAAS,KAAK,SAAS,IAAI,UAAU,CAAC,MAAM,GAAG,KAAK,CAAC,SAAS,EAAE;QACxE,OAAO,KAAK,CAAC;KACd;IACD,IAAI,KAAK,CAAC,SAAS,KAAK,SAAS,IAAI,UAAU,CAAC,MAAM,GAAG,KAAK,CAAC,SAAS,EAAE;QACxE,OAAO,KAAK,CAAC;KACd;IACD,IAAI,KAAK,CAAC,QAAQ,KAAK,SAAS,IAAI,UAAU,CAAC,UAAU,KAAK,KAAK,CAAC,QAAQ,EAAE;QAC5E,OAAO,KAAK,CAAC;KACd;IACD,IAAI,KAAK,CAAC,SAAS,KAAK,SAAS,IAAI,UAAU,CAAC,UAAU,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE;QAC/E,OAAO,KAAK,CAAC;KACd;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,SAAgB,kBAAkB;IAGhC,IAAM,UAAU,GAAG,IAAA,yCAAmB,GAAE,CAAC;IACzC,IAAM,UAAU,GAAG,IAAA,mBAAW,EAC5B,UAAC,KAAuC;QACtC,IAAI,qBAAqB,CAAC,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,OAAO,EAAE,UAAU,CAAC,EAAE;YACrD,OAAO,KAAK,CAAC;SACd;QACD,OAAO,IAAI,CAAC;IACd,CAAC,EACD,CAAC,UAAU,CAAC,CACb,CAAC;IAEF,OAAO,EAAE,UAAU,YAAA,EAAE,CAAC;AACxB,CAAC;AAfD,gDAeC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useSearchParams = void 0;
|
|
4
|
+
var react_1 = require("react");
|
|
5
|
+
/**
|
|
6
|
+
* Custom hook used to fetch search params from the current window URL.
|
|
7
|
+
*/
|
|
8
|
+
function useSearchParams() {
|
|
9
|
+
return (0, react_1.useMemo)(function () { return new URL(window.location.toString()).searchParams; }, []);
|
|
10
|
+
}
|
|
11
|
+
exports.useSearchParams = useSearchParams;
|
|
12
|
+
//# sourceMappingURL=useSearchParams.js.map
|