@trackunit/react-core-contexts 0.4.158 → 0.4.163

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 +54 -29
  2. package/index.js +56 -31
  3. package/package.json +3 -3
package/index.cjs CHANGED
@@ -62,16 +62,8 @@ function __awaiter(thisArg, _arguments, P, generator) {
62
62
  });
63
63
  }
64
64
 
65
- /**
66
- * This is a provider for the ManagerApolloContext.
67
- * It is used to provide the apollo client to the manager app.
68
- *
69
- * @param {IProps} props The props.
70
- * @returns {JSX.Element} The provider.
71
- */
72
- const ManagerApolloProvider = ({ children, isIrisApp }) => {
73
- const { graphqlManagerUrl, graphqlPublicUrl, graphqlManagerImageUploadUrl, graphqlReportUrl, isDev, tracingHeaders } = reactCoreHooks.useEnvironment();
74
- const { token } = reactCoreHooks.useToken();
65
+ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlManagerImageUploadUrl, graphqlReportUrl, isDev, isIrisApp, tracingHeaders, firstToken, }) => {
66
+ let token = firstToken;
75
67
  const managerGraphQLLink = apolloUploadClient.createUploadLink({
76
68
  uri: request => graphqlManagerUrl + "/" + request.operationName,
77
69
  });
@@ -84,12 +76,12 @@ const ManagerApolloProvider = ({ children, isIrisApp }) => {
84
76
  const reportGraphQLLink = apolloUploadClient.createUploadLink({
85
77
  uri: request => graphqlReportUrl + "/" + request.operationName,
86
78
  });
87
- const authLink = React.useMemo(() => context.setContext((_, { headers }) => __awaiter(void 0, void 0, void 0, function* () {
79
+ const authLink = context.setContext((_, { headers }) => __awaiter(void 0, void 0, void 0, function* () {
88
80
  return {
89
81
  headers: Object.assign(Object.assign(Object.assign({}, headers), tracingHeaders), { Authorization: token ? `Bearer ${token}` : null }),
90
82
  };
91
- })), [token, tracingHeaders]);
92
- const errorLink = React.useMemo(() => error.onError((errorResponse) => {
83
+ }));
84
+ const errorLink = error.onError((errorResponse) => {
93
85
  var _a, _b;
94
86
  const traceIds = []; // Used to hold id's for use in Sentry
95
87
  if (errorResponse.graphQLErrors) {
@@ -141,8 +133,8 @@ const ManagerApolloProvider = ({ children, isIrisApp }) => {
141
133
  // eslint-disable-next-line no-console
142
134
  console.error(errorResponse.networkError);
143
135
  }
144
- }), []);
145
- const defaultOptions = React.useMemo(() => ({
136
+ });
137
+ const defaultOptions = {
146
138
  watchQuery: {
147
139
  fetchPolicy: "no-cache",
148
140
  errorPolicy: "all",
@@ -150,9 +142,9 @@ const ManagerApolloProvider = ({ children, isIrisApp }) => {
150
142
  query: {
151
143
  fetchPolicy: "no-cache",
152
144
  },
153
- }), []);
154
- const client$1 = React.useMemo(() => {
155
- return new client.ApolloClient({
145
+ };
146
+ return {
147
+ client: new client.ApolloClient({
156
148
  name: "Manager",
157
149
  connectToDevTools: isDev,
158
150
  cache: new client.InMemoryCache(),
@@ -163,18 +155,51 @@ const ManagerApolloProvider = ({ children, isIrisApp }) => {
163
155
  client.split(operation => operation.getContext().clientName === "imageupload", imageUploadGraphQLLink),
164
156
  client.split(operation => operation.getContext().clientName === "manager", managerGraphQLLink, client.split(operation => operation.getContext().clientName === "report", reportGraphQLLink, isIrisApp ? publicGraphQLLink : managerGraphQLLink)),
165
157
  ]),
158
+ }),
159
+ setToken: (newToken) => {
160
+ token = newToken;
161
+ },
162
+ getToken: () => {
163
+ return token;
164
+ },
165
+ };
166
+ };
167
+ const useApolloClient = (isIrisApp) => {
168
+ var _a;
169
+ const { graphqlManagerUrl, graphqlPublicUrl, graphqlManagerImageUploadUrl, graphqlReportUrl, isDev, tracingHeaders } = reactCoreHooks.useEnvironment();
170
+ const { tasUserId } = reactCoreHooks.useCurrentUser();
171
+ const { token } = reactCoreHooks.useToken();
172
+ const tasUserIdRef = React__namespace.useRef(tasUserId);
173
+ const clientRef = React__namespace.useRef(null);
174
+ /* DONT CHANGE THIS ! its there to ensure we dont recreate apollo client just because the token changes,
175
+ the token will then be set on the instance without react reporting a new client
176
+ and recreating if token change from null to something*/
177
+ if (!clientRef.current || !clientRef.current.getToken() || tasUserIdRef.current !== tasUserId) {
178
+ clientRef.current = createApolloClient({
179
+ graphqlManagerUrl,
180
+ graphqlPublicUrl,
181
+ graphqlManagerImageUploadUrl,
182
+ graphqlReportUrl,
183
+ isDev,
184
+ tracingHeaders,
185
+ isIrisApp,
186
+ firstToken: token,
166
187
  });
167
- }, [
168
- authLink,
169
- defaultOptions,
170
- errorLink,
171
- imageUploadGraphQLLink,
172
- isDev,
173
- isIrisApp,
174
- managerGraphQLLink,
175
- publicGraphQLLink,
176
- reportGraphQLLink,
177
- ]);
188
+ }
189
+ else {
190
+ clientRef.current.setToken(token);
191
+ }
192
+ return (_a = clientRef.current) === null || _a === void 0 ? void 0 : _a.client;
193
+ };
194
+ /**
195
+ * This is a provider for the ManagerApolloContext.
196
+ * It is used to provide the apollo client to the manager app.
197
+ *
198
+ * @param {IProps} props The props.
199
+ * @returns {JSX.Element} The provider.
200
+ */
201
+ const ManagerApolloProvider = ({ children, isIrisApp }) => {
202
+ const client$1 = useApolloClient(isIrisApp);
178
203
  return jsxRuntime.jsx(client.ApolloProvider, Object.assign({ client: client$1 }, { children: children }));
179
204
  };
180
205
 
package/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import { jsx, Fragment } from 'react/jsx-runtime';
2
- import { ApolloClient, InMemoryCache, from, split, ApolloProvider } from '@apollo/client';
2
+ import { ApolloProvider, ApolloClient, InMemoryCache, from, split } from '@apollo/client';
3
3
  import { setContext } from '@apollo/client/link/context';
4
4
  import { onError } from '@apollo/client/link/error';
5
5
  import * as Sentry from '@sentry/browser';
6
- import { useEnvironment, useToken, ToastProvider, AnalyticsContextProvider, AssetSortingProvider, EnvironmentContextProvider, GlobalSelectionProvider, OemBrandingContextProvider as OemBrandingContextProvider$1, TokenProvider, CurrentUserProvider, UserSubscriptionProvider as UserSubscriptionProvider$1, useURLSynchronization } from '@trackunit/react-core-hooks';
6
+ import { useEnvironment, useCurrentUser, useToken, ToastProvider, AnalyticsContextProvider, AssetSortingProvider, EnvironmentContextProvider, GlobalSelectionProvider, OemBrandingContextProvider as OemBrandingContextProvider$1, TokenProvider, CurrentUserProvider, UserSubscriptionProvider as UserSubscriptionProvider$1, useURLSynchronization } from '@trackunit/react-core-hooks';
7
7
  import { createUploadLink } from 'apollo-upload-client';
8
8
  import * as React from 'react';
9
9
  import { useMemo, useEffect, useState } from 'react';
@@ -38,16 +38,8 @@ function __awaiter(thisArg, _arguments, P, generator) {
38
38
  });
39
39
  }
40
40
 
41
- /**
42
- * This is a provider for the ManagerApolloContext.
43
- * It is used to provide the apollo client to the manager app.
44
- *
45
- * @param {IProps} props The props.
46
- * @returns {JSX.Element} The provider.
47
- */
48
- const ManagerApolloProvider = ({ children, isIrisApp }) => {
49
- const { graphqlManagerUrl, graphqlPublicUrl, graphqlManagerImageUploadUrl, graphqlReportUrl, isDev, tracingHeaders } = useEnvironment();
50
- const { token } = useToken();
41
+ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlManagerImageUploadUrl, graphqlReportUrl, isDev, isIrisApp, tracingHeaders, firstToken, }) => {
42
+ let token = firstToken;
51
43
  const managerGraphQLLink = createUploadLink({
52
44
  uri: request => graphqlManagerUrl + "/" + request.operationName,
53
45
  });
@@ -60,12 +52,12 @@ const ManagerApolloProvider = ({ children, isIrisApp }) => {
60
52
  const reportGraphQLLink = createUploadLink({
61
53
  uri: request => graphqlReportUrl + "/" + request.operationName,
62
54
  });
63
- const authLink = useMemo(() => setContext((_, { headers }) => __awaiter(void 0, void 0, void 0, function* () {
55
+ const authLink = setContext((_, { headers }) => __awaiter(void 0, void 0, void 0, function* () {
64
56
  return {
65
57
  headers: Object.assign(Object.assign(Object.assign({}, headers), tracingHeaders), { Authorization: token ? `Bearer ${token}` : null }),
66
58
  };
67
- })), [token, tracingHeaders]);
68
- const errorLink = useMemo(() => onError((errorResponse) => {
59
+ }));
60
+ const errorLink = onError((errorResponse) => {
69
61
  var _a, _b;
70
62
  const traceIds = []; // Used to hold id's for use in Sentry
71
63
  if (errorResponse.graphQLErrors) {
@@ -117,8 +109,8 @@ const ManagerApolloProvider = ({ children, isIrisApp }) => {
117
109
  // eslint-disable-next-line no-console
118
110
  console.error(errorResponse.networkError);
119
111
  }
120
- }), []);
121
- const defaultOptions = useMemo(() => ({
112
+ });
113
+ const defaultOptions = {
122
114
  watchQuery: {
123
115
  fetchPolicy: "no-cache",
124
116
  errorPolicy: "all",
@@ -126,9 +118,9 @@ const ManagerApolloProvider = ({ children, isIrisApp }) => {
126
118
  query: {
127
119
  fetchPolicy: "no-cache",
128
120
  },
129
- }), []);
130
- const client = useMemo(() => {
131
- return new ApolloClient({
121
+ };
122
+ return {
123
+ client: new ApolloClient({
132
124
  name: "Manager",
133
125
  connectToDevTools: isDev,
134
126
  cache: new InMemoryCache(),
@@ -139,18 +131,51 @@ const ManagerApolloProvider = ({ children, isIrisApp }) => {
139
131
  split(operation => operation.getContext().clientName === "imageupload", imageUploadGraphQLLink),
140
132
  split(operation => operation.getContext().clientName === "manager", managerGraphQLLink, split(operation => operation.getContext().clientName === "report", reportGraphQLLink, isIrisApp ? publicGraphQLLink : managerGraphQLLink)),
141
133
  ]),
134
+ }),
135
+ setToken: (newToken) => {
136
+ token = newToken;
137
+ },
138
+ getToken: () => {
139
+ return token;
140
+ },
141
+ };
142
+ };
143
+ const useApolloClient = (isIrisApp) => {
144
+ var _a;
145
+ const { graphqlManagerUrl, graphqlPublicUrl, graphqlManagerImageUploadUrl, graphqlReportUrl, isDev, tracingHeaders } = useEnvironment();
146
+ const { tasUserId } = useCurrentUser();
147
+ const { token } = useToken();
148
+ const tasUserIdRef = React.useRef(tasUserId);
149
+ const clientRef = React.useRef(null);
150
+ /* DONT CHANGE THIS ! its there to ensure we dont recreate apollo client just because the token changes,
151
+ the token will then be set on the instance without react reporting a new client
152
+ and recreating if token change from null to something*/
153
+ if (!clientRef.current || !clientRef.current.getToken() || tasUserIdRef.current !== tasUserId) {
154
+ clientRef.current = createApolloClient({
155
+ graphqlManagerUrl,
156
+ graphqlPublicUrl,
157
+ graphqlManagerImageUploadUrl,
158
+ graphqlReportUrl,
159
+ isDev,
160
+ tracingHeaders,
161
+ isIrisApp,
162
+ firstToken: token,
142
163
  });
143
- }, [
144
- authLink,
145
- defaultOptions,
146
- errorLink,
147
- imageUploadGraphQLLink,
148
- isDev,
149
- isIrisApp,
150
- managerGraphQLLink,
151
- publicGraphQLLink,
152
- reportGraphQLLink,
153
- ]);
164
+ }
165
+ else {
166
+ clientRef.current.setToken(token);
167
+ }
168
+ return (_a = clientRef.current) === null || _a === void 0 ? void 0 : _a.client;
169
+ };
170
+ /**
171
+ * This is a provider for the ManagerApolloContext.
172
+ * It is used to provide the apollo client to the manager app.
173
+ *
174
+ * @param {IProps} props The props.
175
+ * @returns {JSX.Element} The provider.
176
+ */
177
+ const ManagerApolloProvider = ({ children, isIrisApp }) => {
178
+ const client = useApolloClient(isIrisApp);
154
179
  return jsx(ApolloProvider, Object.assign({ client: client }, { children: children }));
155
180
  };
156
181
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-contexts",
3
- "version": "0.4.158",
3
+ "version": "0.4.163",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "dependencies": {
@@ -10,9 +10,9 @@
10
10
  "@sentry/browser": "7.50.0",
11
11
  "@trackunit/i18n-library-translation": "0.0.53",
12
12
  "@trackunit/iris-app-runtime-core": "0.3.46",
13
- "@trackunit/react-components": "0.1.97",
13
+ "@trackunit/react-components": "0.1.99",
14
14
  "@trackunit/react-core-contexts-api": "0.2.37",
15
- "@trackunit/react-core-contexts-test": "0.1.68",
15
+ "@trackunit/react-core-contexts-test": "0.1.69",
16
16
  "@trackunit/react-core-hooks": "0.2.60",
17
17
  "apollo-upload-client": "17.0.0",
18
18
  "react": "18.2.0",