@trackunit/react-core-hooks 1.8.0 → 1.9.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/index.cjs.js CHANGED
@@ -379,21 +379,6 @@ const useModalDialogContext = () => {
379
379
  return modalDialogContext;
380
380
  };
381
381
 
382
- /**
383
- * This is a hook to use the NavigationContext.
384
- *
385
- * @requires NavigationContext
386
- * @example
387
- * import { useNavigateInHost } from "@trackunit/react-core-hooks";
388
- * @see (@link NavigationRuntimeApi)
389
- */
390
- const useNavigateInHost = () => {
391
- const context = react.useContext(reactCoreContextsApi.NavigationContext);
392
- if (!context) {
393
- throw new Error("useNavigateInHost must be used within an NavigationContext");
394
- }
395
- return context;
396
- };
397
382
  /**
398
383
  * This is a hook to use the hasAccessTo in host.
399
384
  * You can use this to ensure the page you link to is available for the user to see.
@@ -415,16 +400,40 @@ const useHasAccessTo = (options) => {
415
400
  throw new Error("useHasAccessTo must be used within an NavigationContext");
416
401
  }
417
402
  const [hasAccess, setHasAccess] = react.useState();
403
+ const [loading, setLoading] = react.useState(true);
404
+ const [error, setError] = react.useState();
418
405
  react.useEffect(() => {
419
406
  async function checkAccess() {
420
- if (options.assetId) {
407
+ try {
421
408
  const doHaveAccess = await context?.hasAccessTo(options);
422
- setHasAccess(() => doHaveAccess);
409
+ setHasAccess(doHaveAccess);
410
+ }
411
+ catch (e) {
412
+ setError(new Error("Failed to check access", { cause: e }));
413
+ }
414
+ finally {
415
+ setLoading(false);
423
416
  }
424
417
  }
425
- checkAccess();
418
+ void checkAccess();
426
419
  }, [context, options]);
427
- return { hasAccess };
420
+ return react.useMemo(() => ({ hasAccess, loading, error }), [hasAccess, loading, error]);
421
+ };
422
+
423
+ /**
424
+ * This is a hook to use the NavigationContext.
425
+ *
426
+ * @requires NavigationContext
427
+ * @example
428
+ * import { useNavigateInHost } from "@trackunit/react-core-hooks";
429
+ * @see (@link NavigationRuntimeApi)
430
+ */
431
+ const useNavigateInHost = () => {
432
+ const context = react.useContext(reactCoreContextsApi.NavigationContext);
433
+ if (!context) {
434
+ throw new Error("useNavigateInHost must be used within an NavigationContext");
435
+ }
436
+ return context;
428
437
  };
429
438
 
