@trackunit/react-core-contexts 0.4.91 → 0.4.93

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.
Files changed (3) hide show
  1. package/index.cjs +314 -0
  2. package/index.js +14 -1
  3. package/package.json +8 -8
package/index.cjs ADDED
@@ -0,0 +1,314 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var jsxRuntime = require('react/jsx-runtime');
6
+ var client = require('@apollo/client');
7
+ var context = require('@apollo/client/link/context');
8
+ var error = require('@apollo/client/link/error');
9
+ var Sentry = require('@sentry/browser');
10
+ var reactCoreHooks = require('@trackunit/react-core-hooks');
11
+ var apolloUploadClient = require('apollo-upload-client');
12
+ var irisAppRuntimeCore = require('@trackunit/iris-app-runtime-core');
13
+ var React = require('react');
14
+ var i18nLibraryTranslation = require('@trackunit/i18n-library-translation');
15
+ var reactComponents = require('@trackunit/react-components');
16
+ var reactRouterDom = require('react-router-dom');
17
+
18
+ function _interopNamespace(e) {
19
+ if (e && e.__esModule) return e;
20
+ var n = Object.create(null);
21
+ if (e) {
22
+ Object.keys(e).forEach(function (k) {
23
+ if (k !== 'default') {
24
+ var d = Object.getOwnPropertyDescriptor(e, k);
25
+ Object.defineProperty(n, k, d.get ? d : {
26
+ enumerable: true,
27
+ get: function () { return e[k]; }
28
+ });
29
+ }
30
+ });
31
+ }
32
+ n["default"] = e;
33
+ return Object.freeze(n);
34
+ }
35
+
36
+ var Sentry__namespace = /*#__PURE__*/_interopNamespace(Sentry);
37
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
38
+
39
+ /******************************************************************************
40
+ Copyright (c) Microsoft Corporation.
41
+
42
+ Permission to use, copy, modify, and/or distribute this software for any
43
+ purpose with or without fee is hereby granted.
44
+
45
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
46
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
47
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
48
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
49
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
50
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
51
+ PERFORMANCE OF THIS SOFTWARE.
52
+ ***************************************************************************** */
53
+
54
+ function __awaiter(thisArg, _arguments, P, generator) {
55
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
56
+ return new (P || (P = Promise))(function (resolve, reject) {
57
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
58
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
59
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
60
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
61
+ });
62
+ }
63
+
64
+ const ManagerApolloProvider = ({ children, isIrisApp }) => {
65
+ const { publicUrl, graphqlLegacyUrl, graphqlManagerUrl, graphqlPublicUrl, graphqlManagerImageUploadUrl, graphqlReportUrl, isDev, tracingHeaders, } = reactCoreHooks.useEnvironment();
66
+ const { token } = reactCoreHooks.useToken();
67
+ const legacyGraphQLLink = apolloUploadClient.createUploadLink({
68
+ uri: request => graphqlLegacyUrl + "/" + request.operationName,
69
+ });
70
+ const managerGraphQLLink = apolloUploadClient.createUploadLink({
71
+ uri: request => graphqlManagerUrl + "/" + request.operationName,
72
+ });
73
+ const publicGraphQLLink = apolloUploadClient.createUploadLink({
74
+ uri: request => graphqlPublicUrl + "/" + request.operationName,
75
+ });
76
+ const imageUploadGraphQLLink = apolloUploadClient.createUploadLink({
77
+ uri: request => graphqlManagerImageUploadUrl + "/" + request.operationName,
78
+ });
79
+ const reportGraphQLLink = apolloUploadClient.createUploadLink({
80
+ uri: request => graphqlReportUrl + "/" + request.operationName,
81
+ });
82
+ const authLink = context.setContext((_, { headers }) => __awaiter(void 0, void 0, void 0, function* () {
83
+ return {
84
+ headers: Object.assign(Object.assign(Object.assign({}, headers), tracingHeaders), { Authorization: token ? `Bearer ${token}` : null }),
85
+ };
86
+ }));
87
+ const errorLink = error.onError((errorResponse) => {
88
+ var _a, _b;
89
+ const traceIds = []; // Used to hold id's for use in Sentry
90
+ if (errorResponse.graphQLErrors) {
91
+ // eslint-disable-next-line no-console
92
+ console.error("Error Fetching Data", errorResponse.graphQLErrors);
93
+ (_a = errorResponse.graphQLErrors) === null || _a === void 0 ? void 0 : _a.forEach(error => {
94
+ var _a, _b;
95
+ if (error === null || error === void 0 ? void 0 : error.extensions) {
96
+ error.extensions.traceId = (_b = (_a = errorResponse.response) === null || _a === void 0 ? void 0 : _a.extensions) === null || _b === void 0 ? void 0 : _b.traceId;
97
+ traceIds.push(error.extensions.traceId);
98
+ }
99
+ });
100
+ /**
101
+ * We want to see the full graphQL error since
102
+ * it contains extra details like the query/mutation
103
+ * name.
104
+ */
105
+ if (Sentry__namespace) {
106
+ if (traceIds.length) {
107
+ Sentry__namespace.setTag("traceIds", traceIds.join(", "));
108
+ }
109
+ Sentry__namespace.addBreadcrumb({
110
+ category: "GraphQL",
111
+ message: "GraphQL Error",
112
+ level: "error",
113
+ data: {
114
+ log: JSON.stringify(errorResponse.graphQLErrors),
115
+ },
116
+ });
117
+ }
118
+ }
119
+ const invalidToken = (_b = errorResponse.graphQLErrors) === null || _b === void 0 ? void 0 : _b.some(x => {
120
+ var _a, _b, _c;
121
+ return ((_a = x === null || x === void 0 ? void 0 : x.extensions) === null || _a === void 0 ? void 0 : _a.code) === "UNAUTHENTICATED" ||
122
+ ((_b = x === null || x === void 0 ? void 0 : x.message) === null || _b === void 0 ? void 0 : _b.includes("Invalid token specified")) ||
123
+ ((_c = x === null || x === void 0 ? void 0 : x.message) === null || _c === void 0 ? void 0 : _c.includes("Access denied! You need to be authorized to perform this action!"));
124
+ });
125
+ if (invalidToken) {
126
+ Sentry__namespace.captureException({
127
+ category: "GraphQL",
128
+ message: "GraphQL Error - invalidToken",
129
+ level: "warning",
130
+ data: {
131
+ log: JSON.stringify(errorResponse.graphQLErrors),
132
+ },
133
+ });
134
+ window.location.replace(`${publicUrl}logout`);
135
+ }
136
+ if (errorResponse.networkError) {
137
+ // eslint-disable-next-line no-console
138
+ console.error(errorResponse.networkError);
139
+ }
140
+ });
141
+ const defaultOptions = {
142
+ watchQuery: {
143
+ fetchPolicy: "no-cache",
144
+ errorPolicy: "all",
145
+ },
146
+ query: {
147
+ fetchPolicy: "no-cache",
148
+ },
149
+ };
150
+ const client$1 = new client.ApolloClient({
151
+ name: "Manager",
152
+ connectToDevTools: isDev,
153
+ cache: new client.InMemoryCache(),
154
+ defaultOptions,
155
+ link: client.from([
156
+ errorLink,
157
+ authLink,
158
+ client.split(operation => operation.getContext().clientName === "imageupload", imageUploadGraphQLLink),
159
+ client.split(operation => operation.getContext().clientName === "manager", managerGraphQLLink, client.split(operation => operation.getContext().clientName === "report", reportGraphQLLink, isIrisApp ? publicGraphQLLink : legacyGraphQLLink)),
160
+ ]),
161
+ });
162
+ return jsxRuntime.jsx(client.ApolloProvider, Object.assign({ client: client$1 }, { children: children }));
163
+ };
164
+
165
+ const ToastProviderApp = ({ children }) => {
166
+ const value = React.useMemo(() => ({ addToast: irisAppRuntimeCore.ToastRuntime.addToast }), []);
167
+ return jsxRuntime.jsx(reactCoreHooks.ToastProvider, Object.assign({ value: value }, { children: children }));
168
+ };
169
+
170
+ /**
171
+ * Subscribe to methods initiated by changes in the host
172
+ *
173
+ * @param methods STABLE object with the methods you'd want to subscribe to. Wrap in useMemo for instance to make it stable
174
+ */
175
+ const useSubscribeToHostChanges = (methods) => {
176
+ React.useEffect(() => {
177
+ const connection = irisAppRuntimeCore.setupHostConnector(methods);
178
+ return () => {
179
+ connection.destroy();
180
+ };
181
+ }, [methods]);
182
+ };
183
+
184
+ const TileAssetSortingProvider = ({ children }) => {
185
+ const [assetSortingState, setAssetSortingState] = React.useState(null);
186
+ React.useEffect(() => {
187
+ irisAppRuntimeCore.AssetSortingRuntime.getAssetSortingState()
188
+ .catch(() => null)
189
+ .then(setAssetSortingState);
190
+ }, []);
191
+ const methods = React.useMemo(() => ({
192
+ onAssetSortingStateChanged: setAssetSortingState,
193
+ }), [setAssetSortingState]);
194
+ useSubscribeToHostChanges(methods);
195
+ const contextValue = React.useMemo(() => ({
196
+ sortingState: assetSortingState,
197
+ setSortBy: irisAppRuntimeCore.AssetSortingRuntime.setAssetSortingState,
198
+ }), [assetSortingState]);
199
+ if (!assetSortingState) {
200
+ return null;
201
+ }
202
+ return jsxRuntime.jsx(reactCoreHooks.AssetSortingProvider, Object.assign({ value: contextValue }, { children: children }));
203
+ };
204
+
205
+ const TileEnvironmentProvider = ({ children }) => {
206
+ const [environment, setEnvironment] = React__namespace.useState(null);
207
+ React__namespace.useEffect(() => {
208
+ const updateEnv = () => __awaiter(void 0, void 0, void 0, function* () {
209
+ try {
210
+ setEnvironment(yield irisAppRuntimeCore.EnvironmentRuntime.getEnvironmentContext());
211
+ }
212
+ catch (error) {
213
+ setEnvironment(null);
214
+ // eslint-disable-next-line no-console
215
+ console.error("Could not load environment!");
216
+ }
217
+ });
218
+ updateEnv();
219
+ }, []);
220
+ if (!environment) {
221
+ return null;
222
+ }
223
+ return jsxRuntime.jsx(reactCoreHooks.EnvironmentContextProvider, Object.assign({ value: environment }, { children: children }));
224
+ };
225
+
226
+ const TileGlobalSelectionProvider = ({ children }) => {
227
+ const [GlobalSelectionContext, setGlobalSelectionContext] = React.useState(null);
228
+ React.useEffect(() => {
229
+ irisAppRuntimeCore.GlobalSelectionRuntime.getGlobalSelection()
230
+ .catch(() => null)
231
+ .then(setGlobalSelectionContext);
232
+ }, []);
233
+ const methods = React.useMemo(() => ({
234
+ onGlobalSelectionChanged: setGlobalSelectionContext,
235
+ }), [setGlobalSelectionContext]);
236
+ useSubscribeToHostChanges(methods);
237
+ if (!GlobalSelectionContext) {
238
+ return null;
239
+ }
240
+ return jsxRuntime.jsx(reactCoreHooks.GlobalSelectionProvider, Object.assign({ value: GlobalSelectionContext }, { children: children }));
241
+ };
242
+
243
+ const TileTokenProvider = ({ children }) => {
244
+ const [tokenContext, setTokenContext] = React__namespace.useState(null);
245
+ React__namespace.useEffect(() => {
246
+ const updateContext = () => __awaiter(void 0, void 0, void 0, function* () {
247
+ try {
248
+ setTokenContext(yield irisAppRuntimeCore.TokenRuntime.getTokenContext());
249
+ }
250
+ catch (error) {
251
+ setTokenContext(null);
252
+ }
253
+ });
254
+ updateContext();
255
+ }, []);
256
+ const methods = React__namespace.useMemo(() => ({
257
+ onTokenChanged: setTokenContext,
258
+ }), [setTokenContext]);
259
+ useSubscribeToHostChanges(methods);
260
+ if (!tokenContext) {
261
+ return null;
262
+ }
263
+ return jsxRuntime.jsx(reactCoreHooks.TokenProvider, Object.assign({ value: tokenContext }, { children: children }));
264
+ };
265
+
266
+ const TileCurrentUserProvider = ({ children }) => {
267
+ const [currentuserContext, setCurrentUserContext] = React__namespace.useState(null);
268
+ React__namespace.useEffect(() => {
269
+ irisAppRuntimeCore.CurrentUserRuntime.getCurrentUserContext()
270
+ .catch(() => null)
271
+ .then(setCurrentUserContext);
272
+ }, []);
273
+ if (!currentuserContext) {
274
+ return null;
275
+ }
276
+ return jsxRuntime.jsx(reactCoreHooks.CurrentUserProvider, Object.assign({ value: currentuserContext }, { children: children }));
277
+ };
278
+
279
+ const UserSubscriptionProvider = ({ children }) => {
280
+ const [subscription, setSubscription] = React__namespace.useState(null);
281
+ React__namespace.useEffect(() => {
282
+ irisAppRuntimeCore.UserSubscriptionRuntime.getUserSubscriptionContext()
283
+ .catch(() => null)
284
+ .then(setSubscription);
285
+ }, []);
286
+ if (!subscription) {
287
+ return null;
288
+ }
289
+ return jsxRuntime.jsx(reactCoreHooks.UserSubscriptionProvider, Object.assign({ value: subscription }, { children: children }));
290
+ };
291
+
292
+ const TrackunitDataProviders = ({ children }) => (jsxRuntime.jsx(TileEnvironmentProvider, { children: jsxRuntime.jsx(TileTokenProvider, { children: jsxRuntime.jsx(TileCurrentUserProvider, { children: jsxRuntime.jsx(UserSubscriptionProvider, { children: jsxRuntime.jsx(TileGlobalSelectionProvider, { children: jsxRuntime.jsx(TileAssetSortingProvider, { children: jsxRuntime.jsx(ManagerApolloProvider, Object.assign({ isIrisApp: true }, { children: children })) }) }) }) }) }) }));
293
+
294
+ const HostNavigator = ({ children }) => {
295
+ reactCoreHooks.useURLSynchronization();
296
+ // eslint-disable-next-line react/jsx-no-useless-fragment
297
+ return jsxRuntime.jsx(jsxRuntime.Fragment, { children: children });
298
+ };
299
+ const TrackunitRouter = ({ children }) => {
300
+ var _a;
301
+ return (jsxRuntime.jsx(reactRouterDom.BrowserRouter, Object.assign({ basename: `${((_a = document.getElementById("baseHref")) === null || _a === void 0 ? void 0 : _a.getAttribute("href")) || "<missing baseHref>"}${global.module}` }, { children: jsxRuntime.jsx(HostNavigator, { children: children }) })));
302
+ };
303
+
304
+ const TrackunitProviders = ({ translations, children }) => {
305
+ if (translations) {
306
+ i18nLibraryTranslation.registerTranslations(translations); // Register the apps translations if passed.
307
+ }
308
+ i18nLibraryTranslation.initializeTranslationsForApp(); // Initialize all registered translations
309
+ return (jsxRuntime.jsx(TrackunitDataProviders, { children: jsxRuntime.jsx(ToastProviderApp, { children: jsxRuntime.jsx(React__namespace.Suspense, Object.assign({ fallback: jsxRuntime.jsx(reactComponents.Spinner, { centering: "centered" }) }, { children: jsxRuntime.jsx(TrackunitRouter, { children: children }) })) }) }));
310
+ };
311
+
312
+ exports.ManagerApolloProvider = ManagerApolloProvider;
313
+ exports.ToastProviderApp = ToastProviderApp;
314
+ exports.TrackunitProviders = TrackunitProviders;
package/index.js CHANGED
@@ -92,8 +92,21 @@ const ManagerApolloProvider = ({ children, isIrisApp }) => {
92
92
  });
