@trackunit/react-core-hooks 0.2.112 → 0.2.115

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
@@ -203,6 +203,31 @@ const OemBrandingContextProvider = (props) => {
203
203
  return jsxRuntime.jsx(OemBrandingContext.Provider, Object.assign({}, props));
204
204
  };
205
205
 
206
+ /******************************************************************************
207
+ Copyright (c) Microsoft Corporation.
208
+
209
+ Permission to use, copy, modify, and/or distribute this software for any
210
+ purpose with or without fee is hereby granted.
211
+
212
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
213
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
214
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
215
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
216
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
217
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
218
+ PERFORMANCE OF THIS SOFTWARE.
219
+ ***************************************************************************** */
220
+
221
+ function __awaiter(thisArg, _arguments, P, generator) {
222
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
223
+ return new (P || (P = Promise))(function (resolve, reject) {
224
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
225
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
226
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
227
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
228
+ });
229
+ }
230
+
206
231
  const NavigationContext = React.createContext(null);
207
232
  /**
208
233
  * This is a provider for the NavigationContext.
@@ -225,6 +250,40 @@ const useNavigateInHost = () => {
225
250
  }
226
251
  return context;
227
252
  };
253
+ /**
254
+ * This is a hook to use the hasAccessTo in host.
255
+ * You can use this to ensure the page you link to is available for the user to see.
256
+ *
257
+ * @requires NavigationContext
258
+ * @example
259
+ * import { useHasAccessTo } from "@trackunit/react-core-hooks";
260
+ * useHasAccessTo({ assetId: assetInfo?.assetId, page: "movement" })
261
+ * @see (@link INavigationContext)
262
+ * @see (@link HasAccessToOptions)
263
+ * @see (@link HasAccessToOptions.assetId)
264
+ * @see (@link HasAccessToOptions.page)
265
+ * @see (@link HasAccessToOptions.siteId)
266
+ * @see (@link INavigationContext)
267
+ */
268
+ const useHasAccessTo = (options) => {
269
+ const context = React.useContext(NavigationContext);
270
+ if (!context) {
271
+ throw new Error("useHasAccessTo must be used within an NavigationContext");
272
+ }
273
+ const [hasAccess, setHasAccess] = React.useState();
274
+ React.useEffect(() => {
275
+ function checkAccess() {
276
+ return __awaiter(this, void 0, void 0, function* () {
277
+ if (options.assetId) {
278
+ const doHaveAccess = yield (context === null || context === void 0 ? void 0 : context.hasAccessTo(options));
279
+ setHasAccess(() => doHaveAccess);
280
+ }
281
+ });
282
+ }
283
+ checkAccess();
284
+ }, [context, options]);
285
+ return { hasAccess };
286
+ };
228
287
 
229
288
  /**
230
289
  * A react hook for notifying host about location changes
@@ -242,31 +301,6 @@ const useURLSynchronization = () => {
242
301
  }, [location]);
243
302
  };
244
303
 
245
- /******************************************************************************
246
- Copyright (c) Microsoft Corporation.
247
-
248
- Permission to use, copy, modify, and/or distribute this software for any
249
- purpose with or without fee is hereby granted.
250
-
251
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
252
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
253
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
254
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
255
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
256
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
257
- PERFORMANCE OF THIS SOFTWARE.
258
- ***************************************************************************** */
259
-
260
- function __awaiter(thisArg, _arguments, P, generator) {
261
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
262
- return new (P || (P = Promise))(function (resolve, reject) {
263
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
264
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
265
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
266
- step((generator = generator.apply(thisArg, _arguments || [])).next());
267
- });
268
- }
269
-
270
304
  /**
271
305
  * A hook to expose asset runtime for React components
272
306
  *
@@ -473,7 +507,7 @@ const TokenProvider = (props) => {
473
507
  *
474
508
  * @param items The list of items to filter.
475
509
  * @param props The properties to search in each item.
476
- * @returns A tuple with the filtered items, the search text and a setter for the search text.
510
+ * @returns { Maybe<string>[]): [T[], string, Dispatch<string>] } A tuple with the filtered items, the search text and a setter for the search text.
477
511
  * @example
478
512
  * const [result, searchText, setSearchText] = useTextSearch(items, item => [item.name, item.description]);
479
513
  */