430
439
  /**
@@ -743,6 +752,28 @@ const useToast = () => {
743
752
  return toastContext;
744
753
  };
745
754
 
755
+ /**
756
+ * This is a hook providing the CurrentUserContext.
757
+ *
758
+ * @requires CurrentUserProvider
759
+ * @example
760
+ * import { useCurrentUser } from "@trackunit/react-core-hooks";
761
+ * const { assumedUser, accountId } = useCurrentUser();
762
+ *
763
+ * //use it for something
764
+ * const { data, loading } = useGetAccountByIdQuery({
765
+ * variables: { accountId: assumedUser?.accountId || accountId || "" },
766
+ * });
767
+ * @see { CurrentUserContext}
768
+ */
769
+ const useCurrentUser = () => {
770
+ const context = react.useContext(reactCoreContextsApi.CurrentUserContext);
771
+ if (!context) {
772
+ throw new Error("useCurrentUser must be used within the CurrentUserProvider");
773
+ }
774
+ return context;
775
+ };
776
+
746
777
  /**
747
778
  * This is a hook providing the Current User language.
748
779
  *
@@ -829,28 +860,6 @@ const setGlobalLanguage = (language) => {
829
860
  }
830
861
  };
831
862
 
832
- /**
833
- * This is a hook providing the CurrentUserContext.
834
- *
835
- * @requires CurrentUserProvider
836
- * @example
837
- * import { useCurrentUser } from "@trackunit/react-core-hooks";
838
- * const { assumedUser, accountId } = useCurrentUser();
839
- *
840
- * //use it for something
841
- * const { data, loading } = useGetAccountByIdQuery({
842
- * variables: { accountId: assumedUser?.accountId || accountId || "" },
843
- * });
844
- * @see { CurrentUserContext}
845
- */
846
- const useCurrentUser = () => {
847
- const context = react.useContext(reactCoreContextsApi.CurrentUserContext);
848
- if (!context) {
849
- throw new Error("useCurrentUser must be used within the CurrentUserProvider");
850
- }
851
- return context;
852
- };
853
-
854
863
  /**
855
864
  * This is a hook to use the WidgetConfigProvider.
856
865
  *
package/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import { AnalyticsContext, AssetSortingContext, ConfirmationDialogContext, EnvironmentContext, ErrorHandlingContext, ExportDataContext, FeatureFlagContext, FilterBarContext, TokenContext, ModalDialogContext, NavigationContext, OemBrandingContext, UserSubscriptionContext, TimeRangeContext, ToastContext, CurrentUserPreferenceContext, CurrentUserContext, WidgetConfigContext } from '@trackunit/react-core-contexts-api';
1
+ import { AnalyticsContext, AssetSortingContext, ConfirmationDialogContext, EnvironmentContext, ErrorHandlingContext, ExportDataContext, FeatureFlagContext, FilterBarContext, TokenContext, ModalDialogContext, NavigationContext, OemBrandingContext, UserSubscriptionContext, TimeRangeContext, ToastContext, CurrentUserContext, CurrentUserPreferenceContext, WidgetConfigContext } from '@trackunit/react-core-contexts-api';
2
2
  import { useContext, useMemo, useState, useCallback, useEffect, useRef } from 'react';
3
3
  import { AssetRuntime, CustomerRuntime, EventRuntime, ParamsRuntime, SiteRuntime, WidgetConfigRuntime } from '@trackunit/iris-app-runtime-core';
4
4
 
@@ -377,21 +377,6 @@ const useModalDialogContext = () => {
377
377
  return modalDialogContext;
378
378
  };
379
379
 
380
- /**
381
- * This is a hook to use the NavigationContext.
382
- *
383
- * @requires NavigationContext
384
- * @example
385
- * import { useNavigateInHost } from "@trackunit/react-core-hooks";
386
- * @see (@link NavigationRuntimeApi)
387
- */
388
- const useNavigateInHost = () => {
389
- const context = useContext(NavigationContext);
390
- if (!context) {
391
- throw new Error("useNavigateInHost must be used within an NavigationContext");
392
- }
393
- return context;
394
- };
395
380
  /**
396
381
  * This is a hook to use the hasAccessTo in host.
397
382
  * You can use this to ensure the page you link to is available for the user to see.
@@ -413,16 +398,40 @@ const useHasAccessTo = (options) => {
413
398
  throw new Error("useHasAccessTo must be used within an NavigationContext");
414
399
  }
415
400
  const [hasAccess, setHasAccess] = useState();
401
+ const [loading, setLoading] = useState(true);
402
+ const [error, setError] = useState();
416
403
  useEffect(() => {
417
404
  async function checkAccess() {
418
- if (options.assetId) {
405
+ try {
419
406
  const doHaveAccess = await context?.hasAccessTo(options);
420
- setHasAccess(() => doHaveAccess);
407
+ setHasAccess(doHaveAccess);
408
+ }
409
+ catch (e) {
410
+ setError(new Error("Failed to check access", { cause: e }));
411
+ }
412
+ finally {
413
+ setLoading(false);
421
414
  }
422
415
  }
423
- checkAccess();
416
+ void checkAccess();
424
417
  }, [context, options]);
425
- return { hasAccess };
418
+ return useMemo(() => ({ hasAccess, loading, error }), [hasAccess, loading, error]);
419
+ };
420
+
421
+ /**
422
+ * This is a hook to use the NavigationContext.
423
+ *
424
+ * @requires NavigationContext
425
+ * @example
426
+ * import { useNavigateInHost } from "@trackunit/react-core-hooks";
427
+ * @see (@link NavigationRuntimeApi)
428
+ */
429
+ const useNavigateInHost = () => {
430
+ const context = useContext(NavigationContext);
431
+ if (!context) {
432
+ throw new Error("useNavigateInHost must be used within an NavigationContext");
433
+ }
434
+ return context;
426
435
  };
427
436
 