93
93
  }
94
94
  }
95
- const invalidToken = (_b = errorResponse.graphQLErrors) === null || _b === void 0 ? void 0 : _b.some(x => { var _a, _b; return ((_a = x === null || x === void 0 ? void 0 : x.extensions) === null || _a === void 0 ? void 0 : _a.code) === "UNAUTHENTICATED" || ((_b = x === null || x === void 0 ? void 0 : x.message) === null || _b === void 0 ? void 0 : _b.includes("Invalid token specified")); });
95
+ const invalidToken = (_b = errorResponse.graphQLErrors) === null || _b === void 0 ? void 0 : _b.some(x => {
96
+ var _a, _b, _c;
97
+ return ((_a = x === null || x === void 0 ? void 0 : x.extensions) === null || _a === void 0 ? void 0 : _a.code) === "UNAUTHENTICATED" ||
98
+ ((_b = x === null || x === void 0 ? void 0 : x.message) === null || _b === void 0 ? void 0 : _b.includes("Invalid token specified")) ||
99
+ ((_c = x === null || x === void 0 ? void 0 : x.message) === null || _c === void 0 ? void 0 : _c.includes("Access denied! You need to be authorized to perform this action!"));
100
+ });
96
101
  if (invalidToken) {
102
+ Sentry.captureException({
103
+ category: "GraphQL",
104
+ message: "GraphQL Error - invalidToken",
105
+ level: "warning",
106
+ data: {
107
+ log: JSON.stringify(errorResponse.graphQLErrors),
108
+ },
109
+ });
97
110
  window.location.replace(`${publicUrl}logout`);
98
111
  }