@@ -616,6 +650,7 @@ exports.useCustomFieldRuntimeForEntity = useCustomFieldRuntimeForEntity;
616
650
  exports.useEnvironment = useEnvironment;
617
651
  exports.useFilterBarContext = useFilterBarContext;
618
652
  exports.useGlobalSelection = useGlobalSelection;
653
+ exports.useHasAccessTo = useHasAccessTo;
619
654
  exports.useNavigateInHost = useNavigateInHost;
620
655
  exports.useOemBrandingContext = useOemBrandingContext;
621
656
  exports.useRestRuntime = useRestRuntime;
package/index.esm.js CHANGED
@@ -177,6 +177,31 @@ const OemBrandingContextProvider = (props) => {
177
177
  return jsx(OemBrandingContext.Provider, Object.assign({}, props));
178
178
  };
179
179
 
180
+ /******************************************************************************
181
+ Copyright (c) Microsoft Corporation.
182
+
183
+ Permission to use, copy, modify, and/or distribute this software for any
184
+ purpose with or without fee is hereby granted.
185
+
186
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
187
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
188
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
189
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
190
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
191
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
192
+ PERFORMANCE OF THIS SOFTWARE.
193
+ ***************************************************************************** */
194
+
195
+ function __awaiter(thisArg, _arguments, P, generator) {
196
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
197
+ return new (P || (P = Promise))(function (resolve, reject) {
198
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
199
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
200
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
201
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
202
+ });
203
+ }
204
+
180
205
  const NavigationContext = createContext(null);
181
206
  /**
182
207
  * This is a provider for the NavigationContext.
@@ -199,6 +224,40 @@ const useNavigateInHost = () => {
199
224
  }
200
225
  return context;
201
226
  };
227
+ /**
228
+ * This is a hook to use the hasAccessTo in host.
229
+ * You can use this to ensure the page you link to is available for the user to see.
230
+ *
231
+ * @requires NavigationContext
232
+ * @example
233
+ * import { useHasAccessTo } from "@trackunit/react-core-hooks";
234
+ * useHasAccessTo({ assetId: assetInfo?.assetId, page: "movement" })
235
+ * @see (@link INavigationContext)
236
+ * @see (@link HasAccessToOptions)
237
+ * @see (@link HasAccessToOptions.assetId)
238
+ * @see (@link HasAccessToOptions.page)
239
+ * @see (@link HasAccessToOptions.siteId)
240
+ * @see (@link INavigationContext)
241
+ */
242
+ const useHasAccessTo = (options) => {
243
+ const context = useContext(NavigationContext);
244
+ if (!context) {
245
+ throw new Error("useHasAccessTo must be used within an NavigationContext");
246
+ }
247
+ const [hasAccess, setHasAccess] = useState();
248
+ useEffect(() => {
249
+ function checkAccess() {
250
+ return __awaiter(this, void 0, void 0, function* () {
251
+ if (options.assetId) {
252
+ const doHaveAccess = yield (context === null || context === void 0 ? void 0 : context.hasAccessTo(options));
253
+ setHasAccess(() => doHaveAccess);
254
+ }
255
+ });
256
+ }
257
+ checkAccess();
258
+ }, [context, options]);
259
+ return { hasAccess };
260
+ };
202
261
 
203
262
  /**
204
263
  * A react hook for notifying host about location changes
@@ -216,31 +275,6 @@ const useURLSynchronization = () => {
216
275
  }, [location]);
217
276
  };
218
277
 
219
- /******************************************************************************
220
- Copyright (c) Microsoft Corporation.
221
-
222
- Permission to use, copy, modify, and/or distribute this software for any
223
- purpose with or without fee is hereby granted.
224
-
225
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
226
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
227
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
228
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
229
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
230
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
231
- PERFORMANCE OF THIS SOFTWARE.
232
- ***************************************************************************** */
233
-
234
- function __awaiter(thisArg, _arguments, P, generator) {
235
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
236
- return new (P || (P = Promise))(function (resolve, reject) {
237
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
238
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
239
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
240
- step((generator = generator.apply(thisArg, _arguments || [])).next());
241
- });
242
- }
243
-
244
278
  /**
245
279
  * A hook to expose asset runtime for React components
246
280
  *
@@ -447,7 +481,7 @@ const TokenProvider = (props) => {
447
481
  *
448
482
  * @param items The list of items to filter.
449
483
  * @param props The properties to search in each item.
450
- * @returns A tuple with the filtered items, the search text and a setter for the search text.
484
+ * @returns { Maybe<string>[]): [T[], string, Dispatch<string>] } A tuple with the filtered items, the search text and a setter for the search text.
451
485
  * @example
452
486
  * const [result, searchText, setSearchText] = useTextSearch(items, item => [item.name, item.description]);
453
487
  */
