@trackunit/react-core-hooks 0.2.64 → 0.2.66
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 +387 -387
- package/index.js +387 -387
- package/package.json +5 -5
- package/src/analytics/AnalyticsProvider.d.ts +23 -23
- package/src/assetSorting/AssetSortingProvider.d.ts +36 -36
- package/src/environment/EnvironmentContextProvider.d.ts +23 -23
- package/src/global-selection/GlobalSelectionProvider.d.ts +26 -26
- package/src/index.d.ts +16 -16
- package/src/irisOemApp/IrisOemAppContextProvider.d.ts +24 -24
- package/src/runtimes/navigation/index.d.ts +1 -1
- package/src/runtimes/navigation/useDeeplink.d.ts +4 -4
- package/src/runtimes/navigation/useNavigationRuntime.d.ts +17 -17
- package/src/runtimes/navigation/useURLSynchronization.d.ts +4 -4
- package/src/runtimes/useAssetRuntime.d.ts +22 -22
- package/src/runtimes/useCustomFieldRuntime.d.ts +9 -9
- package/src/runtimes/useCustomFieldRuntimeForEntity.d.ts +23 -23
- package/src/runtimes/useRestRuntime.d.ts +10 -10
- package/src/runtimes/useSiteRuntime.d.ts +23 -23
- package/src/subscription/UserSubscriptionProvider.d.ts +24 -24
- package/src/toast/ToastProvider.d.ts +29 -29
- package/src/token/TokenProvider.d.ts +22 -22
- package/src/user/CurrentUserProvider.d.ts +27 -27
package/index.js
CHANGED
|
@@ -4,187 +4,187 @@ import { jsx } from 'react/jsx-runtime';
|
|
|
4
4
|
import { NavigationRuntime, AssetRuntime, CustomFieldRuntime, RestRuntime, SiteRuntime } from '@trackunit/iris-app-runtime-core';
|
|
5
5
|
import { useLocation } from 'react-router-dom';
|
|
6
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;
|
|
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;
|
|
33
33
|
};
|
|
34
34
|
|
|
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;
|
|
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;
|
|
75
75
|
};
|
|
76
76
|
|
|
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;
|
|
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;
|
|
101
101
|
};
|
|
102
102
|
|
|
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
|
-
*/
|
|
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
|
+
*/
|
|
128
128
|
const GlobalSelectionProvider = (props) => jsx(GlobalSelectionContext.Provider, Object.assign({}, props));
|
|
129
129
|
|
|
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));
|
|
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));
|
|
155
155
|
};
|
|
156
156
|
|
|
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]);
|
|
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]);
|
|
171
171
|
};
|
|
172
172
|
|
|
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;
|
|
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;
|
|
188
188
|
};
|
|
189
189
|
|
|
190
190
|
/******************************************************************************
|
|
@@ -212,240 +212,240 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
212
212
|
});
|
|
213
213
|
}
|
|
214
214
|
|
|
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 };
|
|
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 };
|
|
242
242
|
};
|
|
243
243
|
|
|
244
|
-
/**
|
|
245
|
-
* A hook to expose custom field runtime methods
|
|
246
|
-
*
|
|
247
|
-
* @returns {UseCustomFieldRuntime} CustomFieldRuntime
|
|
248
|
-
*/
|
|
249
|
-
const useCustomFieldRuntime = () => {
|
|
250
|
-
return CustomFieldRuntime;
|
|
244
|
+
/**
|
|
245
|
+
* A hook to expose custom field runtime methods
|
|
246
|
+
*
|
|
247
|
+
* @returns {UseCustomFieldRuntime} CustomFieldRuntime
|
|
248
|
+
*/
|
|
249
|
+
const useCustomFieldRuntime = () => {
|
|
250
|
+
return CustomFieldRuntime;
|
|
251
251
|
};
|
|
252
252
|
|
|
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
|
-
};
|
|
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
|
+
};
|
|
297
297
|
};
|
|
298
298
|
|
|
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;
|
|
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;
|
|
306
306
|
};
|
|
307
307
|
|
|
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 };
|
|
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 };
|
|
336
336
|
};
|
|
337
337
|
|
|
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;
|
|
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;
|
|
363
363
|
};
|
|
364
364
|
|
|
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;
|
|
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;
|
|
394
394
|
};
|
|
395
395
|
|
|
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));
|
|
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));
|
|
419
419
|
};
|
|
420
420
|
|
|
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;
|
|
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;
|
|
449
449
|
};
|
|
450
450
|
|
|
451
451
|
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 };
|