99
112
  if (errorResponse.networkError) {
package/package.json CHANGED
@@ -1,24 +1,24 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-contexts",
3
- "version": "0.4.91",
3
+ "version": "0.4.93",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "dependencies": {
7
7
  "graphql": "^15.8.0",
8
- "@trackunit/iris-app-runtime-core": "0.3.22",
9
- "@trackunit/react-core-contexts-api": "0.2.19",
10
- "@trackunit/react-core-contexts-test": "0.1.34",
11
- "@trackunit/react-core-hooks": "0.2.32",
8
+ "@trackunit/iris-app-runtime-core": "0.3.23",
9
+ "@trackunit/react-core-contexts-api": "0.2.20",
10
+ "@trackunit/react-core-contexts-test": "0.1.35",
11
+ "@trackunit/react-core-hooks": "0.2.33",
12
12
  "react": "18.2.0",
13
13
  "@apollo/client": "3.7.10",
14
14
  "@sentry/browser": "7.24.2",
15
15
  "apollo-upload-client": "17.0.0",
16
- "@trackunit/i18n-library-translation": "0.0.31",
17
- "@trackunit/react-components": "0.1.65",
16
+ "@trackunit/i18n-library-translation": "0.0.32",
17
+ "@trackunit/react-components": "0.1.66",
18
18
  "react-router-dom": "6.4.5"
19
19
  },
20
20
  "module": "./index.js",
21
- "main": "./index.js",
21
+ "main": "./index.cjs",
22
22
  "type": "module",
23
23
  "types": "./src/index.d.ts",
24
24
  "peerDependencies": {}