@@ -565,4 +599,4 @@ const useCurrentUser = () => {
565
599
  return context;
566
600
  };
567
601
 
568
- export { AnalyticsContext, AnalyticsContextProvider, AssetSortingProvider, CurrentUserPreferenceProvider, CurrentUserProvider, EnvironmentContextProvider, FilterBarProvider, GlobalSelectionProvider, NavigationContextProvider, OemBrandingContextProvider, ToastProvider, TokenProvider, UserSubscriptionProvider, useAnalytics, useAssetRuntime, useAssetSorting, useCurrentUser, useCurrentUserLanguage, useCurrentUserSystemOfMeasurement, useCurrentUserTimeZonePreference, useCustomFieldRuntime, useCustomFieldRuntimeForEntity, useEnvironment, useFilterBarContext, useGlobalSelection, useNavigateInHost, useOemBrandingContext, useRestRuntime, useSiteRuntime, useTextSearch, useToast, useToken, useURLSynchronization, useUserSubscription };
602
+ export { AnalyticsContext, AnalyticsContextProvider, AssetSortingProvider, CurrentUserPreferenceProvider, CurrentUserProvider, EnvironmentContextProvider, FilterBarProvider, GlobalSelectionProvider, NavigationContextProvider, OemBrandingContextProvider, ToastProvider, TokenProvider, UserSubscriptionProvider, useAnalytics, useAssetRuntime, useAssetSorting, useCurrentUser, useCurrentUserLanguage, useCurrentUserSystemOfMeasurement, useCurrentUserTimeZonePreference, useCustomFieldRuntime, useCustomFieldRuntimeForEntity, useEnvironment, useFilterBarContext, useGlobalSelection, useHasAccessTo, useNavigateInHost, useOemBrandingContext, useRestRuntime, useSiteRuntime, useTextSearch, useToast, useToken, useURLSynchronization, useUserSubscription };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-hooks",
3
- "version": "0.2.112",
3
+ "version": "0.2.115",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import { INavigationContext } from "@trackunit/react-core-contexts-api";
2
+ import { HasAccessToOptions, INavigationContext } from "@trackunit/react-core-contexts-api";
3
3
  interface IProps {
4
4
  value: INavigationContext;
5
5
  children?: React.ReactNode;
@@ -17,4 +17,22 @@ export declare const NavigationContextProvider: (props: IProps) => JSX.Element;
17
17
  * @see (@link INavigationContext)
18
18
  */
19
19
  export declare const useNavigateInHost: () => INavigationContext;
20
+ /**
21
+ * This is a hook to use the hasAccessTo in host.
22
+ * You can use this to ensure the page you link to is available for the user to see.
23
+ *
24
+ * @requires NavigationContext
25
+ * @example
26
+ * import { useHasAccessTo } from "@trackunit/react-core-hooks";
27
+ * useHasAccessTo({ assetId: assetInfo?.assetId, page: "movement" })
28
+ * @see (@link INavigationContext)
29
+ * @see (@link HasAccessToOptions)
30
+ * @see (@link HasAccessToOptions.assetId)
31
+ * @see (@link HasAccessToOptions.page)
32
+ * @see (@link HasAccessToOptions.siteId)
33
+ * @see (@link INavigationContext)
34
+ */
35
+ export declare const useHasAccessTo: (options: HasAccessToOptions) => {
36
+ hasAccess: boolean | undefined;
37
+ };
20
38
  export {};
@@ -5,7 +5,7 @@ import { Dispatch } from "react";
5
5
  *
6
6
  * @param items The list of items to filter.
7
7
  * @param props The properties to search in each item.
8
- * @returns A tuple with the filtered items, the search text and a setter for the search text.
8
+ * @returns { Maybe<string>[]): [T[], string, Dispatch<string>] } A tuple with the filtered items, the search text and a setter for the search text.
9
9
  * @example
10
10
  * const [result, searchText, setSearchText] = useTextSearch(items, item => [item.name, item.description]);
11
11
  */