@trackunit/react-core-hooks 0.2.64 → 0.2.65

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.js CHANGED
@@ -2,189 +2,363 @@ import * as React from 'react';
2
2
  import React__default, { createContext, useContext, useState, useEffect } from 'react';
3
3
  import { jsx } from 'react/jsx-runtime';
4
4
  import { NavigationRuntime, AssetRuntime, CustomFieldRuntime, RestRuntime, SiteRuntime } from '@trackunit/iris-app-runtime-core';
5
- import { useLocation } from 'react-router-dom';
6
-
7
- const AnalyticsContext = createContext(null);
8
- const AnalyticsContextProvider = AnalyticsContext.Provider; // easy import
9
- /**
10
- * Hook to get the analytics context.
11
- *
12
- * @requires AnalyticsProvider
13
- * @example
14
- * import { useAnalytics, useEnvironment } from "@trackunit/react-core-hooks";
15
- * const { logPageView, logEvent } = useAnalytics(AllEvents);
16
- *
17
- * // log page view event
18
- * useEffect(() => {
19
- * logPageView({ pageName: "login" });
20
- * }, [logPageView]);
21
- *
22
- * // log event when appropriate
23
- * logEvent("Login", { loginPage: "New Manager" });
24
- *
25
- * @see {@link IAnalyticsContext}
26
- */
27
- const useAnalytics = (type) => {
28
- const context = useContext(AnalyticsContext);
29
- if (!context) {
30
- throw new Error("useAnalytics must be used within an AnalyticsProvider");
31
- }
32
- return context;
5
+
6
+ const AnalyticsContext = createContext(null);
7
+ const AnalyticsContextProvider = AnalyticsContext.Provider; // easy import
8
+ /**
9
+ * Hook to get the analytics context.
10
+ *
11
+ * @requires AnalyticsProvider
12
+ * @example
13
+ * import { useAnalytics, useEnvironment } from "@trackunit/react-core-hooks";
14
+ * const { logPageView, logEvent } = useAnalytics(AllEvents);
15
+ *
16
+ * // log page view event
17
+ * useEffect(() => {
18
+ * logPageView({ pageName: "login" });
19
+ * }, [logPageView]);
20
+ *
21
+ * // log event when appropriate
22
+ * logEvent("Login", { loginPage: "New Manager" });
23
+ *
24
+ * @see {@link IAnalyticsContext}
25
+ */
26
+ const useAnalytics = (type) => {
27
+ const context = useContext(AnalyticsContext);
28
+ if (!context) {
29
+ throw new Error("useAnalytics must be used within an AnalyticsProvider");
30
+ }
31
+ return context;
33
32
  };
34
33
 
35
- const AssetSortingContext = createContext(null);
36
- /**
37
- * This is a provider for the AssetSortingContext.
38
- */
39
- const AssetSortingProvider = AssetSortingContext.Provider;
40
- /**
41
- * This is a hook to use the AssetSortingContext.
42
- *
43
- * @requires AssetSortingProvider
44
- * @example
45
- * import { useAssetSorting } from "@trackunit/react-core-hooks";
46
- * const { sortingState, setSortBy } = useAssetSorting();
47
- *
48
- * const initialSort = useMemo(
49
- * () => ({
50
- * property: {
51
- * label: sortingState.sortBy,
52
- * value: sortingState.sortBy,
53
- * },
54
- * order: sortingState.order,
55
- * }),
56
- * [sortingState]
57
- * );
58
- *
59
- * return (
60
- * <Table
61
- * ...
62
- * headerOnSort={setSortBy}
63
- * headerInitialSort={initialSort}
64
- * />
65
- * );
66
- *
67
- * @see {@link IAssetSortingContext}
68
- */
69
- const useAssetSorting = () => {
70
- const context = useContext(AssetSortingContext);
71
- if (!context) {
72
- throw new Error("useAssetSorting must be used within a AssetSortingProvider");
73
- }
74
- return context;
34
+ const AssetSortingContext = createContext(null);
35
+ /**
36
+ * This is a provider for the AssetSortingContext.
37
+ */
38
+ const AssetSortingProvider = AssetSortingContext.Provider;
39
+ /**
40
+ * This is a hook to use the AssetSortingContext.
41
+ *
42
+ * @requires AssetSortingProvider
43
+ * @example
44
+ * import { useAssetSorting } from "@trackunit/react-core-hooks";
45
+ * const { sortingState, setSortBy } = useAssetSorting();
46
+ *
47
+ * const initialSort = useMemo(
48
+ * () => ({
49
+ * property: {
50
+ * label: sortingState.sortBy,
51
+ * value: sortingState.sortBy,
52
+ * },
53
+ * order: sortingState.order,
54
+ * }),
55
+ * [sortingState]
56
+ * );
57
+ *
58
+ * return (
59
+ * <Table
60
+ * ...
61
+ * headerOnSort={setSortBy}
62
+ * headerInitialSort={initialSort}
63
+ * />
64
+ * );
65
+ *
66
+ * @see {@link IAssetSortingContext}
67
+ */
68
+ const useAssetSorting = () => {
69
+ const context = useContext(AssetSortingContext);
70
+ if (!context) {
71
+ throw new Error("useAssetSorting must be used within a AssetSortingProvider");
72
+ }
73
+ return context;
75
74
  };
76
75
 
77
- const EnvironmentContext = createContext(null);
78
- /**
79
- * This is a provider for the EnvironmentContext.
80
- */
81
- const EnvironmentContextProvider = (props) => {
82
- return jsx(EnvironmentContext.Provider, Object.assign({}, props));
83
- };
84
- /**
85
- * This is a hook to use the EnvironmentContext.
86
- *
87
- * @requires EnvironmentContext
88
- * @example
89
- * import { useEnvironment } from "@trackunit/react-core-hooks";
90
- * const { googleMapsApiKey } = useEnvironment();
91
- * // use api key for something...
92
- *
93
- * @see (@link IEnvironmentContext)
94
- */
95
- const useEnvironment = () => {
96
- const context = useContext(EnvironmentContext);
97
- if (!context) {
98
- throw new Error("useEnvironment must be used within an EnvironmentContext");
99
- }
100
- return context;
76
+ const EnvironmentContext = createContext(null);
77
+ /**
78
+ * This is a provider for the EnvironmentContext.
79
+ */
80
+ const EnvironmentContextProvider = (props) => {
81
+ return jsx(EnvironmentContext.Provider, Object.assign({}, props));
82
+ };
83
+ /**
84
+ * This is a hook to use the EnvironmentContext.
85
+ *
86
+ * @requires EnvironmentContext
87
+ * @example
88
+ * import { useEnvironment } from "@trackunit/react-core-hooks";
89
+ * const { googleMapsApiKey } = useEnvironment();
90
+ * // use api key for something...
91
+ *
92
+ * @see (@link IEnvironmentContext)
93
+ */
94
+ const useEnvironment = () => {
95
+ const context = useContext(EnvironmentContext);
96
+ if (!context) {
97
+ throw new Error("useEnvironment must be used within an EnvironmentContext");
98
+ }
99
+ return context;
101
100
  };
102
101
 
103
- const GlobalSelectionContext = React.createContext(null);
104
- /**
105
- * This is a hook to use the GlobalSelectionContext.
106
- *
107
- * @requires GlobalSelectionProvider
108
- * @example
109
- * import { useGlobalSelection } from "@trackunit/react-core-hooks";
110
- *
111
- * export const useActiveGlobalGroupIdFilter = () => {
112
- * const { selection } = useGlobalSelection();
113
- * return (selection?.type === "group") ? selection.groupId : undefined;
114
- * };
115
- *
116
- * @see {@link IGlobalSelectionContext}
117
- */
118
- const useGlobalSelection = () => {
119
- const context = React.useContext(GlobalSelectionContext);
120
- if (!context) {
121
- throw new Error("useGlobalSelection must be used within the GlobalSelectionContext");
122
- }
123
- return context;
124
- };
125
- /**
126
- * This is a provider for the GlobalSelectionContext.
127
- */
102
+ const GlobalSelectionContext = React.createContext(null);
103
+ /**
104
+ * This is a hook to use the GlobalSelectionContext.
105
+ *
106
+ * @requires GlobalSelectionProvider
107
+ * @example
108
+ * import { useGlobalSelection } from "@trackunit/react-core-hooks";
109
+ *
110
+ * export const useActiveGlobalGroupIdFilter = () => {
111
+ * const { selection } = useGlobalSelection();
112
+ * return (selection?.type === "group") ? selection.groupId : undefined;
113
+ * };
114
+ *
115
+ * @see {@link IGlobalSelectionContext}
116
+ */
117
+ const useGlobalSelection = () => {
118
+ const context = React.useContext(GlobalSelectionContext);
119
+ if (!context) {
120
+ throw new Error("useGlobalSelection must be used within the GlobalSelectionContext");
121
+ }
122
+ return context;
123
+ };
124
+ /**
125
+ * This is a provider for the GlobalSelectionContext.
126
+ */
128
127
  const GlobalSelectionProvider = (props) => jsx(GlobalSelectionContext.Provider, Object.assign({}, props));
129
128
 
130
- const OemBrandingContext = createContext(null);
131
- /**
132
- * This is a hook to use the IOemBrandingContext.
133
- *
134
- * @requires OemBrandingContextProvider
135
- * @example
136
- * import { useOemBrandingContext } from "@trackunit/react-core-hooks";
137
- * const { getOemBranding, getImageByBrand } = useOemBrandingContext();
138
- * // use oem branding
139
- * const branding = getOemBranding("some brand")
140
- *
141
- * @see {@link IOemBrandingContext}
142
- */
143
- const useOemBrandingContext = () => {
144
- const context = useContext(OemBrandingContext);
145
- if (!context) {
146
- throw new Error("useOemBranding must be used within an OemBrandingContextProvider");
147
- }
148
- return context;
149
- };
150
- /**
151
- * This is a provider for the IOemBrandingContext.
152
- */
153
- const OemBrandingContextProvider = (props) => {
154
- return jsx(OemBrandingContext.Provider, Object.assign({}, props));
129
+ const OemBrandingContext = createContext(null);
130
+ /**
131
+ * This is a hook to use the IOemBrandingContext.
132
+ *
133
+ * @requires OemBrandingContextProvider
134
+ * @example
135
+ * import { useOemBrandingContext } from "@trackunit/react-core-hooks";
136
+ * const { getOemBranding, getImageByBrand } = useOemBrandingContext();
137
+ * // use oem branding
138
+ * const branding = getOemBranding("some brand")
139
+ *
140
+ * @see {@link IOemBrandingContext}
141
+ */
142
+ const useOemBrandingContext = () => {
143
+ const context = useContext(OemBrandingContext);
144
+ if (!context) {
145
+ throw new Error("useOemBranding must be used within an OemBrandingContextProvider");
146
+ }
147
+ return context;
148
+ };
149
+ /**
150
+ * This is a provider for the IOemBrandingContext.
151
+ */
152
+ const OemBrandingContextProvider = (props) => {
153
+ return jsx(OemBrandingContext.Provider, Object.assign({}, props));
155
154
  };
156
155
 
157
- /**
158
- * A react hook for notifying host about location changes
159
- */
160
- const useURLSynchronization = () => {
161
- const location = useLocation();
162
- React__default.useEffect(() => {
163
- const deepLink = {
164
- path: location.pathname,
165
- search: location.search,
166
- hash: location.hash,
167
- pathname: "",
168
- };
169
- NavigationRuntime.setDeepLink(deepLink);
170
- }, [location]);
156
+ /**
157
+ * @remix-run/router v1.6.2
158
+ *
159
+ * Copyright (c) Remix Software Inc.
160
+ *
161
+ * This source code is licensed under the MIT license found in the
162
+ * LICENSE.md file in the root directory of this source tree.
163
+ *
164
+ * @license MIT
165
+ */
166
+
167
+ ////////////////////////////////////////////////////////////////////////////////
168
+ //#region Types and Constants
169
+ ////////////////////////////////////////////////////////////////////////////////
170
+
171
+ /**
172
+ * Actions represent the type of change to a location value.
173
+ */
174
+ var Action;
175
+
176
+ (function (Action) {
177
+ /**
178
+ * A POP indicates a change to an arbitrary index in the history stack, such
179
+ * as a back or forward navigation. It does not describe the direction of the
180
+ * navigation, only that the current index changed.
181
+ *
182
+ * Note: This is the default action for newly created history objects.
183
+ */
184
+ Action["Pop"] = "POP";
185
+ /**
186
+ * A PUSH indicates a new entry being added to the history stack, such as when
187
+ * a link is clicked and a new page loads. When this happens, all subsequent
188
+ * entries in the stack are lost.
189
+ */
190
+
191
+ Action["Push"] = "PUSH";
192
+ /**
193
+ * A REPLACE indicates the entry at the current index in the history stack
194
+ * being replaced by a new one.
195
+ */
196
+
197
+ Action["Replace"] = "REPLACE";
198
+ })(Action || (Action = {}));
199
+ function invariant(value, message) {
200
+ if (value === false || value === null || typeof value === "undefined") {
201
+ throw new Error(message);
202
+ }
203
+ }
204
+
205
+ var ResultType;
206
+
207
+ (function (ResultType) {
208
+ ResultType["data"] = "data";
209
+ ResultType["deferred"] = "deferred";
210
+ ResultType["redirect"] = "redirect";
211
+ ResultType["error"] = "error";
212
+ })(ResultType || (ResultType = {}));
213
+
214
+ const validMutationMethodsArr = ["post", "put", "patch", "delete"];
215
+ ["get", ...validMutationMethodsArr];
216
+
217
+ /**
218
+ * React Router v6.11.2
219
+ *
220
+ * Copyright (c) Remix Software Inc.
221
+ *
222
+ * This source code is licensed under the MIT license found in the
223
+ * LICENSE.md file in the root directory of this source tree.
224
+ *
225
+ * @license MIT
226
+ */
227
+
228
+ const DataRouterContext = /*#__PURE__*/React.createContext(null);
229
+
230
+ if (process.env.NODE_ENV !== "production") {
231
+ DataRouterContext.displayName = "DataRouter";
232
+ }
233
+
234
+ const DataRouterStateContext = /*#__PURE__*/React.createContext(null);
235
+
236
+ if (process.env.NODE_ENV !== "production") {
237
+ DataRouterStateContext.displayName = "DataRouterState";
238
+ }
239
+
240
+ const AwaitContext = /*#__PURE__*/React.createContext(null);
241
+
242
+ if (process.env.NODE_ENV !== "production") {
243
+ AwaitContext.displayName = "Await";
244
+ }
245
+
246
+ const NavigationContext = /*#__PURE__*/React.createContext(null);
247
+
248
+ if (process.env.NODE_ENV !== "production") {
249
+ NavigationContext.displayName = "Navigation";
250
+ }
251
+
252
+ const LocationContext = /*#__PURE__*/React.createContext(null);
253
+
254
+ if (process.env.NODE_ENV !== "production") {
255
+ LocationContext.displayName = "Location";
256
+ }
257
+
258
+ const RouteContext = /*#__PURE__*/React.createContext({
259
+ outlet: null,
260
+ matches: [],
261
+ isDataRoute: false
262
+ });
263
+
264
+ if (process.env.NODE_ENV !== "production") {
265
+ RouteContext.displayName = "Route";
266
+ }
267
+
268
+ const RouteErrorContext = /*#__PURE__*/React.createContext(null);
269
+
270
+ if (process.env.NODE_ENV !== "production") {
271
+ RouteErrorContext.displayName = "RouteError";
272
+ }
273
+ /**
274
+ * Returns true if this component is a descendant of a <Router>.
275
+ *
276
+ * @see https://reactrouter.com/hooks/use-in-router-context
277
+ */
278
+
279
+ function useInRouterContext() {
280
+ return React.useContext(LocationContext) != null;
281
+ }
282
+ /**
283
+ * Returns the current location object, which represents the current URL in web
284
+ * browsers.
285
+ *
286
+ * Note: If you're using this it may mean you're doing some of your own
287
+ * "routing" in your app, and we'd like to know what your use case is. We may
288
+ * be able to provide something higher-level to better suit your needs.
289
+ *
290
+ * @see https://reactrouter.com/hooks/use-location
291
+ */
292
+
293
+ function useLocation() {
294
+ !useInRouterContext() ? process.env.NODE_ENV !== "production" ? invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
295
+ // router loaded. We can help them understand how to avoid that.
296
+ "useLocation() may be used only in the context of a <Router> component.") : invariant(false) : void 0;
297
+ return React.useContext(LocationContext).location;
298
+ }
299
+ var DataRouterHook;
300
+
301
+ (function (DataRouterHook) {
302
+ DataRouterHook["UseBlocker"] = "useBlocker";
303
+ DataRouterHook["UseRevalidator"] = "useRevalidator";
304
+ DataRouterHook["UseNavigateStable"] = "useNavigate";
305
+ })(DataRouterHook || (DataRouterHook = {}));
306
+
307
+ var DataRouterStateHook;
308
+
309
+ (function (DataRouterStateHook) {
310
+ DataRouterStateHook["UseBlocker"] = "useBlocker";
311
+ DataRouterStateHook["UseLoaderData"] = "useLoaderData";
312
+ DataRouterStateHook["UseActionData"] = "useActionData";
313
+ DataRouterStateHook["UseRouteError"] = "useRouteError";
314
+ DataRouterStateHook["UseNavigation"] = "useNavigation";
315
+ DataRouterStateHook["UseRouteLoaderData"] = "useRouteLoaderData";
316
+ DataRouterStateHook["UseMatches"] = "useMatches";
317
+ DataRouterStateHook["UseRevalidator"] = "useRevalidator";
318
+ DataRouterStateHook["UseNavigateStable"] = "useNavigate";
319
+ DataRouterStateHook["UseRouteId"] = "useRouteId";
320
+ })(DataRouterStateHook || (DataRouterStateHook = {}));
321
+ var AwaitRenderStatus;
322
+
323
+ (function (AwaitRenderStatus) {
324
+ AwaitRenderStatus[AwaitRenderStatus["pending"] = 0] = "pending";
325
+ AwaitRenderStatus[AwaitRenderStatus["success"] = 1] = "success";
326
+ AwaitRenderStatus[AwaitRenderStatus["error"] = 2] = "error";
327
+ })(AwaitRenderStatus || (AwaitRenderStatus = {}));
328
+
329
+ new Promise(() => {});
330
+
331
+ /**
332
+ * A react hook for notifying host about location changes
333
+ */
334
+ const useURLSynchronization = () => {
335
+ const location = useLocation();
336
+ React__default.useEffect(() => {
337
+ const deepLink = {
338
+ path: location.pathname,
339
+ search: location.search,
340
+ hash: location.hash,
341
+ pathname: "",
342
+ };
343
+ NavigationRuntime.setDeepLink(deepLink);
344
+ }, [location]);
171
345
  };
172
346
 
173
- /**
174
- * A hook to expose navigation runtime for React components
175
- *
176
- * @requires NavigationRuntime
177
- * @returns {UseNavigationRuntime} navigationRuntime
178
- * @example
179
- * import { useNavigationRuntime } from "@trackunit/react-core-hooks";
180
- * const { navigateTo } = useNavigationRuntime();
181
- * // ...
182
- * <Link onClick={() => gotoAssetHome(asset.id)} to="">
183
- * {asset.name}
184
- * </Link>
185
- */
186
- const useNavigationRuntime = () => {
187
- return NavigationRuntime;
347
+ /**
348
+ * A hook to expose navigation runtime for React components
349
+ *
350
+ * @requires NavigationRuntime
351
+ * @returns {UseNavigationRuntime} navigationRuntime
352
+ * @example
353
+ * import { useNavigationRuntime } from "@trackunit/react-core-hooks";
354
+ * const { navigateTo } = useNavigationRuntime();
355
+ * // ...
356
+ * <Link onClick={() => gotoAssetHome(asset.id)} to="">
357
+ * {asset.name}
358
+ * </Link>
359
+ */
360
+ const useNavigationRuntime = () => {
361
+ return NavigationRuntime;
188
362
  };
189
363
 
190
364
  /******************************************************************************
@@ -212,240 +386,240 @@ function __awaiter(thisArg, _arguments, P, generator) {
212
386
  });
213
387
  }
214
388
 
215
- /**
216
- * A hook to expose asset runtime for React components
217
- *
218
- * @requires AssetRuntime
219
- * @returns {UseAssetRuntime} assetRuntime
220
- * @example
221
- * import { useAssetRuntime } from "@trackunit/react-core-hooks";
222
- * const { assetInfo } = useAssetRuntime();
223
- * useEffect(() => {
224
- * (async () => {
225
- * if (assetInfo) {
226
- * getAssetLocation({ variables: { id: assetInfo.assetId } });
227
- * }
228
- * })();
229
- * }, [assetInfo, getAssetLocation]);
230
- *
231
- */
232
- const useAssetRuntime = () => {
233
- const [assetInfo, setAssetInfo] = useState();
234
- useEffect(() => {
235
- const getAssetInfo = () => __awaiter(void 0, void 0, void 0, function* () {
236
- const updatedAssetInfo = yield AssetRuntime.getAssetInfo();
237
- setAssetInfo(updatedAssetInfo);
238
- });
239
- getAssetInfo();
240
- }, []);
241
- return { assetInfo };
389
+ /**
390
+ * A hook to expose asset runtime for React components
391
+ *
392
+ * @requires AssetRuntime
393
+ * @returns {UseAssetRuntime} assetRuntime
394
+ * @example
395
+ * import { useAssetRuntime } from "@trackunit/react-core-hooks";
396
+ * const { assetInfo } = useAssetRuntime();
397
+ * useEffect(() => {
398
+ * (async () => {
399
+ * if (assetInfo) {
400
+ * getAssetLocation({ variables: { id: assetInfo.assetId } });
401
+ * }
402
+ * })();
403
+ * }, [assetInfo, getAssetLocation]);
404
+ *
405
+ */
406
+ const useAssetRuntime = () => {
407
+ const [assetInfo, setAssetInfo] = useState();
408
+ useEffect(() => {
409
+ const getAssetInfo = () => __awaiter(void 0, void 0, void 0, function* () {
410
+ const updatedAssetInfo = yield AssetRuntime.getAssetInfo();
411
+ setAssetInfo(updatedAssetInfo);
412
+ });
413
+ getAssetInfo();
414
+ }, []);
415
+ return { assetInfo };
242
416
  };
243
417
 
244
- /**
245
- * A hook to expose custom field runtime methods
246
- *
247
- * @returns {UseCustomFieldRuntime} CustomFieldRuntime
248
- */
249
- const useCustomFieldRuntime = () => {
250
- return CustomFieldRuntime;
418
+ /**
419
+ * A hook to expose custom field runtime methods
420
+ *
421
+ * @returns {UseCustomFieldRuntime} CustomFieldRuntime
422
+ */
423
+ const useCustomFieldRuntime = () => {
424
+ return CustomFieldRuntime;
251
425
  };
252
426
 
253
- /**
254
- * This hook is a wrapper around useCustomFieldRuntime that automatically fetches and sets custom fields for a given entity.
255
- *
256
- * @param entity The entity to fetch and set custom fields for.
257
- * @returns The custom fields for the entity, and functions to set custom fields for the entity.
258
- * @example
259
- * import { useAssetRuntime, useCustomFieldRuntimeForEntity } from '@trackunit/react-core-hooks';
260
- * const { assetInfo } = useAssetRuntime();
261
- * const { customFields, setCustomFieldsForEntity } = useCustomFieldRuntimeForEntity( {
262
- * id: assetInfo?.assetId || '',
263
- * type: 'ASSET'
264
- * });
265
- *
266
- * // set custom field data
267
- * setCustomFieldsForEntity({"key": "value"});
268
- */
269
- const useCustomFieldRuntimeForEntity = (entity) => {
270
- const { getCustomFieldsFor, setCustomFieldsFromFormData, setCustomFieldsFor } = useCustomFieldRuntime();
271
- const [customFields, setCustomFields] = useState();
272
- useEffect(() => {
273
- if (entity.id) {
274
- (() => __awaiter(void 0, void 0, void 0, function* () {
275
- setCustomFields(yield getCustomFieldsFor(entity));
276
- }))();
277
- }
278
- else {
279
- setCustomFields([]);
280
- }
281
- }, [entity, getCustomFieldsFor]);
282
- const setCustomFieldsForEntity = (values) => __awaiter(void 0, void 0, void 0, function* () {
283
- if (entity.id) {
284
- yield setCustomFieldsFor(entity, values);
285
- }
286
- });
287
- const setCustomFieldsFromFormDataForEntity = (formData) => __awaiter(void 0, void 0, void 0, function* () {
288
- if (entity.id) {
289
- yield setCustomFieldsFromFormData(entity, formData, customFields || []);
290
- }
291
- });
292
- return {
293
- customFields,
294
- setCustomFieldsFromFormDataForEntity,
295
- setCustomFieldsForEntity,
296
- };
427
+ /**
428
+ * This hook is a wrapper around useCustomFieldRuntime that automatically fetches and sets custom fields for a given entity.
429
+ *
430
+ * @param entity The entity to fetch and set custom fields for.
431
+ * @returns The custom fields for the entity, and functions to set custom fields for the entity.
432
+ * @example
433
+ * import { useAssetRuntime, useCustomFieldRuntimeForEntity } from '@trackunit/react-core-hooks';
434
+ * const { assetInfo } = useAssetRuntime();
435
+ * const { customFields, setCustomFieldsForEntity } = useCustomFieldRuntimeForEntity( {
436
+ * id: assetInfo?.assetId || '',
437
+ * type: 'ASSET'
438
+ * });
439
+ *
440
+ * // set custom field data
441
+ * setCustomFieldsForEntity({"key": "value"});
442
+ */
443
+ const useCustomFieldRuntimeForEntity = (entity) => {
444
+ const { getCustomFieldsFor, setCustomFieldsFromFormData, setCustomFieldsFor } = useCustomFieldRuntime();
445
+ const [customFields, setCustomFields] = useState();
446
+ useEffect(() => {
447
+ if (entity.id) {
448
+ (() => __awaiter(void 0, void 0, void 0, function* () {
449
+ setCustomFields(yield getCustomFieldsFor(entity));
450
+ }))();
451
+ }
452
+ else {
453
+ setCustomFields([]);
454
+ }
455
+ }, [entity, getCustomFieldsFor]);
456
+ const setCustomFieldsForEntity = (values) => __awaiter(void 0, void 0, void 0, function* () {
457
+ if (entity.id) {
458
+ yield setCustomFieldsFor(entity, values);
459
+ }
460
+ });
461
+ const setCustomFieldsFromFormDataForEntity = (formData) => __awaiter(void 0, void 0, void 0, function* () {
462
+ if (entity.id) {
463
+ yield setCustomFieldsFromFormData(entity, formData, customFields || []);
464
+ }
465
+ });
466
+ return {
467
+ customFields,
468
+ setCustomFieldsFromFormDataForEntity,
469
+ setCustomFieldsForEntity,
470
+ };
297
471
  };
298
472
 
299
- /**
300
- * A hook to expose rest runtime to be used in React components
301
- *
302
- * @returns {UseRestRuntime} a RestRuntime
303
- */
304
- const useRestRuntime = () => {
305
- return RestRuntime;
473
+ /**
474
+ * A hook to expose rest runtime to be used in React components
475
+ *
476
+ * @returns {UseRestRuntime} a RestRuntime
477
+ */
478
+ const useRestRuntime = () => {
479
+ return RestRuntime;
306
480
  };
307
481
 
308
- /**
309
- * A hook to expose site runtime for React components
310
- *
311
- * @requires SiteRuntime
312
- * @returns {UseSiteRuntime} siteRuntime
313
- * @example
314
- * import { useSiteRuntime } from "@trackunit/react-core-hooks";
315
- * const { siteInfo } = useSiteRuntime();
316
- * // use siteInfo to get assets for instance.
317
- * useEffect(() => {
318
- * (async () => {
319
- * if (siteInfo?.siteId) {
320
- * getSiteAssets({ variables: { siteId: siteInfo?.siteId, siteIdStr: siteInfo?.siteId } });
321
- * }
322
- * })();
323
- * }, [getSiteAssets, siteInfo]);
324
- *
325
- */
326
- const useSiteRuntime = () => {
327
- const [siteInfo, setSiteInfo] = useState();
328
- useEffect(() => {
329
- const getSiteInfo = () => __awaiter(void 0, void 0, void 0, function* () {
330
- const updatedSiteInfo = yield SiteRuntime.getSiteInfo();
331
- setSiteInfo(updatedSiteInfo);
332
- });
333
- getSiteInfo();
334
- }, []);
335
- return { siteInfo };
482
+ /**
483
+ * A hook to expose site runtime for React components
484
+ *
485
+ * @requires SiteRuntime
486
+ * @returns {UseSiteRuntime} siteRuntime
487
+ * @example
488
+ * import { useSiteRuntime } from "@trackunit/react-core-hooks";
489
+ * const { siteInfo } = useSiteRuntime();
490
+ * // use siteInfo to get assets for instance.
491
+ * useEffect(() => {
492
+ * (async () => {
493
+ * if (siteInfo?.siteId) {
494
+ * getSiteAssets({ variables: { siteId: siteInfo?.siteId, siteIdStr: siteInfo?.siteId } });
495
+ * }
496
+ * })();
497
+ * }, [getSiteAssets, siteInfo]);
498
+ *
499
+ */
500
+ const useSiteRuntime = () => {
501
+ const [siteInfo, setSiteInfo] = useState();
502
+ useEffect(() => {
503
+ const getSiteInfo = () => __awaiter(void 0, void 0, void 0, function* () {
504
+ const updatedSiteInfo = yield SiteRuntime.getSiteInfo();
505
+ setSiteInfo(updatedSiteInfo);
506
+ });
507
+ getSiteInfo();
508
+ }, []);
509
+ return { siteInfo };
336
510
  };
337
511
 
338
- const UserSubscriptionContext = React.createContext(null);
339
- /**
340
- * This is a provider for the UserSubscriptionContext.
341
- */
342
- const UserSubscriptionProvider = (props) => {
343
- return jsx(UserSubscriptionContext.Provider, Object.assign({}, props));
344
- };
345
- /**
346
- * This is a hook to use the UserSubscriptionContext.
347
- *
348
- * @requires UserSubscriptionProvider
349
- * @example
350
- * import { useUserSubscription } from "@trackunit/react-core-hooks";
351
- * const { numberOfDaysWithAccessToHistoricalData, packageType } = useUserSubscription();
352
- * // use it for something
353
- * const data = fetchData(numberOfDaysWithAccessToHistoricalData)
354
- *
355
- * @see {@link IUserSubscriptionContext}
356
- */
357
- const useUserSubscription = () => {
358
- const context = React.useContext(UserSubscriptionContext);
359
- if (!context) {
360
- throw new Error("Cannot use useUserSubscription outside UserSubscriptionProvider");
361
- }
362
- return context;
512
+ const UserSubscriptionContext = React.createContext(null);
513
+ /**
514
+ * This is a provider for the UserSubscriptionContext.
515
+ */
516
+ const UserSubscriptionProvider = (props) => {
517
+ return jsx(UserSubscriptionContext.Provider, Object.assign({}, props));
518
+ };
519
+ /**
520
+ * This is a hook to use the UserSubscriptionContext.
521
+ *
522
+ * @requires UserSubscriptionProvider
523
+ * @example
524
+ * import { useUserSubscription } from "@trackunit/react-core-hooks";
525
+ * const { numberOfDaysWithAccessToHistoricalData, packageType } = useUserSubscription();
526
+ * // use it for something
527
+ * const data = fetchData(numberOfDaysWithAccessToHistoricalData)
528
+ *
529
+ * @see {@link IUserSubscriptionContext}
530
+ */
531
+ const useUserSubscription = () => {
532
+ const context = React.useContext(UserSubscriptionContext);
533
+ if (!context) {
534
+ throw new Error("Cannot use useUserSubscription outside UserSubscriptionProvider");
535
+ }
536
+ return context;
363
537
  };
364
538
 
365
- const ToastContext = React.createContext(null);
366
- /**
367
- * This is a provider for the ToastContext.
368
- */
369
- const ToastProvider = (props) => jsx(ToastContext.Provider, Object.assign({}, props));
370
- /**
371
- * This is a hook to use the ToastContext.
372
- *
373
- * @requires ToastProvider
374
- * @example
375
- * import { useToast } from "@trackunit/react-core-hooks";
376
- * const { addToast } = useToast();
377
- * // use the toast
378
- * error &&
379
- * addToast({
380
- * intent: "warning",
381
- * title: t("assetHome.specification.failedToSave"),
382
- * description: error?.message,
383
- * duration: 3000,
384
- * });
385
- *
386
- * @see {@link IToastContext}
387
- */
388
- const useToast = () => {
389
- const toastContext = React.useContext(ToastContext);
390
- if (!toastContext) {
391
- throw new Error("useToast must be used within the ToastProvider");
392
- }
393
- return toastContext;
539
+ const ToastContext = React.createContext(null);
540
+ /**
541
+ * This is a provider for the ToastContext.
542
+ */
543
+ const ToastProvider = (props) => jsx(ToastContext.Provider, Object.assign({}, props));
544
+ /**
545
+ * This is a hook to use the ToastContext.
546
+ *
547
+ * @requires ToastProvider
548
+ * @example
549
+ * import { useToast } from "@trackunit/react-core-hooks";
550
+ * const { addToast } = useToast();
551
+ * // use the toast
552
+ * error &&
553
+ * addToast({
554
+ * intent: "warning",
555
+ * title: t("assetHome.specification.failedToSave"),
556
+ * description: error?.message,
557
+ * duration: 3000,
558
+ * });
559
+ *
560
+ * @see {@link IToastContext}
561
+ */
562
+ const useToast = () => {
563
+ const toastContext = React.useContext(ToastContext);
564
+ if (!toastContext) {
565
+ throw new Error("useToast must be used within the ToastProvider");
566
+ }
567
+ return toastContext;
394
568
  };
395
569
 
396
- const TokenContext = React.createContext(null);
397
- /**
398
- * This is a hook to use the TokenContext.
399
- *
400
- * @requires TokenProvider
401
- * @example
402
- * import { useToken } from "@trackunit/react-core-hooks";
403
- * const { token } = useToken();
404
- *
405
- * @see {@link ITokenContext}
406
- */
407
- const useToken = () => {
408
- const context = React.useContext(TokenContext);
409
- if (!context) {
410
- throw new Error("useToken must be used within the TokenContext");
411
- }
412
- return context;
413
- };
414
- /**
415
- * This is a provider for the TokenContext.
416
- */
417
- const TokenProvider = (props) => {
418
- return jsx(TokenContext.Provider, Object.assign({}, props));
570
+ const TokenContext = React.createContext(null);
571
+ /**
572
+ * This is a hook to use the TokenContext.
573
+ *
574
+ * @requires TokenProvider
575
+ * @example
576
+ * import { useToken } from "@trackunit/react-core-hooks";
577
+ * const { token } = useToken();
578
+ *
579
+ * @see {@link ITokenContext}
580
+ */
581
+ const useToken = () => {
582
+ const context = React.useContext(TokenContext);
583
+ if (!context) {
584
+ throw new Error("useToken must be used within the TokenContext");
585
+ }
586
+ return context;
587
+ };
588
+ /**
589
+ * This is a provider for the TokenContext.
590
+ */
591
+ const TokenProvider = (props) => {
592
+ return jsx(TokenContext.Provider, Object.assign({}, props));
419
593
  };
420
594
 
421
- const CurrentUserContext = React.createContext(null);
422
- /**
423
- * This is a provider for the CurrentUserContext.
424
- */
425
- const CurrentUserProvider = (props) => {
426
- return jsx(CurrentUserContext.Provider, Object.assign({}, props));
427
- };
428
- /**
429
- * This is a hook providing the CurrentUserContext.
430
- *
431
- * @requires CurrentUserProvider
432
- * @example
433
- * import { useCurrentUser } from "@trackunit/react-core-hooks";
434
- * const { assumedUser, accountId } = useCurrentUser();
435
- *
436
- * //use it for something
437
- * const { data, loading } = useGetAccountByIdQuery({
438
- * variables: { accountId: assumedUser?.accountId || accountId || "" },
439
- * });
440
- *
441
- * @see {@link ICurrentUserContext}
442
- */
443
- const useCurrentUser = () => {
444
- const context = React.useContext(CurrentUserContext);
445
- if (!context) {
446
- throw new Error("useCurrentUser must be used within the CurrentUserProvider");
447
- }
448
- return context;
595
+ const CurrentUserContext = React.createContext(null);
596
+ /**
597
+ * This is a provider for the CurrentUserContext.
598
+ */
599
+ const CurrentUserProvider = (props) => {
600
+ return jsx(CurrentUserContext.Provider, Object.assign({}, props));
601
+ };
602
+ /**
603
+ * This is a hook providing the CurrentUserContext.
604
+ *
605
+ * @requires CurrentUserProvider
606
+ * @example
607
+ * import { useCurrentUser } from "@trackunit/react-core-hooks";
608
+ * const { assumedUser, accountId } = useCurrentUser();
609
+ *
610
+ * //use it for something
611
+ * const { data, loading } = useGetAccountByIdQuery({
612
+ * variables: { accountId: assumedUser?.accountId || accountId || "" },
613
+ * });
614
+ *
615
+ * @see {@link ICurrentUserContext}
616
+ */
617
+ const useCurrentUser = () => {
618
+ const context = React.useContext(CurrentUserContext);
619
+ if (!context) {
620
+ throw new Error("useCurrentUser must be used within the CurrentUserProvider");
621
+ }
622
+ return context;
449
623
  };
450
624
 
451
625
  export { AnalyticsContext, AnalyticsContextProvider, AssetSortingProvider, CurrentUserProvider, EnvironmentContextProvider, GlobalSelectionProvider, OemBrandingContextProvider, ToastProvider, TokenProvider, UserSubscriptionProvider, useAnalytics, useAssetRuntime, useAssetSorting, useCurrentUser, useCustomFieldRuntime, useCustomFieldRuntimeForEntity, useEnvironment, useGlobalSelection, useNavigationRuntime, useOemBrandingContext, useRestRuntime, useSiteRuntime, useToast, useToken, useURLSynchronization, useUserSubscription };