@merit-systems/echo-react-sdk 1.0.9 → 1.0.10

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/dist/index.js CHANGED
@@ -3,7 +3,7 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
  var _a;
5
5
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
6
- import React2, { useState, useCallback, useEffect, createContext, useContext, useRef } from "react";
6
+ import React2, { useState, useCallback, useEffect, createContext, useMemo, useContext, useRef } from "react";
7
7
  class InvalidTokenError extends Error {
8
8
  }
9
9
  InvalidTokenError.prototype.name = "InvalidTokenError";
@@ -9067,19 +9067,18 @@ function useEchoBalance(echoClient, appId) {
9067
9067
  };
9068
9068
  }
9069
9069
  function useEchoClient({ apiUrl }) {
9070
- var _a2;
9070
+ var _a2, _b;
9071
9071
  const auth = useAuth();
9072
9072
  const [client, setClient] = useState(null);
9073
9073
  useEffect(() => {
9074
- var _a3;
9075
- if (!((_a3 = auth.user) == null ? void 0 : _a3.access_token)) {
9074
+ if (!auth.isAuthenticated || !auth.user) {
9076
9075
  setClient(null);
9077
9076
  return;
9078
9077
  }
9079
9078
  const tokenProvider = new OAuthTokenProvider({
9080
9079
  getTokenFn: () => {
9081
- var _a4;
9082
- return Promise.resolve(((_a4 = auth.user) == null ? void 0 : _a4.access_token) || null);
9080
+ var _a3;
9081
+ return Promise.resolve(((_a3 = auth.user) == null ? void 0 : _a3.access_token) || null);
9083
9082
  },
9084
9083
  refreshTokenFn: async () => {
9085
9084
  await auth.signinSilent();
@@ -9097,7 +9096,14 @@ function useEchoClient({ apiUrl }) {
9097
9096
  return () => {
9098
9097
  setClient(null);
9099
9098
  };
9100
- }, [apiUrl, (_a2 = auth.user) == null ? void 0 : _a2.access_token, auth.signinSilent, auth.signoutSilent]);
9099
+ }, [
9100
+ apiUrl,
9101
+ auth.isAuthenticated,
9102
+ (_b = (_a2 = auth.user) == null ? void 0 : _a2.profile) == null ? void 0 : _b.sub,
9103
+ // Only recreate if user ID changes
9104
+ auth.signinSilent,
9105
+ auth.signoutSilent
9106
+ ]);
9101
9107
  return client;
9102
9108
  }
9103
9109
  function useEchoPayments(echoClient) {
@@ -9135,6 +9141,9 @@ function useEchoPayments(echoClient) {
9135
9141
  };
9136
9142
  }
9137
9143
  const EchoContext = createContext(null);
9144
+ const EchoRefreshContext = createContext(
9145
+ null
9146
+ );
9138
9147
  function EchoProviderInternal({ config, children }) {
9139
9148
  var _a2, _b, _c;
9140
9149
  const auth = useAuth();
@@ -9167,25 +9176,51 @@ function EchoProviderInternal({ config, children }) {
9167
9176
  return ((_a3 = auth.user) == null ? void 0 : _a3.access_token) || null;
9168
9177
  }, [(_b = auth.user) == null ? void 0 : _b.access_token]);
9169
9178
  const combinedError = ((_c = auth.error) == null ? void 0 : _c.message) || balanceError || paymentError || null;
9170
- const combinedLoading = auth.isLoading || balanceLoading || paymentLoading;
9171
- const contextValue = {
9172
- user: echoUser || null,
9173
- rawUser: user,
9174
- balance,
9175
- freeTierBalance,
9176
- isAuthenticated: auth.isAuthenticated,
9177
- isLoading: combinedLoading,
9178
- error: combinedError,
9179
- token,
9180
- echoClient,
9181
- signIn: auth.signinRedirect,
9182
- signOut: clearAuth,
9183
- refreshBalance,
9184
- createPaymentLink,
9185
- getToken,
9186
- clearAuth
9187
- };
9188
- return /* @__PURE__ */ jsx(EchoContext.Provider, { value: contextValue, children });
9179
+ const isInitialAuthLoading = auth.isLoading && !auth.isAuthenticated;
9180
+ const isTokenRefreshing = auth.isLoading && auth.isAuthenticated;
9181
+ const combinedLoading = isInitialAuthLoading || balanceLoading || paymentLoading;
9182
+ const contextValue = useMemo(
9183
+ () => ({
9184
+ user: echoUser || null,
9185
+ rawUser: user,
9186
+ balance,
9187
+ freeTierBalance,
9188
+ isAuthenticated: auth.isAuthenticated,
9189
+ isLoading: combinedLoading,
9190
+ error: combinedError,
9191
+ token,
9192
+ echoClient,
9193
+ signIn: auth.signinRedirect,
9194
+ signOut: clearAuth,
9195
+ refreshBalance,
9196
+ createPaymentLink,
9197
+ getToken,
9198
+ clearAuth
9199
+ }),
9200
+ [
9201
+ echoUser,
9202
+ user,
9203
+ balance,
9204
+ freeTierBalance,
9205
+ auth.isAuthenticated,
9206
+ combinedLoading,
9207
+ combinedError,
9208
+ token,
9209
+ echoClient,
9210
+ auth.signinRedirect,
9211
+ clearAuth,
9212
+ refreshBalance,
9213
+ createPaymentLink,
9214
+ getToken
9215
+ ]
9216
+ );
9217
+ const refreshContextValue = useMemo(
9218
+ () => ({
9219
+ isRefreshing: isTokenRefreshing
9220
+ }),
9221
+ [isTokenRefreshing]
9222
+ );
9223
+ return /* @__PURE__ */ jsx(EchoContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(EchoRefreshContext.Provider, { value: refreshContextValue, children }) });
9189
9224
  }
9190
9225
  function EchoProvider({ config, children }) {
9191
9226
  const [isClient, setIsClient] = useState(false);