@swype-org/react-sdk 0.1.277 → 0.1.281

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
@@ -796,9 +796,9 @@ async function fetchGuestTransferBalances(apiBaseUrl, transferId, guestSessionTo
796
796
  async function fetchGuestAccount(apiBaseUrl, guestToken) {
797
797
  const params = new URLSearchParams({ guestToken });
798
798
  const res = await fetch(`${apiBaseUrl}/v1/accounts?${params.toString()}`);
799
- if (res.status === 404) return null;
800
799
  if (!res.ok) await throwApiError(res);
801
- return await res.json();
800
+ const data = await res.json();
801
+ return data.items[0] ?? null;
802
802
  }
803
803
  async function createGuestAccount(apiBaseUrl, guestSessionToken, providerId, name) {
804
804
  const res = await fetch(`${apiBaseUrl}/v1/accounts`, {
@@ -6341,6 +6341,7 @@ function StepRendererContent({
6341
6341
  const {
6342
6342
  pollingTransfer,
6343
6343
  pollingError,
6344
+ guestAccountPollingError,
6344
6345
  authExecutorError,
6345
6346
  transferSigningSigning,
6346
6347
  transferSigningError,
@@ -6456,7 +6457,7 @@ function StepRendererContent({
6456
6457
  deeplinkUri: state.deeplinkUri ?? "",
6457
6458
  loading: !isDesktop ? state.creatingTransfer || !state.deeplinkUri : state.creatingTransfer,
6458
6459
  useDeeplink: !isDesktop,
6459
- error: state.error || (!isDesktop ? pollingError : authExecutorError),
6460
+ error: state.error || guestAccountPollingError || (!isDesktop ? pollingError : authExecutorError),
6460
6461
  onRetryStatus: !isDesktop ? handlers.onRetryMobileStatus : void 0,
6461
6462
  onBack: !isDesktop ? handlers.onBackFromOpenWallet : void 0,
6462
6463
  onLogout: handlers.onLogout
@@ -9261,10 +9262,27 @@ function useGuestPreauthWalletSetupEffect(deps) {
9261
9262
  dispatch
9262
9263
  ]);
9263
9264
  }
9265
+ function useGuestAccountAutoPollingEffect(deps) {
9266
+ const {
9267
+ mobileFlow,
9268
+ isGuestFlow,
9269
+ guestSessionToken,
9270
+ isPolling,
9271
+ guestAccount,
9272
+ startPolling
9273
+ } = deps;
9274
+ useEffect(() => {
9275
+ if (!mobileFlow || !isGuestFlow || !guestSessionToken) return;
9276
+ if (isPolling || guestAccount) return;
9277
+ startPolling(guestSessionToken);
9278
+ }, [mobileFlow, isGuestFlow, guestSessionToken, isPolling, guestAccount, startPolling]);
9279
+ }
9264
9280
  function useGuestAccountPolling(intervalMs = 3e3) {
9265
9281
  const { apiBaseUrl } = useBlinkConfig();
9266
9282
  const [guestAccount, setGuestAccount] = useState(null);
9267
9283
  const [isPolling, setIsPolling] = useState(false);
9284
+ const [error, setError] = useState(null);
9285
+ const consecutiveFailuresRef = useRef(0);
9268
9286
  const intervalRef = useRef(null);
9269
9287
  const guestTokenRef = useRef(null);
9270
9288
  const sessionIdRef = useRef(null);
@@ -9286,25 +9304,30 @@ function useGuestAccountPolling(intervalMs = 3e3) {
9286
9304
  const poll = useCallback(async () => {
9287
9305
  const token = guestTokenRef.current;
9288
9306
  const sessionId = sessionIdRef.current;
9289
- if (!token || !sessionId) {
9290
- console.info("[blink-sdk] useGuestAccountPolling.poll skipped (no token or sessionId)", {
9291
- hasToken: !!token,
9292
- hasSessionId: !!sessionId
9293
- });
9307
+ if (!token) {
9308
+ console.info("[blink-sdk] useGuestAccountPolling.poll skipped (no token)");
9294
9309
  return;
9295
9310
  }
9296
9311
  console.info("[blink-sdk] useGuestAccountPolling.poll executing", {
9297
9312
  apiBaseUrl: apiBaseUrlRef.current,
9298
9313
  tokenPrefix: token.slice(0, 8),
9299
- sessionId
9314
+ sessionId: sessionId ?? "(none \u2014 account-only mode)"
9300
9315
  });
9301
9316
  try {
9302
9317
  const lookup = await fetchGuestAccount(apiBaseUrlRef.current, token);
9303
9318
  if (!guestTokenRef.current) return;
9319
+ consecutiveFailuresRef.current = 0;
9320
+ setError(null);
9304
9321
  if (lookup == null) {
9305
9322
  console.info("[blink-sdk] useGuestAccountPolling.poll: account not found yet");
9306
9323
  return;
9307
9324
  }
9325
+ if (!sessionId) {
9326
+ console.info("[blink-sdk] useGuestAccountPolling.poll: COMPLETE \u2014 account found (no session check)");
9327
+ setGuestAccount(lookup);
9328
+ stopPolling();
9329
+ return;
9330
+ }
9308
9331
  console.info("[blink-sdk] useGuestAccountPolling.poll: account found, checking session");
9309
9332
  try {
9310
9333
  const session = await fetchAuthorizationSession(apiBaseUrlRef.current, sessionId);
@@ -9313,28 +9336,38 @@ function useGuestAccountPolling(intervalMs = 3e3) {
9313
9336
  console.info("[blink-sdk] useGuestAccountPolling.poll: session not AUTHORIZED yet", session.status);
9314
9337
  return;
9315
9338
  }
9316
- } catch (err) {
9317
- console.info("[blink-sdk] useGuestAccountPolling.poll: session fetch failed", err);
9339
+ } catch (sessionErr) {
9340
+ consecutiveFailuresRef.current += 1;
9341
+ console.info("[blink-sdk] useGuestAccountPolling.poll: session fetch failed", sessionErr);
9342
+ if (consecutiveFailuresRef.current >= 3) {
9343
+ setError(sessionErr instanceof Error ? sessionErr.message : "Failed to check authorization status");
9344
+ }
9318
9345
  return;
9319
9346
  }
9320
9347
  console.info("[blink-sdk] useGuestAccountPolling.poll: COMPLETE \u2014 account + AUTHORIZED");
9321
9348
  setGuestAccount(lookup);
9322
9349
  stopPolling();
9323
9350
  } catch (err) {
9351
+ consecutiveFailuresRef.current += 1;
9324
9352
  console.info("[blink-sdk] useGuestAccountPolling.poll: fetch error, will retry", err);
9353
+ if (consecutiveFailuresRef.current >= 3) {
9354
+ setError(err instanceof Error ? err.message : "Failed to check account status");
9355
+ }
9325
9356
  }
9326
9357
  }, [stopPolling]);
9327
9358
  const startPolling = useCallback(
9328
9359
  (guestToken, sessionId) => {
9329
9360
  console.info("[blink-sdk] useGuestAccountPolling.startPolling called", {
9330
9361
  tokenPrefix: guestToken.slice(0, 8),
9331
- sessionId,
9362
+ sessionId: sessionId ?? "(none \u2014 account-only mode)",
9332
9363
  intervalMs
9333
9364
  });
9334
9365
  stopPolling();
9335
9366
  guestTokenRef.current = guestToken;
9336
- sessionIdRef.current = sessionId;
9367
+ sessionIdRef.current = sessionId ?? null;
9337
9368
  setGuestAccount(null);
9369
+ setError(null);
9370
+ consecutiveFailuresRef.current = 0;
9338
9371
  setIsPolling(true);
9339
9372
  poll();
9340
9373
  intervalRef.current = setInterval(poll, intervalMs);
@@ -9371,7 +9404,7 @@ function useGuestAccountPolling(intervalMs = 3e3) {
9371
9404
  };
9372
9405
  }, [poll]);
9373
9406
  useEffect(() => () => stopPolling(), [stopPolling]);
9374
- return { guestAccount, isPolling, startPolling, stopPolling };
9407
+ return { guestAccount, error, isPolling, startPolling, stopPolling };
9375
9408
  }
9376
9409
  function BlinkPayment(props) {
9377
9410
  const resetKey = useRef(0);
@@ -9564,6 +9597,33 @@ function BlinkPaymentInner({
9564
9597
  setupAccountIdRef: mobileFlowRefs.setupAccountIdRef,
9565
9598
  startGuestAccountPolling: guestAccountPolling.startPolling
9566
9599
  });
9600
+ useGuestAccountAutoPollingEffect({
9601
+ mobileFlow: state.mobileFlow,
9602
+ isGuestFlow: state.isGuestFlow,
9603
+ guestSessionToken: state.guestSessionToken,
9604
+ isPolling: guestAccountPolling.isPolling,
9605
+ guestAccount: guestAccountPolling.guestAccount,
9606
+ startPolling: guestAccountPolling.startPolling
9607
+ });
9608
+ const guestSessionTokenRef = useRef(state.guestSessionToken);
9609
+ guestSessionTokenRef.current = state.guestSessionToken;
9610
+ const guestPreauthSessionIdRef = useRef(state.guestPreauthSessionId);
9611
+ guestPreauthSessionIdRef.current = state.guestPreauthSessionId;
9612
+ useEffect(() => {
9613
+ const handleVisibility = () => {
9614
+ if (document.visibilityState !== "visible") return;
9615
+ const token = guestSessionTokenRef.current;
9616
+ const sessionId = guestPreauthSessionIdRef.current;
9617
+ if (!token) return;
9618
+ if (guestAccountPolling.isPolling || guestAccountPolling.guestAccount) return;
9619
+ console.info("[blink-sdk] warm-return: restarting guest account polling from React state", {
9620
+ sessionId: sessionId ?? "(none \u2014 account-only mode)"
9621
+ });
9622
+ guestAccountPolling.startPolling(token, sessionId ?? void 0);
9623
+ };
9624
+ document.addEventListener("visibilitychange", handleVisibility);
9625
+ return () => document.removeEventListener("visibilitychange", handleVisibility);
9626
+ }, [guestAccountPolling.isPolling, guestAccountPolling.guestAccount, guestAccountPolling.startPolling]);
9567
9627
  useEffect(() => {
9568
9628
  console.info("[blink-sdk] guestPreauthCompletion effect", {
9569
9629
  hasGuestAccount: !!guestAccountPolling.guestAccount,
@@ -9770,6 +9830,7 @@ function BlinkPaymentInner({
9770
9830
  remote: {
9771
9831
  pollingTransfer: polling.transfer,
9772
9832
  pollingError: polling.error,
9833
+ guestAccountPollingError: guestAccountPolling.error,
9773
9834
  authExecutorError: authExecutor.error,
9774
9835
  transferSigningSigning: transferSigning.signing,
9775
9836
  transferSigningError: transferSigning.error,