@swype-org/react-sdk 0.1.155 → 0.1.157

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
@@ -3429,6 +3429,7 @@ function WalletPickerScreen({
3429
3429
  providers,
3430
3430
  pendingConnections,
3431
3431
  loading,
3432
+ onPrepareProvider,
3432
3433
  onSelectProvider,
3433
3434
  onContinueConnection,
3434
3435
  onBack
@@ -3437,6 +3438,27 @@ function WalletPickerScreen({
3437
3438
  const [hoveredId, setHoveredId] = useState(null);
3438
3439
  const [selectedProviderId, setSelectedProviderId] = useState(null);
3439
3440
  const [cryptoExpanded, setCryptoExpanded] = useState(false);
3441
+ const [connecting, setConnecting] = useState(false);
3442
+ const [preparedSession, setPreparedSession] = useState(null);
3443
+ const [preparing, setPreparing] = useState(false);
3444
+ const prepareIdRef = useRef(0);
3445
+ const handleCardClick = useCallback(async (providerId) => {
3446
+ setSelectedProviderId(providerId);
3447
+ setPreparedSession(null);
3448
+ setPreparing(true);
3449
+ const id = ++prepareIdRef.current;
3450
+ try {
3451
+ const session = await onPrepareProvider(providerId);
3452
+ if (prepareIdRef.current !== id) return;
3453
+ if (session) {
3454
+ setPreparedSession({ ...session, providerId });
3455
+ }
3456
+ } finally {
3457
+ if (prepareIdRef.current === id) {
3458
+ setPreparing(false);
3459
+ }
3460
+ }
3461
+ }, [onPrepareProvider]);
3440
3462
  const hasPending = pendingConnections != null && pendingConnections.length > 0;
3441
3463
  const displayProviders = providers.length > 0 ? providers : [
3442
3464
  { id: "metamask", name: "MetaMask" },
@@ -3455,11 +3477,23 @@ function WalletPickerScreen({
3455
3477
  ScreenLayout,
3456
3478
  {
3457
3479
  footer: /* @__PURE__ */ jsxs(Fragment, { children: [
3458
- selectedProvider && /* @__PURE__ */ jsxs(PrimaryButton, { onClick: () => onSelectProvider(selectedProvider.id), children: [
3459
- "Open ",
3460
- selectedProvider.name,
3461
- " to Link"
3462
- ] }),
3480
+ selectedProvider && /* @__PURE__ */ jsx(
3481
+ PrimaryButton,
3482
+ {
3483
+ onClick: async () => {
3484
+ setConnecting(true);
3485
+ try {
3486
+ const session = preparedSession?.providerId === selectedProvider.id ? preparedSession : void 0;
3487
+ await onSelectProvider(selectedProvider.id, session);
3488
+ } finally {
3489
+ setConnecting(false);
3490
+ }
3491
+ },
3492
+ loading: connecting || preparing,
3493
+ disabled: preparing,
3494
+ children: preparing ? "Preparing..." : `Open ${selectedProvider.name} to Link`
3495
+ }
3496
+ ),
3463
3497
  /* @__PURE__ */ jsx(PoweredByFooter, {})
3464
3498
  ] }),
3465
3499
  children: [
@@ -3536,7 +3570,7 @@ function WalletPickerScreen({
3536
3570
  return /* @__PURE__ */ jsxs(
3537
3571
  "button",
3538
3572
  {
3539
- onClick: () => setSelectedProviderId(p.id),
3573
+ onClick: () => handleCardClick(p.id),
3540
3574
  onMouseEnter: () => setHoveredId(p.id),
3541
3575
  onMouseLeave: () => setHoveredId(null),
3542
3576
  style: cardStyle(tokens, isHovered, isSelected),
@@ -5557,6 +5591,7 @@ function StepRenderer({
5557
5591
  providers: state.providers,
5558
5592
  pendingConnections,
5559
5593
  loading: state.creatingTransfer,
5594
+ onPrepareProvider: handlers.onPrepareProvider,
5560
5595
  onSelectProvider: handlers.onSelectProvider,
5561
5596
  onContinueConnection: handlers.onContinueConnection,
5562
5597
  onBack: () => handlers.onNavigate(state.activeCredentialId ? "deposit" : "create-passkey")
@@ -6503,25 +6538,14 @@ function useProviderHandlers(deps) {
6503
6538
  reauthSessionIdRef,
6504
6539
  reauthTokenRef
6505
6540
  } = deps;
6506
- const handleSelectProvider = useCallback(async (providerId) => {
6507
- dispatch({ type: "SELECT_PROVIDER", providerId });
6541
+ const handlePrepareProvider = useCallback(async (providerId) => {
6508
6542
  if (!activeCredentialId) {
6509
6543
  dispatch({ type: "SET_ERROR", error: "Create or verify a passkey on this device before continuing." });
6510
6544
  dispatch({ type: "NAVIGATE", step: "create-passkey" });
6511
- return;
6545
+ return null;
6512
6546
  }
6513
6547
  const provider = providers.find((p) => p.id === providerId);
6514
6548
  const providerName = provider?.name ?? "Wallet";
6515
- const isMobile = !shouldUseWalletConnector({
6516
- useWalletConnector: useWalletConnectorProp,
6517
- userAgent: typeof navigator === "undefined" ? void 0 : navigator.userAgent
6518
- });
6519
- if (isMobile) {
6520
- dispatch({ type: "PAY_STARTED", isSetupRedirect: true });
6521
- } else {
6522
- dispatch({ type: "PAY_STARTED", isSetupRedirect: false });
6523
- dispatch({ type: "NAVIGATE", step: "setup-status" });
6524
- }
6525
6549
  try {
6526
6550
  const token = await getAccessToken();
6527
6551
  if (!token) throw new Error("Not authenticated");
@@ -6534,21 +6558,71 @@ function useProviderHandlers(deps) {
6534
6558
  });
6535
6559
  const session = account.authorizationSessions?.[0];
6536
6560
  if (!session) throw new Error("No authorization session returned.");
6561
+ return { accountId: account.id, sessionId: session.id, uri: session.uri };
6562
+ } catch (err) {
6563
+ captureException(err);
6564
+ const msg = err instanceof Error ? err.message : "Failed to set up wallet";
6565
+ dispatch({ type: "PAY_ERROR", error: msg, fallbackStep: "wallet-picker" });
6566
+ onError?.(msg);
6567
+ return null;
6568
+ }
6569
+ }, [activeCredentialId, providers, apiBaseUrl, getAccessToken, onError, dispatch]);
6570
+ const handleSelectProvider = useCallback(async (providerId, preparedSession) => {
6571
+ dispatch({ type: "SELECT_PROVIDER", providerId });
6572
+ if (!activeCredentialId) {
6573
+ dispatch({ type: "SET_ERROR", error: "Create or verify a passkey on this device before continuing." });
6574
+ dispatch({ type: "NAVIGATE", step: "create-passkey" });
6575
+ return;
6576
+ }
6577
+ const provider = providers.find((p) => p.id === providerId);
6578
+ const providerName = provider?.name ?? "Wallet";
6579
+ const isMobile = !shouldUseWalletConnector({
6580
+ useWalletConnector: useWalletConnectorProp,
6581
+ userAgent: typeof navigator === "undefined" ? void 0 : navigator.userAgent
6582
+ });
6583
+ if (!isMobile) {
6584
+ dispatch({ type: "PAY_STARTED", isSetupRedirect: false });
6585
+ dispatch({ type: "NAVIGATE", step: "setup-status" });
6586
+ }
6587
+ try {
6588
+ let accountId;
6589
+ let sessionId;
6590
+ let sessionUri;
6591
+ if (preparedSession) {
6592
+ accountId = preparedSession.accountId;
6593
+ sessionId = preparedSession.sessionId;
6594
+ sessionUri = preparedSession.uri;
6595
+ } else {
6596
+ const token = await getAccessToken();
6597
+ if (!token) throw new Error("Not authenticated");
6598
+ const newAccountId = crypto.randomUUID();
6599
+ const account = await createAccount(apiBaseUrl, token, {
6600
+ id: newAccountId,
6601
+ name: providerName,
6602
+ credentialId: activeCredentialId,
6603
+ providerId
6604
+ });
6605
+ const session = account.authorizationSessions?.[0];
6606
+ if (!session) throw new Error("No authorization session returned.");
6607
+ accountId = account.id;
6608
+ sessionId = session.id;
6609
+ sessionUri = session.uri;
6610
+ }
6537
6611
  if (isMobile) {
6538
6612
  handlingMobileReturnRef.current = false;
6539
6613
  mobileSetupFlowRef.current = true;
6540
- setupAccountIdRef.current = account.id;
6614
+ setupAccountIdRef.current = accountId;
6541
6615
  persistMobileFlowState({
6542
- accountId: account.id,
6543
- sessionId: session.id,
6544
- deeplinkUri: session.uri,
6616
+ accountId,
6617
+ sessionId,
6618
+ deeplinkUri: sessionUri,
6545
6619
  providerId,
6546
6620
  isSetup: true
6547
6621
  });
6548
- dispatch({ type: "MOBILE_DEEPLINK_READY", deeplinkUri: session.uri });
6549
- triggerDeeplink(session.uri);
6622
+ triggerDeeplink(sessionUri);
6623
+ dispatch({ type: "MOBILE_DEEPLINK_READY", deeplinkUri: sessionUri });
6550
6624
  } else {
6551
- await authExecutor.executeSessionById(session.id);
6625
+ await authExecutor.executeSessionById(sessionId);
6552
6626
  await reloadAccounts();
6553
6627
  dispatch({ type: "NAVIGATE", step: "deposit" });
6554
6628
  }
@@ -6558,7 +6632,9 @@ function useProviderHandlers(deps) {
6558
6632
  dispatch({ type: "PAY_ERROR", error: msg, fallbackStep: "wallet-picker" });
6559
6633
  onError?.(msg);
6560
6634
  } finally {
6561
- dispatch({ type: "PAY_ENDED" });
6635
+ if (!isMobile) {
6636
+ dispatch({ type: "PAY_ENDED" });
6637
+ }
6562
6638
  }
6563
6639
  }, [
6564
6640
  activeCredentialId,
@@ -6776,6 +6852,7 @@ function useProviderHandlers(deps) {
6776
6852
  reauthTokenRef
6777
6853
  ]);
6778
6854
  return {
6855
+ handlePrepareProvider,
6779
6856
  handleSelectProvider,
6780
6857
  handleContinueConnection,
6781
6858
  handleSelectAccount,
@@ -7634,6 +7711,7 @@ function SwypePaymentInner({
7634
7711
  onRegisterPasskey: passkey.handleRegisterPasskey,
7635
7712
  onCreatePasskeyViaPopup: passkey.handleCreatePasskeyViaPopup,
7636
7713
  onVerifyPasskeyViaPopup: passkey.handleVerifyPasskeyViaPopup,
7714
+ onPrepareProvider: provider.handlePrepareProvider,
7637
7715
  onSelectProvider: provider.handleSelectProvider,
7638
7716
  onContinueConnection: provider.handleContinueConnection,
7639
7717
  onSelectAccount: provider.handleSelectAccount,