428
437
  /**
@@ -741,6 +750,28 @@ const useToast = () => {
741
750
  return toastContext;
742
751
  };
743
752
 
753
+ /**
754
+ * This is a hook providing the CurrentUserContext.
755
+ *
756
+ * @requires CurrentUserProvider
757
+ * @example
758
+ * import { useCurrentUser } from "@trackunit/react-core-hooks";
759
+ * const { assumedUser, accountId } = useCurrentUser();
760
+ *
761
+ * //use it for something
762
+ * const { data, loading } = useGetAccountByIdQuery({
763
+ * variables: { accountId: assumedUser?.accountId || accountId || "" },
764
+ * });
765
+ * @see { CurrentUserContext}
766
+ */
767
+ const useCurrentUser = () => {
768
+ const context = useContext(CurrentUserContext);
769
+ if (!context) {
770
+ throw new Error("useCurrentUser must be used within the CurrentUserProvider");
771
+ }
772
+ return context;
773
+ };
774
+
744
775
  /**
745
776
  * This is a hook providing the Current User language.
746
777
  *
@@ -827,28 +858,6 @@ const setGlobalLanguage = (language) => {
827
858
  }
828
859
  };
829
860
 
830
- /**
831
- * This is a hook providing the CurrentUserContext.
832
- *
833
- * @requires CurrentUserProvider
834
- * @example
835
- * import { useCurrentUser } from "@trackunit/react-core-hooks";
836
- * const { assumedUser, accountId } = useCurrentUser();
837
- *
838
- * //use it for something
839
- * const { data, loading } = useGetAccountByIdQuery({
840
- * variables: { accountId: assumedUser?.accountId || accountId || "" },
841
- * });
842
- * @see { CurrentUserContext}
843
- */
844
- const useCurrentUser = () => {
845
- const context = useContext(CurrentUserContext);
846
- if (!context) {
847
- throw new Error("useCurrentUser must be used within the CurrentUserProvider");
848
- }
849
- return context;
850
- };
851
-
852
861
  /**
853
862
  * This is a hook to use the WidgetConfigProvider.
854
863
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-hooks",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -8,9 +8,9 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "react": "19.0.0",
11
- "@trackunit/iris-app-runtime-core": "1.9.0",
12
- "@trackunit/iris-app-runtime-core-api": "1.8.0",
13
- "@trackunit/react-core-contexts-api": "1.9.0"
11
+ "@trackunit/iris-app-runtime-core": "1.10.0",
12
+ "@trackunit/iris-app-runtime-core-api": "1.9.0",
13
+ "@trackunit/react-core-contexts-api": "1.10.0"
14
14
  },
15
15
  "module": "./index.esm.js",
16
16
  "main": "./index.cjs.js",
package/src/index.d.ts CHANGED
@@ -10,6 +10,7 @@ export * from "./filterBar/useFilterBarContext";
10
10
  export * from "./images/useImageUploader";
11
11
  export * from "./images/useIrisAppImage";
12
12
  export * from "./modalDialog/useModalDialogContext";
13
+ export * from "./navigation/useHasAccessTo";
13
14
  export * from "./navigation/useNavigateInHost";
14
15
  export * from "./oemBranding/useOemBrandingContext";
15
16
  export * from "./runtimes/useAssetRuntime";
@@ -22,6 +23,6 @@ export * from "./timeRange/useTimeRange";
22
23
  export * from "./toast/useToast";
23
24
  export * from "./token/useToken";
24
25
  export * from "./useFeatureBranchQueryString";
25
- export * from "./user/useCurrentUserPreference";
26
26
  export * from "./user/useCurrentUser";
27
+ export * from "./user/useCurrentUserPreference";
27
28
  export * from "./widgetConfig/useWidgetConfig";
@@ -0,0 +1,21 @@
1
+ import { HasAccessToOptions } from "@trackunit/iris-app-runtime-core-api";
2
+ /**
3
+ * This is a hook to use the hasAccessTo in host.
4
+ * You can use this to ensure the page you link to is available for the user to see.
5
+ *
6
+ * @requires NavigationContext
7
+ * @example
8
+ * import { useHasAccessTo } from "@trackunit/react-core-hooks";
9
+ * useHasAccessTo({ assetId: assetInfo?.assetId, page: "movement" })
10
+ * @see (@link NavigationRuntimeApi)
11
+ * @see (@link HasAccessToOptions)
12
+ * @see (@link HasAccessToOptions.assetId)
13
+ * @see (@link HasAccessToOptions.page)
14
+ * @see (@link HasAccessToOptions.siteId)
15
+ * @see (@link NavigationRuntimeApi)
16
+ */
17
+ export declare const useHasAccessTo: (options: HasAccessToOptions) => {
18
+ hasAccess: boolean | undefined;
19
+ loading: boolean;
20
+ error: Error | undefined;
21
+ };
@@ -1,4 +1,4 @@
1
- import { HasAccessToOptions, NavigationRuntimeApi } from "@trackunit/iris-app-runtime-core-api";
1
+ import { NavigationRuntimeApi } from "@trackunit/iris-app-runtime-core-api";
2
2
  /**
3
3
  * This is a hook to use the NavigationContext.
4
4
  *
@@ -8,21 +8,3 @@ import { HasAccessToOptions, NavigationRuntimeApi } from "@trackunit/iris-app-ru
8
8
  * @see (@link NavigationRuntimeApi)
9
9
  */
10
10
  export declare const useNavigateInHost: () => NavigationRuntimeApi;
11
- /**
12
- * This is a hook to use the hasAccessTo in host.
13
- * You can use this to ensure the page you link to is available for the user to see.
14
- *
15
- * @requires NavigationContext
16
- * @example
17
- * import { useHasAccessTo } from "@trackunit/react-core-hooks";
18
- * useHasAccessTo({ assetId: assetInfo?.assetId, page: "movement" })
19
- * @see (@link NavigationRuntimeApi)
20
- * @see (@link HasAccessToOptions)
21
- * @see (@link HasAccessToOptions.assetId)
22
- * @see (@link HasAccessToOptions.page)
23
- * @see (@link HasAccessToOptions.siteId)
24
- * @see (@link NavigationRuntimeApi)
25
- */
26
- export declare const useHasAccessTo: (options: HasAccessToOptions) => {
27
- hasAccess: boolean | undefined;
28
- };