@trackunit/react-core-contexts 0.4.91 → 0.4.92
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 +301 -0
- package/package.json +8 -8
package/index.cjs
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
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 => { 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")); });
|
|
120
|
+
if (invalidToken) {
|
|
121
|
+
window.location.replace(`${publicUrl}logout`);
|
|
122
|
+
}
|
|
123
|
+
if (errorResponse.networkError) {
|
|
124
|
+
// eslint-disable-next-line no-console
|
|
125
|
+
console.error(errorResponse.networkError);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
const defaultOptions = {
|
|
129
|
+
watchQuery: {
|
|
130
|
+
fetchPolicy: "no-cache",
|
|
131
|
+
errorPolicy: "all",
|
|
132
|
+
},
|
|
133
|
+
query: {
|
|
134
|
+
fetchPolicy: "no-cache",
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
const client$1 = new client.ApolloClient({
|
|
138
|
+
name: "Manager",
|
|
139
|
+
connectToDevTools: isDev,
|
|
140
|
+
cache: new client.InMemoryCache(),
|
|
141
|
+
defaultOptions,
|
|
142
|
+
link: client.from([
|
|
143
|
+
errorLink,
|
|
144
|
+
authLink,
|
|
145
|
+
client.split(operation => operation.getContext().clientName === "imageupload", imageUploadGraphQLLink),
|
|
146
|
+
client.split(operation => operation.getContext().clientName === "manager", managerGraphQLLink, client.split(operation => operation.getContext().clientName === "report", reportGraphQLLink, isIrisApp ? publicGraphQLLink : legacyGraphQLLink)),
|
|
147
|
+
]),
|
|
148
|
+
});
|
|
149
|
+
return jsxRuntime.jsx(client.ApolloProvider, Object.assign({ client: client$1 }, { children: children }));
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const ToastProviderApp = ({ children }) => {
|
|
153
|
+
const value = React.useMemo(() => ({ addToast: irisAppRuntimeCore.ToastRuntime.addToast }), []);
|
|
154
|
+
return jsxRuntime.jsx(reactCoreHooks.ToastProvider, Object.assign({ value: value }, { children: children }));
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Subscribe to methods initiated by changes in the host
|
|
159
|
+
*
|
|
160
|
+
* @param methods STABLE object with the methods you'd want to subscribe to. Wrap in useMemo for instance to make it stable
|
|
161
|
+
*/
|
|
162
|
+
const useSubscribeToHostChanges = (methods) => {
|
|
163
|
+
React.useEffect(() => {
|
|
164
|
+
const connection = irisAppRuntimeCore.setupHostConnector(methods);
|
|
165
|
+
return () => {
|
|
166
|
+
connection.destroy();
|
|
167
|
+
};
|
|
168
|
+
}, [methods]);
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const TileAssetSortingProvider = ({ children }) => {
|
|
172
|
+
const [assetSortingState, setAssetSortingState] = React.useState(null);
|
|
173
|
+
React.useEffect(() => {
|
|
174
|
+
irisAppRuntimeCore.AssetSortingRuntime.getAssetSortingState()
|
|
175
|
+
.catch(() => null)
|
|
176
|
+
.then(setAssetSortingState);
|
|
177
|
+
}, []);
|
|
178
|
+
const methods = React.useMemo(() => ({
|
|
179
|
+
onAssetSortingStateChanged: setAssetSortingState,
|
|
180
|
+
}), [setAssetSortingState]);
|
|
181
|
+
useSubscribeToHostChanges(methods);
|
|
182
|
+
const contextValue = React.useMemo(() => ({
|
|
183
|
+
sortingState: assetSortingState,
|
|
184
|
+
setSortBy: irisAppRuntimeCore.AssetSortingRuntime.setAssetSortingState,
|
|
185
|
+
}), [assetSortingState]);
|
|
186
|
+
if (!assetSortingState) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
return jsxRuntime.jsx(reactCoreHooks.AssetSortingProvider, Object.assign({ value: contextValue }, { children: children }));
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const TileEnvironmentProvider = ({ children }) => {
|
|
193
|
+
const [environment, setEnvironment] = React__namespace.useState(null);
|
|
194
|
+
React__namespace.useEffect(() => {
|
|
195
|
+
const updateEnv = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
196
|
+
try {
|
|
197
|
+
setEnvironment(yield irisAppRuntimeCore.EnvironmentRuntime.getEnvironmentContext());
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
setEnvironment(null);
|
|
201
|
+
// eslint-disable-next-line no-console
|
|
202
|
+
console.error("Could not load environment!");
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
updateEnv();
|
|
206
|
+
}, []);
|
|
207
|
+
if (!environment) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
return jsxRuntime.jsx(reactCoreHooks.EnvironmentContextProvider, Object.assign({ value: environment }, { children: children }));
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const TileGlobalSelectionProvider = ({ children }) => {
|
|
214
|
+
const [GlobalSelectionContext, setGlobalSelectionContext] = React.useState(null);
|
|
215
|
+
React.useEffect(() => {
|
|
216
|
+
irisAppRuntimeCore.GlobalSelectionRuntime.getGlobalSelection()
|
|
217
|
+
.catch(() => null)
|
|
218
|
+
.then(setGlobalSelectionContext);
|
|
219
|
+
}, []);
|
|
220
|
+
const methods = React.useMemo(() => ({
|
|
221
|
+
onGlobalSelectionChanged: setGlobalSelectionContext,
|
|
222
|
+
}), [setGlobalSelectionContext]);
|
|
223
|
+
useSubscribeToHostChanges(methods);
|
|
224
|
+
if (!GlobalSelectionContext) {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
return jsxRuntime.jsx(reactCoreHooks.GlobalSelectionProvider, Object.assign({ value: GlobalSelectionContext }, { children: children }));
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const TileTokenProvider = ({ children }) => {
|
|
231
|
+
const [tokenContext, setTokenContext] = React__namespace.useState(null);
|
|
232
|
+
React__namespace.useEffect(() => {
|
|
233
|
+
const updateContext = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
234
|
+
try {
|
|
235
|
+
setTokenContext(yield irisAppRuntimeCore.TokenRuntime.getTokenContext());
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
setTokenContext(null);
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
updateContext();
|
|
242
|
+
}, []);
|
|
243
|
+
const methods = React__namespace.useMemo(() => ({
|
|
244
|
+
onTokenChanged: setTokenContext,
|
|
245
|
+
}), [setTokenContext]);
|
|
246
|
+
useSubscribeToHostChanges(methods);
|
|
247
|
+
if (!tokenContext) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
return jsxRuntime.jsx(reactCoreHooks.TokenProvider, Object.assign({ value: tokenContext }, { children: children }));
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const TileCurrentUserProvider = ({ children }) => {
|
|
254
|
+
const [currentuserContext, setCurrentUserContext] = React__namespace.useState(null);
|
|
255
|
+
React__namespace.useEffect(() => {
|
|
256
|
+
irisAppRuntimeCore.CurrentUserRuntime.getCurrentUserContext()
|
|
257
|
+
.catch(() => null)
|
|
258
|
+
.then(setCurrentUserContext);
|
|
259
|
+
}, []);
|
|
260
|
+
if (!currentuserContext) {
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
return jsxRuntime.jsx(reactCoreHooks.CurrentUserProvider, Object.assign({ value: currentuserContext }, { children: children }));
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const UserSubscriptionProvider = ({ children }) => {
|
|
267
|
+
const [subscription, setSubscription] = React__namespace.useState(null);
|
|
268
|
+
React__namespace.useEffect(() => {
|
|
269
|
+
irisAppRuntimeCore.UserSubscriptionRuntime.getUserSubscriptionContext()
|
|
270
|
+
.catch(() => null)
|
|
271
|
+
.then(setSubscription);
|
|
272
|
+
}, []);
|
|
273
|
+
if (!subscription) {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
return jsxRuntime.jsx(reactCoreHooks.UserSubscriptionProvider, Object.assign({ value: subscription }, { children: children }));
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
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 })) }) }) }) }) }) }));
|
|
280
|
+
|
|
281
|
+
const HostNavigator = ({ children }) => {
|
|
282
|
+
reactCoreHooks.useURLSynchronization();
|
|
283
|
+
// eslint-disable-next-line react/jsx-no-useless-fragment
|
|
284
|
+
return jsxRuntime.jsx(jsxRuntime.Fragment, { children: children });
|
|
285
|
+
};
|
|
286
|
+
const TrackunitRouter = ({ children }) => {
|
|
287
|
+
var _a;
|
|
288
|
+
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 }) })));
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const TrackunitProviders = ({ translations, children }) => {
|
|
292
|
+
if (translations) {
|
|
293
|
+
i18nLibraryTranslation.registerTranslations(translations); // Register the apps translations if passed.
|
|
294
|
+
}
|
|
295
|
+
i18nLibraryTranslation.initializeTranslationsForApp(); // Initialize all registered translations
|
|
296
|
+
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 }) })) }) }));
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
exports.ManagerApolloProvider = ManagerApolloProvider;
|
|
300
|
+
exports.ToastProviderApp = ToastProviderApp;
|
|
301
|
+
exports.TrackunitProviders = TrackunitProviders;
|
package/package.json
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-core-contexts",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.92",
|
|
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.
|
|
9
|
-
"@trackunit/react-core-contexts-api": "0.2.
|
|
10
|
-
"@trackunit/react-core-contexts-test": "0.1.
|
|
11
|
-
"@trackunit/react-core-hooks": "0.2.
|
|
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.
|
|
17
|
-
"@trackunit/react-components": "0.1.
|
|
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.
|
|
21
|
+
"main": "./index.cjs",
|
|
22
22
|
"type": "module",
|
|
23
23
|
"types": "./src/index.d.ts",
|
|
24
24
|
"peerDependencies": {}
|