@swype-org/react-sdk 0.1.88 → 0.1.89

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.cjs CHANGED
@@ -1583,43 +1583,6 @@ function useTransferSigning(pollIntervalMs = 2e3, options) {
1583
1583
  );
1584
1584
  return { signing, signPayload, error, signTransfer: signTransfer2 };
1585
1585
  }
1586
- function Spinner({ size = 40, label }) {
1587
- const { tokens } = useSwypeConfig();
1588
- return /* @__PURE__ */ jsxRuntime.jsxs(
1589
- "div",
1590
- {
1591
- style: {
1592
- display: "flex",
1593
- flexDirection: "column",
1594
- alignItems: "center",
1595
- gap: "12px"
1596
- },
1597
- children: [
1598
- /* @__PURE__ */ jsxRuntime.jsx(
1599
- "div",
1600
- {
1601
- style: {
1602
- width: size,
1603
- height: size,
1604
- border: `4px solid ${tokens.bgInput}`,
1605
- borderTopColor: tokens.accent,
1606
- borderRightColor: tokens.accent + "66",
1607
- borderRadius: "50%",
1608
- boxShadow: "inset 0 0 0 1px rgba(255,255,255,0.1)",
1609
- animation: "swype-spin 0.9s linear infinite"
1610
- }
1611
- }
1612
- ),
1613
- label && /* @__PURE__ */ jsxRuntime.jsx("p", { style: { color: tokens.textSecondary, fontSize: "0.875rem", margin: 0 }, children: label }),
1614
- /* @__PURE__ */ jsxRuntime.jsx("style", { children: `
1615
- @keyframes swype-spin {
1616
- to { transform: rotate(360deg); }
1617
- }
1618
- ` })
1619
- ]
1620
- }
1621
- );
1622
- }
1623
1586
 
1624
1587
  // src/auth.ts
1625
1588
  var EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
@@ -1781,6 +1744,426 @@ function resolveDataLoadAction({
1781
1744
  }
1782
1745
  return "load";
1783
1746
  }
1747
+
1748
+ // src/paymentHelpers.ts
1749
+ var ACTIVE_CREDENTIAL_STORAGE_KEY = "swype_active_credential_id";
1750
+ var MOBILE_FLOW_STORAGE_KEY = "swype_mobile_flow";
1751
+ var MIN_SEND_AMOUNT_USD = 0.25;
1752
+ function persistMobileFlowState(data) {
1753
+ try {
1754
+ sessionStorage.setItem(MOBILE_FLOW_STORAGE_KEY, JSON.stringify(data));
1755
+ } catch {
1756
+ }
1757
+ }
1758
+ function loadMobileFlowState() {
1759
+ try {
1760
+ const raw = sessionStorage.getItem(MOBILE_FLOW_STORAGE_KEY);
1761
+ if (!raw) return null;
1762
+ return JSON.parse(raw);
1763
+ } catch {
1764
+ return null;
1765
+ }
1766
+ }
1767
+ function clearMobileFlowState() {
1768
+ try {
1769
+ sessionStorage.removeItem(MOBILE_FLOW_STORAGE_KEY);
1770
+ } catch {
1771
+ }
1772
+ }
1773
+ function computeSmartDefaults(accts, transferAmount) {
1774
+ if (accts.length === 0) return null;
1775
+ for (const acct of accts) {
1776
+ for (const wallet of acct.wallets) {
1777
+ if (wallet.status === "ACTIVE") {
1778
+ const bestSource = wallet.sources.find(
1779
+ (s) => s.balance.available.amount >= transferAmount
1780
+ );
1781
+ if (bestSource) {
1782
+ return { accountId: acct.id, walletId: wallet.id };
1783
+ }
1784
+ }
1785
+ }
1786
+ }
1787
+ let bestAccount = null;
1788
+ let bestWallet = null;
1789
+ let bestBalance = -1;
1790
+ let bestIsActive = false;
1791
+ for (const acct of accts) {
1792
+ for (const wallet of acct.wallets) {
1793
+ const walletBal = wallet.balance.available.amount;
1794
+ const isActive = wallet.status === "ACTIVE";
1795
+ if (walletBal > bestBalance || walletBal === bestBalance && isActive && !bestIsActive) {
1796
+ bestBalance = walletBal;
1797
+ bestAccount = acct;
1798
+ bestWallet = wallet;
1799
+ bestIsActive = isActive;
1800
+ }
1801
+ }
1802
+ }
1803
+ if (bestAccount) {
1804
+ return {
1805
+ accountId: bestAccount.id,
1806
+ walletId: bestWallet?.id ?? null
1807
+ };
1808
+ }
1809
+ return { accountId: accts[0].id, walletId: null };
1810
+ }
1811
+ function parseRawBalance(rawBalance, decimals) {
1812
+ const parsed = Number(rawBalance);
1813
+ if (!Number.isFinite(parsed)) return 0;
1814
+ return parsed / 10 ** decimals;
1815
+ }
1816
+ function buildSelectSourceChoices(options) {
1817
+ const chainChoices = [];
1818
+ const chainIndexByName = /* @__PURE__ */ new Map();
1819
+ for (const option of options) {
1820
+ const { chainName, tokenSymbol } = option;
1821
+ const balance = parseRawBalance(option.rawBalance, option.decimals);
1822
+ let chainChoice;
1823
+ const existingIdx = chainIndexByName.get(chainName);
1824
+ if (existingIdx === void 0) {
1825
+ chainChoice = { chainName, balance: 0, tokens: [] };
1826
+ chainIndexByName.set(chainName, chainChoices.length);
1827
+ chainChoices.push(chainChoice);
1828
+ } else {
1829
+ chainChoice = chainChoices[existingIdx];
1830
+ }
1831
+ chainChoice.balance += balance;
1832
+ const existing = chainChoice.tokens.find((t) => t.tokenSymbol === tokenSymbol);
1833
+ if (existing) {
1834
+ existing.balance += balance;
1835
+ } else {
1836
+ chainChoice.tokens.push({ tokenSymbol, balance });
1837
+ }
1838
+ }
1839
+ return chainChoices;
1840
+ }
1841
+
1842
+ // src/paymentReducer.ts
1843
+ function deriveSourceTypeAndId(state) {
1844
+ if (state.connectingNewAccount) {
1845
+ return { sourceType: "providerId", sourceId: state.selectedProviderId ?? "" };
1846
+ }
1847
+ if (state.selectedWalletId) {
1848
+ return { sourceType: "walletId", sourceId: state.selectedWalletId };
1849
+ }
1850
+ if (state.selectedAccountId) {
1851
+ return { sourceType: "accountId", sourceId: state.selectedAccountId };
1852
+ }
1853
+ return { sourceType: "providerId", sourceId: state.selectedProviderId ?? "" };
1854
+ }
1855
+ function createInitialState(config) {
1856
+ return {
1857
+ step: "login",
1858
+ error: null,
1859
+ providers: [],
1860
+ accounts: [],
1861
+ chains: [],
1862
+ loadingData: false,
1863
+ selectedAccountId: null,
1864
+ selectedWalletId: null,
1865
+ selectedProviderId: null,
1866
+ connectingNewAccount: false,
1867
+ amount: config.depositAmount != null ? config.depositAmount.toString() : "",
1868
+ transfer: null,
1869
+ creatingTransfer: false,
1870
+ registeringPasskey: false,
1871
+ verifyingPasskeyPopup: false,
1872
+ passkeyPopupNeeded: config.passkeyPopupNeeded,
1873
+ activeCredentialId: config.activeCredentialId,
1874
+ knownCredentialIds: [],
1875
+ verificationTarget: null,
1876
+ oneTapLimit: 100,
1877
+ mobileFlow: false,
1878
+ deeplinkUri: null,
1879
+ increasingLimit: false
1880
+ };
1881
+ }
1882
+ function paymentReducer(state, action) {
1883
+ switch (action.type) {
1884
+ // ── Auth ──────────────────────────────────────────────────────
1885
+ case "CODE_SENT":
1886
+ return {
1887
+ ...state,
1888
+ verificationTarget: action.target,
1889
+ error: null,
1890
+ step: "otp-verify"
1891
+ };
1892
+ case "BACK_TO_LOGIN":
1893
+ return {
1894
+ ...state,
1895
+ verificationTarget: null,
1896
+ error: null,
1897
+ step: "login"
1898
+ };
1899
+ // ── Passkey ──────────────────────────────────────────────────
1900
+ case "PASSKEY_CONFIG_LOADED":
1901
+ return {
1902
+ ...state,
1903
+ knownCredentialIds: action.knownIds,
1904
+ oneTapLimit: action.oneTapLimit ?? state.oneTapLimit
1905
+ };
1906
+ case "PASSKEY_ACTIVATED":
1907
+ return {
1908
+ ...state,
1909
+ activeCredentialId: action.credentialId,
1910
+ passkeyPopupNeeded: false
1911
+ };
1912
+ case "SET_PASSKEY_POPUP_NEEDED":
1913
+ return { ...state, passkeyPopupNeeded: action.needed };
1914
+ case "SET_REGISTERING_PASSKEY":
1915
+ return { ...state, registeringPasskey: action.value };
1916
+ case "SET_VERIFYING_PASSKEY":
1917
+ return { ...state, verifyingPasskeyPopup: action.value };
1918
+ // ── Data loading ─────────────────────────────────────────────
1919
+ case "DATA_LOAD_START":
1920
+ return { ...state, loadingData: true, error: null };
1921
+ case "DATA_LOADED": {
1922
+ const next = {
1923
+ ...state,
1924
+ providers: action.providers,
1925
+ accounts: action.accounts,
1926
+ chains: action.chains
1927
+ };
1928
+ if (action.defaults) {
1929
+ next.selectedAccountId = action.defaults.accountId;
1930
+ next.selectedWalletId = action.defaults.walletId;
1931
+ } else if (action.fallbackProviderId && !state.connectingNewAccount) {
1932
+ next.selectedProviderId = action.fallbackProviderId;
1933
+ }
1934
+ if (action.clearMobileState) {
1935
+ next.mobileFlow = false;
1936
+ next.deeplinkUri = null;
1937
+ }
1938
+ if (action.resolvedStep !== void 0) {
1939
+ next.step = action.resolvedStep;
1940
+ }
1941
+ return next;
1942
+ }
1943
+ case "DATA_LOAD_END":
1944
+ return { ...state, loadingData: false };
1945
+ case "ACCOUNTS_RELOADED": {
1946
+ const next = {
1947
+ ...state,
1948
+ accounts: action.accounts,
1949
+ providers: action.providers
1950
+ };
1951
+ if (action.defaults) {
1952
+ next.selectedAccountId = action.defaults.accountId;
1953
+ next.selectedWalletId = action.defaults.walletId;
1954
+ next.connectingNewAccount = false;
1955
+ }
1956
+ return next;
1957
+ }
1958
+ // ── Source selection ──────────────────────────────────────────
1959
+ case "SELECT_PROVIDER":
1960
+ return {
1961
+ ...state,
1962
+ selectedProviderId: action.providerId,
1963
+ selectedAccountId: null,
1964
+ connectingNewAccount: true
1965
+ };
1966
+ case "SELECT_ACCOUNT":
1967
+ return {
1968
+ ...state,
1969
+ selectedAccountId: action.accountId,
1970
+ selectedWalletId: action.walletId,
1971
+ connectingNewAccount: false,
1972
+ step: "deposit"
1973
+ };
1974
+ // ── Transfer lifecycle ───────────────────────────────────────
1975
+ case "PAY_STARTED":
1976
+ return {
1977
+ ...state,
1978
+ step: action.isSetupRedirect ? "open-wallet" : "processing",
1979
+ error: null,
1980
+ creatingTransfer: true,
1981
+ deeplinkUri: null,
1982
+ mobileFlow: false
1983
+ };
1984
+ case "PAY_ENDED":
1985
+ return { ...state, creatingTransfer: false };
1986
+ case "PAY_ERROR":
1987
+ return {
1988
+ ...state,
1989
+ error: action.error,
1990
+ step: action.fallbackStep
1991
+ };
1992
+ case "TRANSFER_CREATED":
1993
+ return { ...state, transfer: action.transfer };
1994
+ case "TRANSFER_SIGNED":
1995
+ return { ...state, transfer: action.transfer };
1996
+ case "TRANSFER_COMPLETED":
1997
+ return {
1998
+ ...state,
1999
+ transfer: action.transfer,
2000
+ step: "success",
2001
+ mobileFlow: false,
2002
+ deeplinkUri: null
2003
+ };
2004
+ case "TRANSFER_FAILED":
2005
+ return {
2006
+ ...state,
2007
+ transfer: action.transfer,
2008
+ error: action.error,
2009
+ step: "success",
2010
+ mobileFlow: false,
2011
+ deeplinkUri: null
2012
+ };
2013
+ case "PROCESSING_TIMEOUT":
2014
+ return { ...state, error: action.error, step: "deposit" };
2015
+ case "CONFIRM_SIGN_SUCCESS":
2016
+ return {
2017
+ ...state,
2018
+ transfer: action.transfer,
2019
+ step: "processing",
2020
+ mobileFlow: false,
2021
+ deeplinkUri: null
2022
+ };
2023
+ // ── Mobile flow ──────────────────────────────────────────────
2024
+ case "MOBILE_DEEPLINK_READY":
2025
+ return {
2026
+ ...state,
2027
+ mobileFlow: true,
2028
+ deeplinkUri: action.deeplinkUri,
2029
+ step: "open-wallet"
2030
+ };
2031
+ case "MOBILE_SETUP_COMPLETE":
2032
+ return {
2033
+ ...state,
2034
+ transfer: action.transfer,
2035
+ error: null,
2036
+ mobileFlow: false,
2037
+ deeplinkUri: null,
2038
+ step: "deposit"
2039
+ };
2040
+ case "MOBILE_SIGN_READY":
2041
+ return {
2042
+ ...state,
2043
+ transfer: action.transfer,
2044
+ error: null,
2045
+ mobileFlow: false,
2046
+ deeplinkUri: null,
2047
+ step: "confirm-sign"
2048
+ };
2049
+ case "CLEAR_MOBILE_STATE":
2050
+ return { ...state, mobileFlow: false, deeplinkUri: null };
2051
+ case "ENTER_MOBILE_FLOW":
2052
+ return {
2053
+ ...state,
2054
+ mobileFlow: true,
2055
+ deeplinkUri: action.deeplinkUri,
2056
+ selectedProviderId: action.providerId,
2057
+ error: action.error ?? null,
2058
+ step: "open-wallet"
2059
+ };
2060
+ case "MOBILE_RESUME_SUCCESS":
2061
+ return {
2062
+ ...state,
2063
+ transfer: action.transfer,
2064
+ error: null,
2065
+ mobileFlow: false,
2066
+ deeplinkUri: null,
2067
+ step: "success"
2068
+ };
2069
+ case "MOBILE_RESUME_FAILED":
2070
+ return {
2071
+ ...state,
2072
+ transfer: action.transfer,
2073
+ error: "Transfer failed.",
2074
+ mobileFlow: false,
2075
+ deeplinkUri: null,
2076
+ step: "success"
2077
+ };
2078
+ case "MOBILE_RESUME_PROCESSING":
2079
+ return {
2080
+ ...state,
2081
+ transfer: action.transfer,
2082
+ error: null,
2083
+ mobileFlow: false,
2084
+ deeplinkUri: null,
2085
+ step: "processing"
2086
+ };
2087
+ // ── Increase limit ───────────────────────────────────────────
2088
+ case "SET_INCREASING_LIMIT":
2089
+ return { ...state, increasingLimit: action.value };
2090
+ case "INCREASE_LIMIT_DEEPLINK":
2091
+ return {
2092
+ ...state,
2093
+ transfer: action.transfer,
2094
+ mobileFlow: true,
2095
+ deeplinkUri: action.deeplinkUri
2096
+ };
2097
+ // ── Navigation & error ───────────────────────────────────────
2098
+ case "NAVIGATE":
2099
+ return { ...state, step: action.step };
2100
+ case "SET_ERROR":
2101
+ return { ...state, error: action.error };
2102
+ // ── Lifecycle ────────────────────────────────────────────────
2103
+ case "NEW_PAYMENT":
2104
+ return {
2105
+ ...state,
2106
+ step: "deposit",
2107
+ transfer: null,
2108
+ error: null,
2109
+ amount: action.depositAmount != null ? action.depositAmount.toString() : "",
2110
+ mobileFlow: false,
2111
+ deeplinkUri: null,
2112
+ connectingNewAccount: false,
2113
+ selectedWalletId: null,
2114
+ selectedAccountId: action.firstAccountId
2115
+ };
2116
+ case "LOGOUT":
2117
+ return {
2118
+ ...createInitialState({
2119
+ depositAmount: action.depositAmount,
2120
+ passkeyPopupNeeded: state.passkeyPopupNeeded,
2121
+ activeCredentialId: null
2122
+ })
2123
+ };
2124
+ case "SYNC_AMOUNT":
2125
+ return { ...state, amount: action.amount };
2126
+ default:
2127
+ return state;
2128
+ }
2129
+ }
2130
+ function Spinner({ size = 40, label }) {
2131
+ const { tokens } = useSwypeConfig();
2132
+ return /* @__PURE__ */ jsxRuntime.jsxs(
2133
+ "div",
2134
+ {
2135
+ style: {
2136
+ display: "flex",
2137
+ flexDirection: "column",
2138
+ alignItems: "center",
2139
+ gap: "12px"
2140
+ },
2141
+ children: [
2142
+ /* @__PURE__ */ jsxRuntime.jsx(
2143
+ "div",
2144
+ {
2145
+ style: {
2146
+ width: size,
2147
+ height: size,
2148
+ border: `4px solid ${tokens.bgInput}`,
2149
+ borderTopColor: tokens.accent,
2150
+ borderRightColor: tokens.accent + "66",
2151
+ borderRadius: "50%",
2152
+ boxShadow: "inset 0 0 0 1px rgba(255,255,255,0.1)",
2153
+ animation: "swype-spin 0.9s linear infinite"
2154
+ }
2155
+ }
2156
+ ),
2157
+ label && /* @__PURE__ */ jsxRuntime.jsx("p", { style: { color: tokens.textSecondary, fontSize: "0.875rem", margin: 0 }, children: label }),
2158
+ /* @__PURE__ */ jsxRuntime.jsx("style", { children: `
2159
+ @keyframes swype-spin {
2160
+ to { transform: rotate(360deg); }
2161
+ }
2162
+ ` })
2163
+ ]
2164
+ }
2165
+ );
2166
+ }
1784
2167
  var FOOTER_CSS = `
1785
2168
  .swype-screen-footer {
1786
2169
  padding-bottom: max(24px, env(safe-area-inset-bottom, 24px));
@@ -4497,22 +4880,259 @@ var errorStyle2 = (color) => ({
4497
4880
  color: "#ef4444",
4498
4881
  margin: "8px 0 0"
4499
4882
  });
4500
- var PaymentErrorBoundary = class extends react.Component {
4501
- constructor(props) {
4502
- super(props);
4503
- this.state = { hasError: false };
4504
- }
4505
- static getDerivedStateFromError() {
4506
- return { hasError: true };
4507
- }
4508
- componentDidCatch(error, _info) {
4509
- captureException(error);
4883
+ function CenteredSpinner({ label }) {
4884
+ return /* @__PURE__ */ jsxRuntime.jsx(ScreenLayout, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { style: { textAlign: "center", padding: "48px 0", flex: 1, display: "flex", alignItems: "center", justifyContent: "center" }, children: /* @__PURE__ */ jsxRuntime.jsx(Spinner, { label }) }) });
4885
+ }
4886
+ function StepRenderer({
4887
+ state,
4888
+ ready,
4889
+ authenticated,
4890
+ activeOtpStatus,
4891
+ pollingTransfer,
4892
+ pollingError,
4893
+ authExecutorError,
4894
+ transferSigningSigning,
4895
+ transferSigningError,
4896
+ pendingConnections,
4897
+ sourceName,
4898
+ sourceAddress,
4899
+ sourceVerified,
4900
+ maxSourceBalance,
4901
+ tokenCount,
4902
+ selectedAccount,
4903
+ selectSourceChoices,
4904
+ selectSourceRecommended,
4905
+ authInput,
4906
+ otpCode,
4907
+ selectSourceChainName,
4908
+ selectSourceTokenSymbol,
4909
+ merchantName,
4910
+ onBack,
4911
+ onDismiss,
4912
+ autoCloseSeconds,
4913
+ depositAmount,
4914
+ handlers
4915
+ }) {
4916
+ const { step } = state;
4917
+ if (!ready) {
4918
+ return /* @__PURE__ */ jsxRuntime.jsx(CenteredSpinner, { label: "Initializing..." });
4510
4919
  }
4511
- handleReset = () => {
4512
- this.setState({ hasError: false });
4513
- this.props.onReset();
4514
- };
4515
- render() {
4920
+ if (step === "login" && !authenticated) {
4921
+ return /* @__PURE__ */ jsxRuntime.jsx(
4922
+ LoginScreen,
4923
+ {
4924
+ authInput,
4925
+ onAuthInputChange: handlers.onSetAuthInput,
4926
+ onSubmit: handlers.onSendLoginCode,
4927
+ sending: activeOtpStatus === "sending-code",
4928
+ error: state.error,
4929
+ onBack,
4930
+ merchantInitials: merchantName ? merchantName.slice(0, 2).toUpperCase() : void 0
4931
+ }
4932
+ );
4933
+ }
4934
+ if (step === "otp-verify" && !authenticated) {
4935
+ return /* @__PURE__ */ jsxRuntime.jsx(
4936
+ OtpVerifyScreen,
4937
+ {
4938
+ maskedIdentifier: state.verificationTarget ? maskAuthIdentifier(state.verificationTarget) : "",
4939
+ otpCode,
4940
+ onOtpChange: (code) => {
4941
+ handlers.onSetOtpCode(code);
4942
+ },
4943
+ onVerify: handlers.onVerifyLoginCode,
4944
+ onResend: handlers.onResendLoginCode,
4945
+ onBack: handlers.onBackFromOtp,
4946
+ verifying: activeOtpStatus === "submitting-code",
4947
+ error: state.error
4948
+ }
4949
+ );
4950
+ }
4951
+ if ((step === "login" || step === "otp-verify") && authenticated) {
4952
+ return /* @__PURE__ */ jsxRuntime.jsx(CenteredSpinner, { label: "Verifying your passkey..." });
4953
+ }
4954
+ if (step === "verify-passkey") {
4955
+ return /* @__PURE__ */ jsxRuntime.jsx(
4956
+ VerifyPasskeyScreen,
4957
+ {
4958
+ onVerify: handlers.onVerifyPasskeyViaPopup,
4959
+ onBack: handlers.onLogout,
4960
+ verifying: state.verifyingPasskeyPopup,
4961
+ error: state.error
4962
+ }
4963
+ );
4964
+ }
4965
+ if (step === "create-passkey") {
4966
+ return /* @__PURE__ */ jsxRuntime.jsx(
4967
+ CreatePasskeyScreen,
4968
+ {
4969
+ onCreatePasskey: handlers.onRegisterPasskey,
4970
+ onBack: handlers.onLogout,
4971
+ creating: state.registeringPasskey,
4972
+ error: state.error,
4973
+ popupFallback: state.passkeyPopupNeeded,
4974
+ onCreatePasskeyViaPopup: handlers.onCreatePasskeyViaPopup
4975
+ }
4976
+ );
4977
+ }
4978
+ if (step === "wallet-picker") {
4979
+ return /* @__PURE__ */ jsxRuntime.jsx(
4980
+ WalletPickerScreen,
4981
+ {
4982
+ providers: state.providers,
4983
+ pendingConnections,
4984
+ loading: state.creatingTransfer,
4985
+ onSelectProvider: handlers.onSelectProvider,
4986
+ onContinueConnection: handlers.onContinueConnection,
4987
+ onBack: () => handlers.onNavigate(state.activeCredentialId ? "deposit" : "create-passkey")
4988
+ }
4989
+ );
4990
+ }
4991
+ if (step === "open-wallet") {
4992
+ const providerName = state.providers.find((p) => p.id === state.selectedProviderId)?.name ?? null;
4993
+ return /* @__PURE__ */ jsxRuntime.jsx(
4994
+ OpenWalletScreen,
4995
+ {
4996
+ walletName: providerName,
4997
+ deeplinkUri: state.deeplinkUri ?? "",
4998
+ loading: state.creatingTransfer || !state.deeplinkUri,
4999
+ error: state.error || pollingError,
5000
+ onRetryStatus: handlers.onRetryMobileStatus,
5001
+ onLogout: handlers.onLogout
5002
+ }
5003
+ );
5004
+ }
5005
+ if (step === "confirm-sign") {
5006
+ const providerName = state.providers.find((p) => p.id === state.selectedProviderId)?.name ?? null;
5007
+ return /* @__PURE__ */ jsxRuntime.jsx(
5008
+ ConfirmSignScreen,
5009
+ {
5010
+ walletName: providerName,
5011
+ signing: transferSigningSigning,
5012
+ error: state.error || transferSigningError,
5013
+ onSign: handlers.onConfirmSign,
5014
+ onLogout: handlers.onLogout
5015
+ }
5016
+ );
5017
+ }
5018
+ if (step === "deposit") {
5019
+ if (state.loadingData) {
5020
+ return /* @__PURE__ */ jsxRuntime.jsx(CenteredSpinner, { label: "Loading..." });
5021
+ }
5022
+ const parsedAmt = depositAmount != null ? depositAmount : 5;
5023
+ return /* @__PURE__ */ jsxRuntime.jsx(
5024
+ DepositScreen,
5025
+ {
5026
+ merchantName,
5027
+ sourceName,
5028
+ sourceAddress,
5029
+ sourceVerified,
5030
+ availableBalance: maxSourceBalance,
5031
+ remainingLimit: selectedAccount?.remainingAllowance ?? state.oneTapLimit,
5032
+ tokenCount,
5033
+ initialAmount: parsedAmt,
5034
+ processing: state.creatingTransfer,
5035
+ error: state.error,
5036
+ onDeposit: handlers.onPay,
5037
+ onChangeSource: () => handlers.onNavigate("wallet-picker"),
5038
+ onSwitchWallet: () => handlers.onNavigate("wallet-picker"),
5039
+ onBack: onBack ?? (() => handlers.onLogout()),
5040
+ onLogout: handlers.onLogout,
5041
+ onIncreaseLimit: handlers.onIncreaseLimit,
5042
+ increasingLimit: state.increasingLimit
5043
+ }
5044
+ );
5045
+ }
5046
+ if (step === "processing") {
5047
+ const polledStatus = pollingTransfer?.status;
5048
+ const transferPhase = state.creatingTransfer ? "creating" : polledStatus === "SENDING" || polledStatus === "SENT" ? "sent" : "verifying";
5049
+ return /* @__PURE__ */ jsxRuntime.jsx(
5050
+ TransferStatusScreen,
5051
+ {
5052
+ phase: transferPhase,
5053
+ error: state.error || authExecutorError || transferSigningError || pollingError,
5054
+ onLogout: handlers.onLogout
5055
+ }
5056
+ );
5057
+ }
5058
+ if (step === "select-source") {
5059
+ return /* @__PURE__ */ jsxRuntime.jsx(
5060
+ SelectSourceScreen,
5061
+ {
5062
+ choices: selectSourceChoices,
5063
+ selectedChainName: selectSourceChainName,
5064
+ selectedTokenSymbol: selectSourceTokenSymbol,
5065
+ recommended: selectSourceRecommended,
5066
+ onChainChange: handlers.onSelectSourceChainChange,
5067
+ onTokenChange: handlers.onSetSelectSourceTokenSymbol,
5068
+ onConfirm: handlers.onConfirmSelectSource,
5069
+ onLogout: handlers.onLogout
5070
+ }
5071
+ );
5072
+ }
5073
+ if (step === "success") {
5074
+ const succeeded = state.transfer?.status === "COMPLETED";
5075
+ const displayAmount = state.transfer?.amount?.amount ?? 0;
5076
+ const displayCurrency = state.transfer?.amount?.currency ?? "USD";
5077
+ return /* @__PURE__ */ jsxRuntime.jsx(
5078
+ SuccessScreen,
5079
+ {
5080
+ amount: displayAmount,
5081
+ currency: displayCurrency,
5082
+ succeeded,
5083
+ error: state.error,
5084
+ merchantName,
5085
+ sourceName,
5086
+ remainingLimit: succeeded ? (() => {
5087
+ const limit = selectedAccount?.remainingAllowance ?? state.oneTapLimit;
5088
+ return limit > displayAmount ? limit - displayAmount : 0;
5089
+ })() : void 0,
5090
+ onDone: onDismiss ?? handlers.onNewPayment,
5091
+ onLogout: handlers.onLogout,
5092
+ autoCloseSeconds
5093
+ }
5094
+ );
5095
+ }
5096
+ if (step === "low-balance") {
5097
+ return /* @__PURE__ */ jsxRuntime.jsx(
5098
+ DepositScreen,
5099
+ {
5100
+ merchantName,
5101
+ sourceName,
5102
+ sourceAddress,
5103
+ sourceVerified,
5104
+ availableBalance: 0,
5105
+ remainingLimit: selectedAccount?.remainingAllowance ?? state.oneTapLimit,
5106
+ tokenCount,
5107
+ initialAmount: depositAmount ?? 5,
5108
+ processing: false,
5109
+ error: state.error,
5110
+ onDeposit: handlers.onPay,
5111
+ onChangeSource: () => handlers.onNavigate("wallet-picker"),
5112
+ onSwitchWallet: () => handlers.onNavigate("wallet-picker"),
5113
+ onBack: onBack ?? (() => handlers.onLogout()),
5114
+ onLogout: handlers.onLogout
5115
+ }
5116
+ );
5117
+ }
5118
+ return null;
5119
+ }
5120
+ var PaymentErrorBoundary = class extends react.Component {
5121
+ constructor(props) {
5122
+ super(props);
5123
+ this.state = { hasError: false };
5124
+ }
5125
+ static getDerivedStateFromError() {
5126
+ return { hasError: true };
5127
+ }
5128
+ componentDidCatch(error, _info) {
5129
+ captureException(error);
5130
+ }
5131
+ handleReset = () => {
5132
+ this.setState({ hasError: false });
5133
+ this.props.onReset();
5134
+ };
5135
+ render() {
4516
5136
  if (!this.state.hasError) {
4517
5137
  return this.props.children;
4518
5138
  }
@@ -4565,98 +5185,6 @@ var buttonStyle3 = {
4565
5185
  fontFamily: "inherit",
4566
5186
  cursor: "pointer"
4567
5187
  };
4568
- var ACTIVE_CREDENTIAL_STORAGE_KEY = "swype_active_credential_id";
4569
- var MOBILE_FLOW_STORAGE_KEY = "swype_mobile_flow";
4570
- var MIN_SEND_AMOUNT_USD = 0.25;
4571
- function persistMobileFlowState(data) {
4572
- try {
4573
- sessionStorage.setItem(MOBILE_FLOW_STORAGE_KEY, JSON.stringify(data));
4574
- } catch {
4575
- }
4576
- }
4577
- function loadMobileFlowState() {
4578
- try {
4579
- const raw = sessionStorage.getItem(MOBILE_FLOW_STORAGE_KEY);
4580
- if (!raw) return null;
4581
- return JSON.parse(raw);
4582
- } catch {
4583
- return null;
4584
- }
4585
- }
4586
- function clearMobileFlowState() {
4587
- try {
4588
- sessionStorage.removeItem(MOBILE_FLOW_STORAGE_KEY);
4589
- } catch {
4590
- }
4591
- }
4592
- function computeSmartDefaults(accts, transferAmount) {
4593
- if (accts.length === 0) return null;
4594
- for (const acct of accts) {
4595
- for (const wallet of acct.wallets) {
4596
- if (wallet.status === "ACTIVE") {
4597
- const bestSource = wallet.sources.find(
4598
- (s) => s.balance.available.amount >= transferAmount
4599
- );
4600
- if (bestSource) {
4601
- return { accountId: acct.id, walletId: wallet.id };
4602
- }
4603
- }
4604
- }
4605
- }
4606
- let bestAccount = null;
4607
- let bestWallet = null;
4608
- let bestBalance = -1;
4609
- let bestIsActive = false;
4610
- for (const acct of accts) {
4611
- for (const wallet of acct.wallets) {
4612
- const walletBal = wallet.balance.available.amount;
4613
- const isActive = wallet.status === "ACTIVE";
4614
- if (walletBal > bestBalance || walletBal === bestBalance && isActive && !bestIsActive) {
4615
- bestBalance = walletBal;
4616
- bestAccount = acct;
4617
- bestWallet = wallet;
4618
- bestIsActive = isActive;
4619
- }
4620
- }
4621
- }
4622
- if (bestAccount) {
4623
- return {
4624
- accountId: bestAccount.id,
4625
- walletId: bestWallet?.id ?? null
4626
- };
4627
- }
4628
- return { accountId: accts[0].id, walletId: null };
4629
- }
4630
- function parseRawBalance(rawBalance, decimals) {
4631
- const parsed = Number(rawBalance);
4632
- if (!Number.isFinite(parsed)) return 0;
4633
- return parsed / 10 ** decimals;
4634
- }
4635
- function buildSelectSourceChoices(options) {
4636
- const chainChoices = [];
4637
- const chainIndexByName = /* @__PURE__ */ new Map();
4638
- for (const option of options) {
4639
- const { chainName, tokenSymbol } = option;
4640
- const balance = parseRawBalance(option.rawBalance, option.decimals);
4641
- let chainChoice;
4642
- const existingIdx = chainIndexByName.get(chainName);
4643
- if (existingIdx === void 0) {
4644
- chainChoice = { chainName, balance: 0, tokens: [] };
4645
- chainIndexByName.set(chainName, chainChoices.length);
4646
- chainChoices.push(chainChoice);
4647
- } else {
4648
- chainChoice = chainChoices[existingIdx];
4649
- }
4650
- chainChoice.balance += balance;
4651
- const existing = chainChoice.tokens.find((t) => t.tokenSymbol === tokenSymbol);
4652
- if (existing) {
4653
- existing.balance += balance;
4654
- } else {
4655
- chainChoice.tokens.push({ tokenSymbol, balance });
4656
- }
4657
- }
4658
- return chainChoices;
4659
- }
4660
5188
  function SwypePayment(props) {
4661
5189
  const resetKey = react.useRef(0);
4662
5190
  const handleBoundaryReset = react.useCallback(() => {
@@ -4668,7 +5196,7 @@ function SwypePaymentInner({
4668
5196
  destination,
4669
5197
  onComplete,
4670
5198
  onError,
4671
- useWalletConnector,
5199
+ useWalletConnector: useWalletConnectorProp,
4672
5200
  idempotencyKey,
4673
5201
  merchantAuthorization,
4674
5202
  merchantName,
@@ -4676,7 +5204,7 @@ function SwypePaymentInner({
4676
5204
  onDismiss,
4677
5205
  autoCloseSeconds
4678
5206
  }) {
4679
- const { apiBaseUrl, tokens, depositAmount } = useSwypeConfig();
5207
+ const { apiBaseUrl, depositAmount } = useSwypeConfig();
4680
5208
  const { ready, authenticated, user, logout, getAccessToken } = reactAuth.usePrivy();
4681
5209
  const {
4682
5210
  sendCode: sendEmailCode,
@@ -4688,161 +5216,74 @@ function SwypePaymentInner({
4688
5216
  loginWithCode: loginWithSmsCode,
4689
5217
  state: smsLoginState
4690
5218
  } = reactAuth.useLoginWithSms();
4691
- const { initOAuth } = reactAuth.useLoginWithOAuth();
4692
- const [step, setStep] = react.useState("login");
4693
- const [error, setError] = react.useState(null);
4694
- const [providers, setProviders] = react.useState([]);
4695
- const [accounts, setAccounts] = react.useState([]);
4696
- const [chains, setChains] = react.useState([]);
4697
- const [loadingData, setLoadingData] = react.useState(false);
4698
- const [selectedAccountId, setSelectedAccountId] = react.useState(null);
4699
- const [selectedWalletId, setSelectedWalletId] = react.useState(null);
4700
- const [selectedProviderId, setSelectedProviderId] = react.useState(null);
4701
- const [connectingNewAccount, setConnectingNewAccount] = react.useState(false);
4702
- const [amount, setAmount] = react.useState(
4703
- depositAmount != null ? depositAmount.toString() : ""
4704
- );
4705
- const [transfer, setTransfer] = react.useState(null);
4706
- const [creatingTransfer, setCreatingTransfer] = react.useState(false);
4707
- const [registeringPasskey, setRegisteringPasskey] = react.useState(false);
4708
- const [verifyingPasskeyPopup, setVerifyingPasskeyPopup] = react.useState(false);
4709
- const [passkeyPopupNeeded, setPasskeyPopupNeeded] = react.useState(
4710
- () => isSafari() && isInCrossOriginIframe()
5219
+ reactAuth.useLoginWithOAuth();
5220
+ const [state, dispatch] = react.useReducer(
5221
+ paymentReducer,
5222
+ {
5223
+ depositAmount,
5224
+ passkeyPopupNeeded: isSafari() && isInCrossOriginIframe(),
5225
+ activeCredentialId: typeof window === "undefined" ? null : window.localStorage.getItem(ACTIVE_CREDENTIAL_STORAGE_KEY)
5226
+ },
5227
+ createInitialState
4711
5228
  );
4712
- const [activeCredentialId, setActiveCredentialId] = react.useState(() => {
4713
- if (typeof window === "undefined") return null;
4714
- return window.localStorage.getItem(ACTIVE_CREDENTIAL_STORAGE_KEY);
4715
- });
4716
- const [knownCredentialIds, setKnownCredentialIds] = react.useState([]);
4717
- const [authInput, setAuthInput] = react.useState("");
4718
- const [verificationTarget, setVerificationTarget] = react.useState(null);
4719
- const [otpCode, setOtpCode] = react.useState("");
4720
- const [oneTapLimit, setOneTapLimit] = react.useState(100);
4721
- const [mobileFlow, setMobileFlow] = react.useState(false);
4722
- const [deeplinkUri, setDeeplinkUri] = react.useState(null);
4723
5229
  const loadingDataRef = react.useRef(false);
4724
5230
  const pollingTransferIdRef = react.useRef(null);
4725
- const mobileSigningTransferIdRef = react.useRef(null);
4726
5231
  const mobileSetupFlowRef = react.useRef(false);
4727
5232
  const handlingMobileReturnRef = react.useRef(false);
4728
5233
  const processingStartedAtRef = react.useRef(null);
4729
- const [selectSourceChainName, setSelectSourceChainName] = react.useState("");
4730
- const [selectSourceTokenSymbol, setSelectSourceTokenSymbol] = react.useState("");
4731
5234
  const initializedSelectSourceActionRef = react.useRef(null);
4732
5235
  const preSelectSourceStepRef = react.useRef(null);
5236
+ const [authInput, setAuthInput] = react.useState("");
5237
+ const [otpCode, setOtpCode] = react.useState("");
5238
+ const [selectSourceChainName, setSelectSourceChainName] = react.useState("");
5239
+ const [selectSourceTokenSymbol, setSelectSourceTokenSymbol] = react.useState("");
4733
5240
  const authExecutor = useAuthorizationExecutor();
4734
5241
  const polling = useTransferPolling();
4735
5242
  const transferSigning = useTransferSigning();
4736
- const sourceType = connectingNewAccount ? "providerId" : selectedWalletId ? "walletId" : selectedAccountId ? "accountId" : "providerId";
4737
- const sourceId = connectingNewAccount ? selectedProviderId ?? "" : selectedWalletId ? selectedWalletId : selectedAccountId ? selectedAccountId : selectedProviderId ?? "";
4738
- const reloadAccounts = react.useCallback(async () => {
4739
- const token = await getAccessToken();
4740
- if (!token || !activeCredentialId) return;
4741
- const [accts, prov] = await Promise.all([
4742
- fetchAccounts(apiBaseUrl, token, activeCredentialId),
4743
- fetchProviders(apiBaseUrl, token)
4744
- ]);
4745
- setAccounts(accts);
4746
- setProviders(prov);
4747
- const parsedAmt = depositAmount != null ? depositAmount : 0;
4748
- const defaults = computeSmartDefaults(accts, parsedAmt);
4749
- if (defaults) {
4750
- setSelectedAccountId(defaults.accountId);
4751
- setSelectedWalletId(defaults.walletId);
4752
- setConnectingNewAccount(false);
4753
- }
4754
- }, [getAccessToken, activeCredentialId, apiBaseUrl, depositAmount]);
4755
- const resetDataLoadingState = react.useCallback(() => {
4756
- loadingDataRef.current = false;
4757
- setLoadingData(false);
4758
- }, []);
4759
- const enterPersistedMobileFlow = react.useCallback((persisted, errorMessage) => {
4760
- setMobileFlow(true);
4761
- setDeeplinkUri(persisted.deeplinkUri);
4762
- setSelectedProviderId(persisted.providerId);
4763
- pollingTransferIdRef.current = persisted.transferId;
4764
- mobileSetupFlowRef.current = persisted.isSetup;
4765
- setError(errorMessage ?? null);
4766
- setStep("open-wallet");
4767
- polling.startPolling(persisted.transferId);
4768
- }, [polling]);
4769
- const handleAuthorizedMobileReturn = react.useCallback(async (authorizedTransfer, isSetup) => {
4770
- if (handlingMobileReturnRef.current) return;
4771
- handlingMobileReturnRef.current = true;
4772
- polling.stopPolling();
4773
- if (isSetup) {
4774
- mobileSetupFlowRef.current = false;
4775
- clearMobileFlowState();
4776
- try {
4777
- await reloadAccounts();
4778
- resetDataLoadingState();
4779
- setTransfer(authorizedTransfer);
4780
- setError(null);
4781
- setDeeplinkUri(null);
4782
- setMobileFlow(false);
4783
- setStep("deposit");
4784
- } catch (err) {
4785
- handlingMobileReturnRef.current = false;
4786
- setError(
4787
- err instanceof Error ? err.message : "Wallet authorized, but we could not refresh your account yet."
4788
- );
4789
- setStep("open-wallet");
5243
+ const { sourceType, sourceId } = deriveSourceTypeAndId(state);
5244
+ const selectedAccount = state.accounts.find((a) => a.id === state.selectedAccountId);
5245
+ const selectedWallet = selectedAccount?.wallets.find(
5246
+ (w) => w.id === state.selectedWalletId
5247
+ );
5248
+ const sourceName = selectedAccount?.name ?? selectedWallet?.chain.name ?? "Wallet";
5249
+ const sourceAddress = selectedWallet ? `${selectedWallet.name.slice(0, 6)}...${selectedWallet.name.slice(-4)}` : void 0;
5250
+ const sourceVerified = selectedWallet?.status === "ACTIVE";
5251
+ const pendingConnections = react.useMemo(
5252
+ () => state.accounts.filter(
5253
+ (a) => a.wallets.length > 0 && !a.wallets.some((w) => w.status === "ACTIVE")
5254
+ ),
5255
+ [state.accounts]
5256
+ );
5257
+ const maxSourceBalance = react.useMemo(() => {
5258
+ let max = 0;
5259
+ for (const acct of state.accounts) {
5260
+ for (const wallet of acct.wallets) {
5261
+ for (const source of wallet.sources) {
5262
+ if (source.balance.available.amount > max) {
5263
+ max = source.balance.available.amount;
5264
+ }
5265
+ }
4790
5266
  }
4791
- return;
4792
- }
4793
- setTransfer(authorizedTransfer);
4794
- mobileSetupFlowRef.current = false;
4795
- clearMobileFlowState();
4796
- setError(null);
4797
- setDeeplinkUri(null);
4798
- setMobileFlow(false);
4799
- setStep("confirm-sign");
4800
- }, [polling.stopPolling, reloadAccounts, resetDataLoadingState]);
4801
- const handleRetryMobileStatus = react.useCallback(() => {
4802
- setError(null);
4803
- handlingMobileReturnRef.current = false;
4804
- const currentTransfer = polling.transfer ?? transfer;
4805
- if (currentTransfer?.status === "AUTHORIZED") {
4806
- void handleAuthorizedMobileReturn(currentTransfer, mobileSetupFlowRef.current);
4807
- return;
4808
- }
4809
- const transferIdToResume = pollingTransferIdRef.current ?? currentTransfer?.id;
4810
- if (transferIdToResume) {
4811
- polling.startPolling(transferIdToResume);
4812
- }
4813
- }, [handleAuthorizedMobileReturn, polling, transfer]);
4814
- react.useEffect(() => {
4815
- if (depositAmount != null) {
4816
- setAmount(depositAmount.toString());
4817
5267
  }
4818
- }, [depositAmount]);
4819
- const resetHeadlessLogin = react.useCallback(() => {
4820
- setAuthInput("");
4821
- setVerificationTarget(null);
4822
- setOtpCode("");
4823
- }, []);
4824
- react.useCallback(async (provider) => {
4825
- setError(null);
4826
- try {
4827
- await initOAuth({ provider });
4828
- } catch (err) {
4829
- captureException(err);
4830
- setError(err instanceof Error ? err.message : "Social login failed");
5268
+ return max;
5269
+ }, [state.accounts]);
5270
+ const tokenCount = react.useMemo(() => {
5271
+ let count = 0;
5272
+ for (const acct of state.accounts) {
5273
+ for (const wallet of acct.wallets) {
5274
+ count += wallet.sources.length;
5275
+ }
4831
5276
  }
4832
- }, [initOAuth]);
4833
- const activeOtpStatus = verificationTarget?.kind === "email" ? emailLoginState.status : verificationTarget?.kind === "phone" ? smsLoginState.status : "initial";
4834
- const activeOtpErrorMessage = verificationTarget?.kind === "email" && emailLoginState.status === "error" ? emailLoginState.error?.message ?? "Failed to continue with email." : verificationTarget?.kind === "phone" && smsLoginState.status === "error" ? smsLoginState.error?.message ?? "Failed to continue with phone number." : null;
4835
- react.useEffect(() => {
4836
- if (authenticated) return;
4837
- if (activeOtpErrorMessage) setError(activeOtpErrorMessage);
4838
- }, [activeOtpErrorMessage, authenticated]);
5277
+ return count;
5278
+ }, [state.accounts]);
5279
+ const activeOtpStatus = state.verificationTarget?.kind === "email" ? emailLoginState.status : state.verificationTarget?.kind === "phone" ? smsLoginState.status : "initial";
5280
+ const activeOtpErrorMessage = state.verificationTarget?.kind === "email" && emailLoginState.status === "error" ? emailLoginState.error?.message ?? "Failed to continue with email." : state.verificationTarget?.kind === "phone" && smsLoginState.status === "error" ? smsLoginState.error?.message ?? "Failed to continue with phone number." : null;
4839
5281
  const handleSendLoginCode = react.useCallback(async () => {
4840
5282
  const normalizedIdentifier = normalizeAuthIdentifier(authInput);
4841
5283
  if (!normalizedIdentifier) {
4842
- setError("Enter a valid email address or phone number.");
5284
+ dispatch({ type: "SET_ERROR", error: "Enter a valid email address or phone number." });
4843
5285
  return;
4844
5286
  }
4845
- setError(null);
4846
5287
  setOtpCode("");
4847
5288
  try {
4848
5289
  if (normalizedIdentifier.kind === "email") {
@@ -4850,468 +5291,214 @@ function SwypePaymentInner({
4850
5291
  } else {
4851
5292
  await sendSmsCode({ phoneNumber: normalizedIdentifier.value });
4852
5293
  }
4853
- setVerificationTarget(normalizedIdentifier);
4854
- setStep("otp-verify");
5294
+ dispatch({ type: "CODE_SENT", target: normalizedIdentifier });
4855
5295
  } catch (err) {
4856
5296
  captureException(err);
4857
- setError(err instanceof Error ? err.message : "Failed to send verification code");
5297
+ dispatch({
5298
+ type: "SET_ERROR",
5299
+ error: err instanceof Error ? err.message : "Failed to send verification code"
5300
+ });
4858
5301
  }
4859
5302
  }, [authInput, sendEmailCode, sendSmsCode]);
4860
5303
  const handleVerifyLoginCode = react.useCallback(async () => {
4861
- if (!verificationTarget) return;
5304
+ if (!state.verificationTarget) return;
4862
5305
  const trimmedCode = otpCode.trim();
4863
5306
  if (!/^\d{6}$/.test(trimmedCode)) {
4864
- setError("Enter the 6-digit verification code.");
5307
+ dispatch({ type: "SET_ERROR", error: "Enter the 6-digit verification code." });
4865
5308
  return;
4866
5309
  }
4867
- setError(null);
5310
+ dispatch({ type: "SET_ERROR", error: null });
4868
5311
  try {
4869
- if (verificationTarget.kind === "email") {
5312
+ if (state.verificationTarget.kind === "email") {
4870
5313
  await loginWithEmailCode({ code: trimmedCode });
4871
5314
  } else {
4872
5315
  await loginWithSmsCode({ code: trimmedCode });
4873
5316
  }
4874
5317
  } catch (err) {
4875
5318
  captureException(err);
4876
- setError(err instanceof Error ? err.message : "Failed to verify code");
4877
- }
4878
- }, [verificationTarget, otpCode, loginWithEmailCode, loginWithSmsCode]);
4879
- react.useEffect(() => {
4880
- if (step === "otp-verify" && /^\d{6}$/.test(otpCode.trim()) && activeOtpStatus !== "submitting-code") {
4881
- handleVerifyLoginCode();
5319
+ dispatch({
5320
+ type: "SET_ERROR",
5321
+ error: err instanceof Error ? err.message : "Failed to verify code"
5322
+ });
4882
5323
  }
4883
- }, [otpCode, step, activeOtpStatus, handleVerifyLoginCode]);
5324
+ }, [state.verificationTarget, otpCode, loginWithEmailCode, loginWithSmsCode]);
4884
5325
  const handleResendLoginCode = react.useCallback(async () => {
4885
- if (!verificationTarget) return;
4886
- setError(null);
5326
+ if (!state.verificationTarget) return;
5327
+ dispatch({ type: "SET_ERROR", error: null });
4887
5328
  try {
4888
- if (verificationTarget.kind === "email") {
4889
- await sendEmailCode({ email: verificationTarget.value });
5329
+ if (state.verificationTarget.kind === "email") {
5330
+ await sendEmailCode({ email: state.verificationTarget.value });
4890
5331
  } else {
4891
- await sendSmsCode({ phoneNumber: verificationTarget.value });
5332
+ await sendSmsCode({ phoneNumber: state.verificationTarget.value });
4892
5333
  }
4893
5334
  } catch (err) {
4894
5335
  captureException(err);
4895
- setError(err instanceof Error ? err.message : "Failed to resend code");
5336
+ dispatch({
5337
+ type: "SET_ERROR",
5338
+ error: err instanceof Error ? err.message : "Failed to resend code"
5339
+ });
4896
5340
  }
4897
- }, [verificationTarget, sendEmailCode, sendSmsCode]);
4898
- react.useEffect(() => {
4899
- if (!ready || !authenticated) return;
4900
- if (step !== "login" && step !== "otp-verify") return;
4901
- let cancelled = false;
4902
- setError(null);
4903
- resetHeadlessLogin();
4904
- const restoreOrDeposit = async (credId, token) => {
4905
- const persisted = loadMobileFlowState();
4906
- let accts = [];
4907
- try {
4908
- accts = await fetchAccounts(apiBaseUrl, token, credId);
4909
- if (cancelled) return;
4910
- } catch {
5341
+ }, [state.verificationTarget, sendEmailCode, sendSmsCode]);
5342
+ const completePasskeyRegistration = react.useCallback(async (credentialId, publicKey) => {
5343
+ const token = await getAccessToken();
5344
+ if (!token) throw new Error("Not authenticated");
5345
+ await registerPasskey(apiBaseUrl, token, credentialId, publicKey);
5346
+ dispatch({ type: "PASSKEY_ACTIVATED", credentialId });
5347
+ window.localStorage.setItem(ACTIVE_CREDENTIAL_STORAGE_KEY, credentialId);
5348
+ const resolved = resolvePostAuthStep({
5349
+ hasPasskey: true,
5350
+ accounts: state.accounts,
5351
+ persistedMobileFlow: loadMobileFlowState(),
5352
+ mobileSetupInProgress: mobileSetupFlowRef.current,
5353
+ connectingNewAccount: state.connectingNewAccount
5354
+ });
5355
+ if (resolved.clearPersistedFlow) clearMobileFlowState();
5356
+ dispatch({ type: "NAVIGATE", step: resolved.step });
5357
+ }, [getAccessToken, apiBaseUrl, state.accounts, state.connectingNewAccount]);
5358
+ const handleRegisterPasskey = react.useCallback(async () => {
5359
+ dispatch({ type: "SET_REGISTERING_PASSKEY", value: true });
5360
+ dispatch({ type: "SET_ERROR", error: null });
5361
+ try {
5362
+ const passkeyDisplayName = user?.email?.address ?? user?.google?.name ?? user?.id ?? "Swype User";
5363
+ const { credentialId, publicKey } = await createPasskeyCredential({
5364
+ userId: user?.id ?? "unknown",
5365
+ displayName: passkeyDisplayName
5366
+ });
5367
+ await completePasskeyRegistration(credentialId, publicKey);
5368
+ } catch (err) {
5369
+ if (err instanceof PasskeyIframeBlockedError) {
5370
+ dispatch({ type: "SET_PASSKEY_POPUP_NEEDED", needed: true });
5371
+ } else {
5372
+ captureException(err);
5373
+ dispatch({
5374
+ type: "SET_ERROR",
5375
+ error: err instanceof Error ? err.message : "Failed to register passkey"
5376
+ });
4911
5377
  }
5378
+ } finally {
5379
+ dispatch({ type: "SET_REGISTERING_PASSKEY", value: false });
5380
+ }
5381
+ }, [user, completePasskeyRegistration]);
5382
+ const handleCreatePasskeyViaPopup = react.useCallback(async () => {
5383
+ dispatch({ type: "SET_REGISTERING_PASSKEY", value: true });
5384
+ dispatch({ type: "SET_ERROR", error: null });
5385
+ try {
5386
+ const token = await getAccessToken();
5387
+ const passkeyDisplayName = user?.email?.address ?? user?.google?.name ?? user?.id ?? "Swype User";
5388
+ const popupOptions = buildPasskeyPopupOptions({
5389
+ userId: user?.id ?? "unknown",
5390
+ displayName: passkeyDisplayName,
5391
+ authToken: token ?? void 0,
5392
+ apiBaseUrl
5393
+ });
5394
+ const { credentialId } = await createPasskeyViaPopup(popupOptions);
5395
+ dispatch({ type: "PASSKEY_ACTIVATED", credentialId });
5396
+ localStorage.setItem(ACTIVE_CREDENTIAL_STORAGE_KEY, credentialId);
4912
5397
  const resolved = resolvePostAuthStep({
4913
5398
  hasPasskey: true,
4914
- accounts: accts,
4915
- persistedMobileFlow: persisted,
4916
- mobileSetupInProgress: false,
4917
- connectingNewAccount: false
5399
+ accounts: state.accounts,
5400
+ persistedMobileFlow: loadMobileFlowState(),
5401
+ mobileSetupInProgress: mobileSetupFlowRef.current,
5402
+ connectingNewAccount: state.connectingNewAccount
4918
5403
  });
4919
- if (resolved.clearPersistedFlow) {
4920
- clearMobileFlowState();
4921
- }
4922
- if (resolved.step === "deposit" && persisted && persisted.isSetup) {
4923
- try {
4924
- const existingTransfer = await fetchTransfer(apiBaseUrl, token, persisted.transferId);
4925
- if (cancelled) return;
4926
- if (existingTransfer.status === "AUTHORIZED") {
4927
- await handleAuthorizedMobileReturn(existingTransfer, true);
4928
- return;
4929
- }
4930
- } catch {
4931
- }
4932
- }
4933
- if (resolved.step === "open-wallet" && persisted) {
4934
- try {
4935
- const existingTransfer = await fetchTransfer(apiBaseUrl, token, persisted.transferId);
4936
- if (cancelled) return;
4937
- const mobileResolution = resolveRestoredMobileFlow(
4938
- existingTransfer.status,
4939
- persisted.isSetup
4940
- );
4941
- if (mobileResolution.kind === "resume-setup-deposit") {
4942
- await handleAuthorizedMobileReturn(existingTransfer, true);
4943
- return;
4944
- }
4945
- if (mobileResolution.kind === "resume-confirm-sign") {
4946
- await handleAuthorizedMobileReturn(existingTransfer, false);
4947
- return;
4948
- }
4949
- if (mobileResolution.kind === "resume-success") {
4950
- clearMobileFlowState();
4951
- setMobileFlow(false);
4952
- setDeeplinkUri(null);
4953
- setTransfer(existingTransfer);
4954
- setError(null);
4955
- setStep("success");
4956
- onComplete?.(existingTransfer);
4957
- return;
4958
- }
4959
- if (mobileResolution.kind === "resume-failed") {
4960
- clearMobileFlowState();
4961
- setMobileFlow(false);
4962
- setDeeplinkUri(null);
4963
- setTransfer(existingTransfer);
4964
- setError("Transfer failed.");
4965
- setStep("success");
4966
- return;
4967
- }
4968
- if (mobileResolution.kind === "resume-processing") {
4969
- clearMobileFlowState();
4970
- setMobileFlow(false);
4971
- setDeeplinkUri(null);
4972
- setTransfer(existingTransfer);
4973
- setError(null);
4974
- setStep("processing");
4975
- polling.startPolling(existingTransfer.id);
4976
- return;
4977
- }
4978
- if (mobileResolution.kind === "resume-stale-setup") {
4979
- clearMobileFlowState();
4980
- if (!cancelled) setStep("wallet-picker");
4981
- return;
4982
- }
4983
- } catch (err) {
4984
- if (cancelled) return;
4985
- enterPersistedMobileFlow(
4986
- persisted,
4987
- err instanceof Error ? err.message : "Unable to refresh wallet authorization status."
4988
- );
4989
- return;
4990
- }
4991
- enterPersistedMobileFlow(persisted);
4992
- return;
4993
- }
4994
- setStep(resolved.step);
4995
- };
4996
- const checkPasskey = async () => {
4997
- try {
4998
- const token = await getAccessToken();
4999
- if (!token || cancelled) return;
5000
- const { config } = await fetchUserConfig(apiBaseUrl, token);
5001
- if (cancelled) return;
5002
- if (config.defaultAllowance != null) {
5003
- setOneTapLimit(config.defaultAllowance);
5004
- }
5005
- const allPasskeys = config.passkeys ?? (config.passkey ? [config.passkey] : []);
5006
- setKnownCredentialIds(allPasskeys.map((p) => p.credentialId));
5007
- if (allPasskeys.length === 0) {
5008
- setStep("create-passkey");
5009
- return;
5010
- }
5011
- if (activeCredentialId && allPasskeys.some((p) => p.credentialId === activeCredentialId)) {
5012
- await restoreOrDeposit(activeCredentialId, token);
5013
- return;
5014
- }
5015
- if (cancelled) return;
5016
- const credentialIds = allPasskeys.map((p) => p.credentialId);
5017
- if (isSafari() && isInCrossOriginIframe()) {
5018
- setStep("verify-passkey");
5019
- return;
5020
- }
5021
- const matched = await findDevicePasskey(credentialIds);
5022
- if (cancelled) return;
5023
- if (matched) {
5024
- setActiveCredentialId(matched);
5025
- window.localStorage.setItem(ACTIVE_CREDENTIAL_STORAGE_KEY, matched);
5404
+ if (resolved.clearPersistedFlow) clearMobileFlowState();
5405
+ dispatch({ type: "NAVIGATE", step: resolved.step });
5406
+ } catch (err) {
5407
+ captureException(err);
5408
+ dispatch({
5409
+ type: "SET_ERROR",
5410
+ error: err instanceof Error ? err.message : "Failed to register passkey"
5411
+ });
5412
+ } finally {
5413
+ dispatch({ type: "SET_REGISTERING_PASSKEY", value: false });
5414
+ }
5415
+ }, [user, getAccessToken, apiBaseUrl, state.accounts, state.connectingNewAccount]);
5416
+ const handleVerifyPasskeyViaPopup = react.useCallback(async () => {
5417
+ dispatch({ type: "SET_VERIFYING_PASSKEY", value: true });
5418
+ dispatch({ type: "SET_ERROR", error: null });
5419
+ try {
5420
+ const token = await getAccessToken();
5421
+ const matched = await findDevicePasskeyViaPopup({
5422
+ credentialIds: state.knownCredentialIds,
5423
+ rpId: resolvePasskeyRpId(),
5424
+ authToken: token ?? void 0,
5425
+ apiBaseUrl
5426
+ });
5427
+ if (matched) {
5428
+ dispatch({ type: "PASSKEY_ACTIVATED", credentialId: matched });
5429
+ window.localStorage.setItem(ACTIVE_CREDENTIAL_STORAGE_KEY, matched);
5430
+ if (token) {
5026
5431
  reportPasskeyActivity(apiBaseUrl, token, matched).catch(() => {
5027
5432
  });
5028
- await restoreOrDeposit(matched, token);
5029
- return;
5030
5433
  }
5031
- setStep("create-passkey");
5032
- } catch {
5033
- if (!cancelled) setStep("create-passkey");
5434
+ dispatch({ type: "NAVIGATE", step: "login" });
5435
+ } else {
5436
+ dispatch({ type: "NAVIGATE", step: "create-passkey" });
5034
5437
  }
5035
- };
5036
- checkPasskey();
5037
- return () => {
5038
- cancelled = true;
5039
- };
5040
- }, [
5041
- ready,
5042
- authenticated,
5043
- step,
5044
- apiBaseUrl,
5045
- getAccessToken,
5046
- activeCredentialId,
5047
- resetHeadlessLogin,
5048
- enterPersistedMobileFlow,
5049
- handleAuthorizedMobileReturn,
5050
- onComplete
5051
- ]);
5052
- react.useEffect(() => {
5053
- const loadAction = resolveDataLoadAction({
5054
- authenticated,
5055
- step,
5056
- accountsCount: accounts.length,
5057
- hasActiveCredential: !!activeCredentialId,
5058
- loading: loadingDataRef.current
5059
- });
5060
- if (loadAction === "reset") {
5061
- resetDataLoadingState();
5062
- return;
5063
- }
5064
- if (loadAction === "wait") {
5065
- return;
5066
- }
5067
- const credentialId = activeCredentialId;
5068
- if (!credentialId) {
5069
- resetDataLoadingState();
5070
- return;
5438
+ } catch {
5439
+ dispatch({ type: "NAVIGATE", step: "create-passkey" });
5440
+ } finally {
5441
+ dispatch({ type: "SET_VERIFYING_PASSKEY", value: false });
5071
5442
  }
5072
- let cancelled = false;
5073
- loadingDataRef.current = true;
5074
- const load = async () => {
5075
- setLoadingData(true);
5076
- setError(null);
5443
+ }, [state.knownCredentialIds, getAccessToken, apiBaseUrl]);
5444
+ const reloadAccounts = react.useCallback(async () => {
5445
+ const token = await getAccessToken();
5446
+ if (!token || !state.activeCredentialId) return;
5447
+ const [accts, prov] = await Promise.all([
5448
+ fetchAccounts(apiBaseUrl, token, state.activeCredentialId),
5449
+ fetchProviders(apiBaseUrl, token)
5450
+ ]);
5451
+ const parsedAmt = depositAmount != null ? depositAmount : 0;
5452
+ const defaults = computeSmartDefaults(accts, parsedAmt);
5453
+ dispatch({ type: "ACCOUNTS_RELOADED", accounts: accts, providers: prov, defaults });
5454
+ }, [getAccessToken, state.activeCredentialId, apiBaseUrl, depositAmount]);
5455
+ const handleAuthorizedMobileReturn = react.useCallback(async (authorizedTransfer, isSetup) => {
5456
+ if (handlingMobileReturnRef.current) return;
5457
+ handlingMobileReturnRef.current = true;
5458
+ polling.stopPolling();
5459
+ if (isSetup) {
5460
+ mobileSetupFlowRef.current = false;
5461
+ clearMobileFlowState();
5077
5462
  try {
5078
- const token = await getAccessToken();
5079
- if (!token) throw new Error("Not authenticated");
5080
- const [prov, accts, chn] = await Promise.all([
5081
- fetchProviders(apiBaseUrl, token),
5082
- fetchAccounts(apiBaseUrl, token, credentialId),
5083
- fetchChains(apiBaseUrl, token)
5084
- ]);
5085
- if (cancelled) return;
5086
- setProviders(prov);
5087
- setAccounts(accts);
5088
- setChains(chn);
5089
- const parsedAmt = depositAmount != null ? depositAmount : 0;
5090
- const defaults = computeSmartDefaults(accts, parsedAmt);
5091
- if (defaults) {
5092
- setSelectedAccountId(defaults.accountId);
5093
- setSelectedWalletId(defaults.walletId);
5094
- } else if (prov.length > 0 && !connectingNewAccount) {
5095
- setSelectedProviderId(prov[0].id);
5096
- }
5097
- const persisted = loadMobileFlowState();
5098
- const resolved = resolvePostAuthStep({
5099
- hasPasskey: !!activeCredentialId,
5100
- accounts: accts,
5101
- persistedMobileFlow: persisted,
5102
- mobileSetupInProgress: mobileSetupFlowRef.current,
5103
- connectingNewAccount
5104
- });
5105
- if (resolved.clearPersistedFlow) {
5106
- clearMobileFlowState();
5107
- setMobileFlow(false);
5108
- setDeeplinkUri(null);
5109
- }
5110
- const correctableSteps = ["deposit", "wallet-picker", "open-wallet"];
5111
- if (correctableSteps.includes(step)) {
5112
- setStep(resolved.step);
5113
- }
5463
+ await reloadAccounts();
5464
+ loadingDataRef.current = false;
5465
+ dispatch({ type: "MOBILE_SETUP_COMPLETE", transfer: authorizedTransfer });
5114
5466
  } catch (err) {
5115
- if (!cancelled) {
5116
- captureException(err);
5117
- setError(err instanceof Error ? err.message : "Failed to load data");
5118
- }
5119
- } finally {
5120
- if (!cancelled) {
5121
- resetDataLoadingState();
5122
- }
5123
- }
5124
- };
5125
- load();
5126
- return () => {
5127
- cancelled = true;
5128
- loadingDataRef.current = false;
5129
- };
5130
- }, [authenticated, step, accounts.length, apiBaseUrl, getAccessToken, activeCredentialId, depositAmount, connectingNewAccount, resetDataLoadingState]);
5131
- react.useEffect(() => {
5132
- if (!polling.transfer) return;
5133
- if (polling.transfer.status === "COMPLETED") {
5134
- clearMobileFlowState();
5135
- setStep("success");
5136
- setTransfer(polling.transfer);
5137
- onComplete?.(polling.transfer);
5138
- } else if (polling.transfer.status === "FAILED") {
5139
- clearMobileFlowState();
5140
- setStep("success");
5141
- setTransfer(polling.transfer);
5142
- setError("Transfer failed.");
5143
- }
5144
- }, [polling.transfer, onComplete]);
5145
- react.useEffect(() => {
5146
- if (step !== "processing") {
5147
- processingStartedAtRef.current = null;
5148
- return;
5149
- }
5150
- if (!processingStartedAtRef.current) {
5151
- processingStartedAtRef.current = Date.now();
5152
- }
5153
- const elapsedMs = Date.now() - processingStartedAtRef.current;
5154
- const remainingMs = PROCESSING_TIMEOUT_MS - elapsedMs;
5155
- const handleTimeout = () => {
5156
- if (!hasProcessingTimedOut(processingStartedAtRef.current, Date.now())) return;
5157
- const status = getTransferStatus(polling.transfer, transfer);
5158
- const msg = buildProcessingTimeoutMessage(status);
5159
- captureException(new Error(msg));
5160
- polling.stopPolling();
5161
- setStep("deposit");
5162
- setError(msg);
5163
- onError?.(msg);
5164
- };
5165
- if (remainingMs <= 0) {
5166
- handleTimeout();
5167
- return;
5168
- }
5169
- const timeoutId = window.setTimeout(handleTimeout, remainingMs);
5170
- return () => window.clearTimeout(timeoutId);
5171
- }, [step, polling.transfer, transfer, polling.stopPolling, onError]);
5172
- react.useEffect(() => {
5173
- if (!mobileFlow) {
5174
- handlingMobileReturnRef.current = false;
5175
- return;
5176
- }
5177
- if (handlingMobileReturnRef.current) return;
5178
- const polledTransfer = polling.transfer;
5179
- if (!polledTransfer || polledTransfer.status !== "AUTHORIZED") return;
5180
- void handleAuthorizedMobileReturn(polledTransfer, mobileSetupFlowRef.current);
5181
- }, [mobileFlow, polling.transfer, handleAuthorizedMobileReturn]);
5182
- react.useEffect(() => {
5183
- if (!mobileFlow) return;
5184
- if (handlingMobileReturnRef.current) return;
5185
- const transferIdToResume = pollingTransferIdRef.current ?? transfer?.id;
5186
- if (!transferIdToResume) return;
5187
- if (!polling.isPolling) polling.startPolling(transferIdToResume);
5188
- const handleVisibility = () => {
5189
- if (document.visibilityState === "visible" && !handlingMobileReturnRef.current) {
5190
- polling.startPolling(transferIdToResume);
5467
+ handlingMobileReturnRef.current = false;
5468
+ dispatch({
5469
+ type: "SET_ERROR",
5470
+ error: err instanceof Error ? err.message : "Wallet authorized, but we could not refresh your account yet."
5471
+ });
5472
+ dispatch({ type: "NAVIGATE", step: "open-wallet" });
5191
5473
  }
5192
- };
5193
- document.addEventListener("visibilitychange", handleVisibility);
5194
- return () => document.removeEventListener("visibilitychange", handleVisibility);
5195
- }, [mobileFlow, transfer?.id, polling.isPolling, polling.startPolling]);
5196
- const pendingSelectSourceAction = authExecutor.pendingSelectSource;
5197
- const selectSourceChoices = react.useMemo(() => {
5198
- if (!pendingSelectSourceAction) return [];
5199
- const options = pendingSelectSourceAction.metadata?.options ?? [];
5200
- return buildSelectSourceChoices(options);
5201
- }, [pendingSelectSourceAction]);
5202
- const selectSourceRecommended = react.useMemo(() => {
5203
- if (!pendingSelectSourceAction) return null;
5204
- return pendingSelectSourceAction.metadata?.recommended ?? null;
5205
- }, [pendingSelectSourceAction]);
5206
- react.useEffect(() => {
5207
- if (!pendingSelectSourceAction) {
5208
- initializedSelectSourceActionRef.current = null;
5209
- setSelectSourceChainName("");
5210
- setSelectSourceTokenSymbol("");
5211
5474
  return;
5212
5475
  }
5213
- if (initializedSelectSourceActionRef.current === pendingSelectSourceAction.id) return;
5214
- const hasRecommended = !!selectSourceRecommended && selectSourceChoices.some(
5215
- (chain) => chain.chainName === selectSourceRecommended.chainName && chain.tokens.some((t) => t.tokenSymbol === selectSourceRecommended.tokenSymbol)
5216
- );
5217
- if (hasRecommended && selectSourceRecommended) {
5218
- setSelectSourceChainName(selectSourceRecommended.chainName);
5219
- setSelectSourceTokenSymbol(selectSourceRecommended.tokenSymbol);
5220
- } else if (selectSourceChoices.length > 0 && selectSourceChoices[0].tokens.length > 0) {
5221
- setSelectSourceChainName(selectSourceChoices[0].chainName);
5222
- setSelectSourceTokenSymbol(selectSourceChoices[0].tokens[0].tokenSymbol);
5223
- } else {
5224
- setSelectSourceChainName("Base");
5225
- setSelectSourceTokenSymbol("USDC");
5226
- }
5227
- initializedSelectSourceActionRef.current = pendingSelectSourceAction.id;
5228
- }, [pendingSelectSourceAction, selectSourceChoices, selectSourceRecommended]);
5229
- react.useEffect(() => {
5230
- if (pendingSelectSourceAction && step === "processing") {
5231
- preSelectSourceStepRef.current = step;
5232
- setStep("select-source");
5233
- } else if (!pendingSelectSourceAction && step === "select-source") {
5234
- setStep(preSelectSourceStepRef.current ?? "processing");
5235
- preSelectSourceStepRef.current = null;
5236
- }
5237
- }, [pendingSelectSourceAction, step]);
5238
- const handleSelectSourceChainChange = react.useCallback(
5239
- (chainName) => {
5240
- setSelectSourceChainName(chainName);
5241
- const chain = selectSourceChoices.find((c) => c.chainName === chainName);
5242
- if (!chain || chain.tokens.length === 0) return;
5243
- const recommendedToken = selectSourceRecommended?.chainName === chainName ? selectSourceRecommended.tokenSymbol : null;
5244
- const hasRecommended = !!recommendedToken && chain.tokens.some((t) => t.tokenSymbol === recommendedToken);
5245
- setSelectSourceTokenSymbol(
5246
- hasRecommended ? recommendedToken : chain.tokens[0].tokenSymbol
5247
- );
5248
- },
5249
- [selectSourceChoices, selectSourceRecommended]
5250
- );
5251
- const pendingConnections = react.useMemo(
5252
- () => accounts.filter(
5253
- (a) => a.wallets.length > 0 && !a.wallets.some((w) => w.status === "ACTIVE")
5254
- ),
5255
- [accounts]
5256
- );
5257
- const selectedAccount = accounts.find((a) => a.id === selectedAccountId);
5258
- const selectedWallet = selectedAccount?.wallets.find((w) => w.id === selectedWalletId);
5259
- const sourceName = selectedAccount?.name ?? selectedWallet?.chain.name ?? "Wallet";
5260
- const sourceAddress = selectedWallet ? `${selectedWallet.name.slice(0, 6)}...${selectedWallet.name.slice(-4)}` : void 0;
5261
- const sourceVerified = selectedWallet?.status === "ACTIVE";
5262
- const maxSourceBalance = react.useMemo(() => {
5263
- let max = 0;
5264
- for (const acct of accounts) {
5265
- for (const wallet of acct.wallets) {
5266
- for (const source of wallet.sources) {
5267
- if (source.balance.available.amount > max) {
5268
- max = source.balance.available.amount;
5269
- }
5270
- }
5271
- }
5272
- }
5273
- return max;
5274
- }, [accounts]);
5275
- const tokenCount = react.useMemo(() => {
5276
- let count = 0;
5277
- for (const acct of accounts) {
5278
- for (const wallet of acct.wallets) {
5279
- count += wallet.sources.length;
5280
- }
5281
- }
5282
- return count;
5283
- }, [accounts]);
5284
- const handlePay = react.useCallback(async (depositAmount2, sourceOverrides) => {
5285
- const parsedAmount = depositAmount2;
5286
- if (isNaN(parsedAmount) || parsedAmount < MIN_SEND_AMOUNT_USD) {
5287
- setError(`Minimum amount is $${MIN_SEND_AMOUNT_USD.toFixed(2)}.`);
5476
+ mobileSetupFlowRef.current = false;
5477
+ clearMobileFlowState();
5478
+ dispatch({ type: "MOBILE_SIGN_READY", transfer: authorizedTransfer });
5479
+ }, [polling.stopPolling, reloadAccounts]);
5480
+ const handlePay = react.useCallback(async (payAmount, sourceOverrides) => {
5481
+ if (isNaN(payAmount) || payAmount < MIN_SEND_AMOUNT_USD) {
5482
+ dispatch({ type: "SET_ERROR", error: `Minimum amount is $${MIN_SEND_AMOUNT_USD.toFixed(2)}.` });
5288
5483
  return;
5289
5484
  }
5290
5485
  if (!sourceOverrides?.sourceId && !sourceId) {
5291
- setError("No account or provider selected.");
5486
+ dispatch({ type: "SET_ERROR", error: "No account or provider selected." });
5292
5487
  return;
5293
5488
  }
5294
- if (!activeCredentialId) {
5295
- setError("Create a passkey on this device before continuing.");
5296
- setStep("create-passkey");
5489
+ if (!state.activeCredentialId) {
5490
+ dispatch({ type: "SET_ERROR", error: "Create a passkey on this device before continuing." });
5491
+ dispatch({ type: "NAVIGATE", step: "create-passkey" });
5297
5492
  return;
5298
5493
  }
5299
5494
  const isSetupRedirect = mobileSetupFlowRef.current;
5300
- if (isSetupRedirect) {
5301
- setStep("open-wallet");
5302
- } else {
5303
- setStep("processing");
5304
- }
5495
+ dispatch({ type: "PAY_STARTED", isSetupRedirect });
5305
5496
  processingStartedAtRef.current = Date.now();
5306
- setError(null);
5307
- setCreatingTransfer(true);
5308
- setDeeplinkUri(null);
5309
- setMobileFlow(false);
5310
5497
  try {
5311
- if (transfer?.status === "AUTHORIZED") {
5312
- const signedTransfer2 = await transferSigning.signTransfer(transfer.id);
5313
- setTransfer(signedTransfer2);
5314
- polling.startPolling(transfer.id);
5498
+ if (state.transfer?.status === "AUTHORIZED") {
5499
+ const signedTransfer2 = await transferSigning.signTransfer(state.transfer.id);
5500
+ dispatch({ type: "TRANSFER_SIGNED", transfer: signedTransfer2 });
5501
+ polling.startPolling(state.transfer.id);
5315
5502
  return;
5316
5503
  }
5317
5504
  const token = await getAccessToken();
@@ -5319,21 +5506,21 @@ function SwypePaymentInner({
5319
5506
  let effectiveSourceType = sourceOverrides?.sourceType ?? sourceType;
5320
5507
  let effectiveSourceId = sourceOverrides?.sourceId ?? sourceId;
5321
5508
  if (effectiveSourceType === "accountId") {
5322
- const acct = accounts.find((a) => a.id === effectiveSourceId);
5509
+ const acct = state.accounts.find((a) => a.id === effectiveSourceId);
5323
5510
  const activeWallet = acct?.wallets.find((w) => w.status === "ACTIVE");
5324
5511
  if (activeWallet) {
5325
5512
  effectiveSourceType = "walletId";
5326
5513
  effectiveSourceId = activeWallet.id;
5327
5514
  }
5328
5515
  }
5329
- const isActiveWallet = effectiveSourceType === "walletId" && accounts.some(
5516
+ const isActiveWallet = effectiveSourceType === "walletId" && state.accounts.some(
5330
5517
  (a) => a.wallets.some((w) => w.id === effectiveSourceId && w.status === "ACTIVE")
5331
5518
  );
5332
5519
  if (!isActiveWallet && !isSetupRedirect) {
5333
5520
  let found = false;
5334
- for (const acct of accounts) {
5521
+ for (const acct of state.accounts) {
5335
5522
  for (const wallet of acct.wallets) {
5336
- if (wallet.status === "ACTIVE" && wallet.sources.some((s) => s.balance.available.amount >= parsedAmount)) {
5523
+ if (wallet.status === "ACTIVE" && wallet.sources.some((s) => s.balance.available.amount >= payAmount)) {
5337
5524
  effectiveSourceType = "walletId";
5338
5525
  effectiveSourceId = wallet.id;
5339
5526
  found = true;
@@ -5345,30 +5532,28 @@ function SwypePaymentInner({
5345
5532
  }
5346
5533
  const t = await createTransfer(apiBaseUrl, token, {
5347
5534
  id: idempotencyKey,
5348
- credentialId: activeCredentialId,
5535
+ credentialId: state.activeCredentialId,
5349
5536
  merchantAuthorization,
5350
5537
  sourceType: effectiveSourceType,
5351
5538
  sourceId: effectiveSourceId,
5352
5539
  destination,
5353
- amount: parsedAmount
5540
+ amount: payAmount
5354
5541
  });
5355
- setTransfer(t);
5542
+ dispatch({ type: "TRANSFER_CREATED", transfer: t });
5356
5543
  if (t.authorizationSessions && t.authorizationSessions.length > 0) {
5357
- const shouldUseConnector = shouldUseWalletConnector({
5358
- useWalletConnector,
5544
+ const useConnector = shouldUseWalletConnector({
5545
+ useWalletConnector: useWalletConnectorProp,
5359
5546
  userAgent: typeof navigator === "undefined" ? void 0 : navigator.userAgent
5360
5547
  });
5361
- if (!shouldUseConnector) {
5548
+ if (!useConnector) {
5362
5549
  const uri = t.authorizationSessions[0].uri;
5363
- setMobileFlow(true);
5364
5550
  pollingTransferIdRef.current = t.id;
5365
5551
  polling.startPolling(t.id);
5366
- setDeeplinkUri(uri);
5367
- setStep("open-wallet");
5552
+ dispatch({ type: "MOBILE_DEEPLINK_READY", deeplinkUri: uri });
5368
5553
  persistMobileFlowState({
5369
5554
  transferId: t.id,
5370
5555
  deeplinkUri: uri,
5371
- providerId: sourceOverrides?.sourceType === "providerId" ? sourceOverrides.sourceId : selectedProviderId,
5556
+ providerId: sourceOverrides?.sourceType === "providerId" ? sourceOverrides.sourceId : state.selectedProviderId,
5372
5557
  isSetup: mobileSetupFlowRef.current
5373
5558
  });
5374
5559
  triggerDeeplink(uri);
@@ -5378,55 +5563,58 @@ function SwypePaymentInner({
5378
5563
  }
5379
5564
  }
5380
5565
  const signedTransfer = await transferSigning.signTransfer(t.id);
5381
- setTransfer(signedTransfer);
5566
+ dispatch({ type: "TRANSFER_SIGNED", transfer: signedTransfer });
5382
5567
  polling.startPolling(t.id);
5383
5568
  } catch (err) {
5384
5569
  captureException(err);
5385
5570
  const msg = err instanceof Error ? err.message : "Transfer failed";
5386
- setError(msg);
5571
+ dispatch({
5572
+ type: "PAY_ERROR",
5573
+ error: msg,
5574
+ fallbackStep: isSetupRedirect ? "wallet-picker" : "deposit"
5575
+ });
5387
5576
  onError?.(msg);
5388
- setStep(isSetupRedirect ? "wallet-picker" : "deposit");
5389
5577
  } finally {
5390
- setCreatingTransfer(false);
5578
+ dispatch({ type: "PAY_ENDED" });
5391
5579
  }
5392
5580
  }, [
5393
5581
  sourceId,
5394
5582
  sourceType,
5395
- activeCredentialId,
5583
+ state.activeCredentialId,
5584
+ state.transfer,
5585
+ state.accounts,
5586
+ state.selectedProviderId,
5396
5587
  destination,
5397
5588
  apiBaseUrl,
5398
5589
  getAccessToken,
5399
- accounts,
5400
5590
  authExecutor,
5401
5591
  transferSigning,
5402
5592
  polling,
5403
5593
  onError,
5404
- useWalletConnector,
5594
+ useWalletConnectorProp,
5405
5595
  idempotencyKey,
5406
- merchantAuthorization,
5407
- transfer
5596
+ merchantAuthorization
5408
5597
  ]);
5409
- const [increasingLimit, setIncreasingLimit] = react.useState(false);
5410
5598
  const handleIncreaseLimit = react.useCallback(async () => {
5411
5599
  const parsedAmount = depositAmount ?? 5;
5412
5600
  if (!sourceId) {
5413
- setError("No account or provider selected.");
5601
+ dispatch({ type: "SET_ERROR", error: "No account or provider selected." });
5414
5602
  return;
5415
5603
  }
5416
- if (!activeCredentialId) {
5417
- setError("Create a passkey on this device before continuing.");
5418
- setStep("create-passkey");
5604
+ if (!state.activeCredentialId) {
5605
+ dispatch({ type: "SET_ERROR", error: "Create a passkey on this device before continuing." });
5606
+ dispatch({ type: "NAVIGATE", step: "create-passkey" });
5419
5607
  return;
5420
5608
  }
5421
- setError(null);
5422
- setIncreasingLimit(true);
5609
+ dispatch({ type: "SET_ERROR", error: null });
5610
+ dispatch({ type: "SET_INCREASING_LIMIT", value: true });
5423
5611
  try {
5424
5612
  const token = await getAccessToken();
5425
5613
  if (!token) throw new Error("Not authenticated");
5426
5614
  let effectiveSourceType = sourceType;
5427
5615
  let effectiveSourceId = sourceId;
5428
5616
  if (effectiveSourceType === "accountId") {
5429
- const acct = accounts.find((a) => a.id === effectiveSourceId);
5617
+ const acct = state.accounts.find((a) => a.id === effectiveSourceId);
5430
5618
  const activeWallet = acct?.wallets.find((w) => w.status === "ACTIVE");
5431
5619
  if (activeWallet) {
5432
5620
  effectiveSourceType = "walletId";
@@ -5435,198 +5623,118 @@ function SwypePaymentInner({
5435
5623
  }
5436
5624
  const t = await createTransfer(apiBaseUrl, token, {
5437
5625
  id: idempotencyKey,
5438
- credentialId: activeCredentialId,
5626
+ credentialId: state.activeCredentialId,
5439
5627
  merchantAuthorization,
5440
5628
  sourceType: effectiveSourceType,
5441
5629
  sourceId: effectiveSourceId,
5442
5630
  destination,
5443
5631
  amount: parsedAmount
5444
5632
  });
5445
- setTransfer(t);
5446
5633
  if (t.authorizationSessions && t.authorizationSessions.length > 0) {
5447
5634
  const uri = t.authorizationSessions[0].uri;
5448
- setMobileFlow(true);
5449
5635
  pollingTransferIdRef.current = t.id;
5450
5636
  mobileSetupFlowRef.current = true;
5451
5637
  handlingMobileReturnRef.current = false;
5452
5638
  polling.startPolling(t.id);
5453
- setDeeplinkUri(uri);
5639
+ dispatch({ type: "INCREASE_LIMIT_DEEPLINK", transfer: t, deeplinkUri: uri });
5454
5640
  persistMobileFlowState({
5455
5641
  transferId: t.id,
5456
5642
  deeplinkUri: uri,
5457
- providerId: selectedProviderId,
5643
+ providerId: state.selectedProviderId,
5458
5644
  isSetup: true
5459
5645
  });
5460
5646
  triggerDeeplink(uri);
5647
+ } else {
5648
+ dispatch({ type: "TRANSFER_CREATED", transfer: t });
5461
5649
  }
5462
5650
  } catch (err) {
5463
5651
  captureException(err);
5464
5652
  const msg = err instanceof Error ? err.message : "Failed to increase limit";
5465
- setError(msg);
5653
+ dispatch({ type: "SET_ERROR", error: msg });
5466
5654
  onError?.(msg);
5467
5655
  } finally {
5468
- setIncreasingLimit(false);
5656
+ dispatch({ type: "SET_INCREASING_LIMIT", value: false });
5469
5657
  }
5470
5658
  }, [
5471
5659
  depositAmount,
5472
5660
  sourceId,
5473
5661
  sourceType,
5474
- activeCredentialId,
5662
+ state.activeCredentialId,
5663
+ state.accounts,
5664
+ state.selectedProviderId,
5475
5665
  apiBaseUrl,
5476
5666
  getAccessToken,
5477
- accounts,
5478
5667
  polling,
5479
5668
  onError,
5480
5669
  idempotencyKey,
5481
5670
  merchantAuthorization,
5482
- destination,
5483
- selectedProviderId
5671
+ destination
5484
5672
  ]);
5485
- const completePasskeyRegistration = react.useCallback(async (credentialId, publicKey) => {
5486
- const token = await getAccessToken();
5487
- if (!token) throw new Error("Not authenticated");
5488
- await registerPasskey(apiBaseUrl, token, credentialId, publicKey);
5489
- setActiveCredentialId(credentialId);
5490
- window.localStorage.setItem(ACTIVE_CREDENTIAL_STORAGE_KEY, credentialId);
5491
- setPasskeyPopupNeeded(false);
5492
- const resolved = resolvePostAuthStep({
5493
- hasPasskey: true,
5494
- accounts,
5495
- persistedMobileFlow: loadMobileFlowState(),
5496
- mobileSetupInProgress: mobileSetupFlowRef.current,
5497
- connectingNewAccount
5498
- });
5499
- if (resolved.clearPersistedFlow) {
5500
- clearMobileFlowState();
5501
- }
5502
- setStep(resolved.step);
5503
- }, [getAccessToken, apiBaseUrl, accounts, connectingNewAccount]);
5504
- const handleRegisterPasskey = react.useCallback(async () => {
5505
- setRegisteringPasskey(true);
5506
- setError(null);
5507
- try {
5508
- const passkeyDisplayName = user?.email?.address ?? user?.google?.name ?? user?.id ?? "Swype User";
5509
- const { credentialId, publicKey } = await createPasskeyCredential({
5510
- userId: user?.id ?? "unknown",
5511
- displayName: passkeyDisplayName
5512
- });
5513
- await completePasskeyRegistration(credentialId, publicKey);
5514
- } catch (err) {
5515
- if (err instanceof PasskeyIframeBlockedError) {
5516
- setPasskeyPopupNeeded(true);
5517
- } else {
5518
- captureException(err);
5519
- setError(err instanceof Error ? err.message : "Failed to register passkey");
5520
- }
5521
- } finally {
5522
- setRegisteringPasskey(false);
5523
- }
5524
- }, [user, completePasskeyRegistration]);
5525
- const handleCreatePasskeyViaPopup = react.useCallback(async () => {
5526
- setRegisteringPasskey(true);
5527
- setError(null);
5673
+ const handleConfirmSign = react.useCallback(async () => {
5674
+ const t = state.transfer ?? polling.transfer;
5675
+ if (!t) return;
5528
5676
  try {
5529
- const token = await getAccessToken();
5530
- const passkeyDisplayName = user?.email?.address ?? user?.google?.name ?? user?.id ?? "Swype User";
5531
- const popupOptions = buildPasskeyPopupOptions({
5532
- userId: user?.id ?? "unknown",
5533
- displayName: passkeyDisplayName,
5534
- authToken: token ?? void 0,
5535
- apiBaseUrl
5536
- });
5537
- const { credentialId } = await createPasskeyViaPopup(popupOptions);
5538
- setActiveCredentialId(credentialId);
5539
- localStorage.setItem(ACTIVE_CREDENTIAL_STORAGE_KEY, credentialId);
5540
- setPasskeyPopupNeeded(false);
5541
- const resolved = resolvePostAuthStep({
5542
- hasPasskey: true,
5543
- accounts,
5544
- persistedMobileFlow: loadMobileFlowState(),
5545
- mobileSetupInProgress: mobileSetupFlowRef.current,
5546
- connectingNewAccount
5547
- });
5548
- if (resolved.clearPersistedFlow) {
5549
- clearMobileFlowState();
5550
- }
5551
- setStep(resolved.step);
5677
+ const signedTransfer = await transferSigning.signTransfer(t.id);
5678
+ clearMobileFlowState();
5679
+ dispatch({ type: "CONFIRM_SIGN_SUCCESS", transfer: signedTransfer });
5680
+ polling.startPolling(t.id);
5552
5681
  } catch (err) {
5553
5682
  captureException(err);
5554
- setError(err instanceof Error ? err.message : "Failed to register passkey");
5555
- } finally {
5556
- setRegisteringPasskey(false);
5683
+ const msg = err instanceof Error ? err.message : "Failed to sign transfer";
5684
+ dispatch({ type: "SET_ERROR", error: msg });
5685
+ onError?.(msg);
5557
5686
  }
5558
- }, [user, getAccessToken, apiBaseUrl, accounts, connectingNewAccount]);
5559
- const handleVerifyPasskeyViaPopup = react.useCallback(async () => {
5560
- setVerifyingPasskeyPopup(true);
5561
- setError(null);
5562
- try {
5563
- const token = await getAccessToken();
5564
- const matched = await findDevicePasskeyViaPopup({
5565
- credentialIds: knownCredentialIds,
5566
- rpId: resolvePasskeyRpId(),
5567
- authToken: token ?? void 0,
5568
- apiBaseUrl
5569
- });
5570
- if (matched) {
5571
- setActiveCredentialId(matched);
5572
- window.localStorage.setItem(ACTIVE_CREDENTIAL_STORAGE_KEY, matched);
5573
- if (token) {
5574
- reportPasskeyActivity(apiBaseUrl, token, matched).catch(() => {
5575
- });
5576
- }
5577
- setStep("login");
5578
- } else {
5579
- setStep("create-passkey");
5580
- }
5581
- } catch {
5582
- setStep("create-passkey");
5583
- } finally {
5584
- setVerifyingPasskeyPopup(false);
5687
+ }, [state.transfer, polling.transfer, polling.startPolling, transferSigning, onError]);
5688
+ const handleRetryMobileStatus = react.useCallback(() => {
5689
+ dispatch({ type: "SET_ERROR", error: null });
5690
+ handlingMobileReturnRef.current = false;
5691
+ const currentTransfer = polling.transfer ?? state.transfer;
5692
+ if (currentTransfer?.status === "AUTHORIZED") {
5693
+ void handleAuthorizedMobileReturn(currentTransfer, mobileSetupFlowRef.current);
5694
+ return;
5695
+ }
5696
+ const transferIdToResume = pollingTransferIdRef.current ?? currentTransfer?.id;
5697
+ if (transferIdToResume) {
5698
+ polling.startPolling(transferIdToResume);
5585
5699
  }
5586
- }, [knownCredentialIds, getAccessToken, apiBaseUrl]);
5700
+ }, [handleAuthorizedMobileReturn, polling, state.transfer]);
5587
5701
  const handleSelectProvider = react.useCallback((providerId) => {
5588
- setSelectedProviderId(providerId);
5589
- setSelectedAccountId(null);
5590
- setConnectingNewAccount(true);
5702
+ dispatch({ type: "SELECT_PROVIDER", providerId });
5591
5703
  const isMobile = !shouldUseWalletConnector({
5592
- useWalletConnector,
5704
+ useWalletConnector: useWalletConnectorProp,
5593
5705
  userAgent: typeof navigator === "undefined" ? void 0 : navigator.userAgent
5594
5706
  });
5595
5707
  if (isMobile) {
5596
5708
  handlingMobileReturnRef.current = false;
5597
5709
  mobileSetupFlowRef.current = true;
5598
- const amount2 = depositAmount ?? 5;
5599
- handlePay(amount2, { sourceType: "providerId", sourceId: providerId });
5710
+ const amount = depositAmount ?? 5;
5711
+ handlePay(amount, { sourceType: "providerId", sourceId: providerId });
5600
5712
  } else {
5601
- setStep("deposit");
5713
+ dispatch({ type: "NAVIGATE", step: "deposit" });
5602
5714
  }
5603
- }, [useWalletConnector, depositAmount, handlePay]);
5715
+ }, [useWalletConnectorProp, depositAmount, handlePay]);
5604
5716
  const handleContinueConnection = react.useCallback(
5605
5717
  (accountId) => {
5606
- const acct = accounts.find((a) => a.id === accountId);
5607
- setSelectedAccountId(accountId);
5608
- setSelectedWalletId(acct?.wallets[0]?.id ?? null);
5609
- setConnectingNewAccount(false);
5610
- setStep("deposit");
5718
+ const acct = state.accounts.find((a) => a.id === accountId);
5719
+ dispatch({
5720
+ type: "SELECT_ACCOUNT",
5721
+ accountId,
5722
+ walletId: acct?.wallets[0]?.id ?? null
5723
+ });
5611
5724
  },
5612
- [accounts]
5725
+ [state.accounts]
5613
5726
  );
5614
5727
  const handleNewPayment = react.useCallback(() => {
5615
5728
  clearMobileFlowState();
5616
- setStep("deposit");
5617
- setTransfer(null);
5618
- setError(null);
5619
- setAmount(depositAmount != null ? depositAmount.toString() : "");
5620
- setMobileFlow(false);
5621
- setDeeplinkUri(null);
5622
5729
  processingStartedAtRef.current = null;
5623
5730
  pollingTransferIdRef.current = null;
5624
- mobileSigningTransferIdRef.current = null;
5625
5731
  preSelectSourceStepRef.current = null;
5626
- setConnectingNewAccount(false);
5627
- setSelectedWalletId(null);
5628
- if (accounts.length > 0) setSelectedAccountId(accounts[0].id);
5629
- }, [depositAmount, accounts]);
5732
+ dispatch({
5733
+ type: "NEW_PAYMENT",
5734
+ depositAmount,
5735
+ firstAccountId: state.accounts.length > 0 ? state.accounts[0].id : null
5736
+ });
5737
+ }, [depositAmount, state.accounts]);
5630
5738
  const handleLogout = react.useCallback(async () => {
5631
5739
  try {
5632
5740
  await logout();
@@ -5637,254 +5745,464 @@ function SwypePaymentInner({
5637
5745
  window.localStorage.removeItem(ACTIVE_CREDENTIAL_STORAGE_KEY);
5638
5746
  }
5639
5747
  polling.stopPolling();
5640
- setActiveCredentialId(null);
5641
- setStep("login");
5642
- setError(null);
5643
- setTransfer(null);
5644
- setCreatingTransfer(false);
5645
- setRegisteringPasskey(false);
5646
- setProviders([]);
5647
- setAccounts([]);
5648
- setChains([]);
5649
- setSelectedAccountId(null);
5650
- setSelectedWalletId(null);
5651
- setSelectedProviderId(null);
5652
- setConnectingNewAccount(false);
5653
- setAmount(depositAmount != null ? depositAmount.toString() : "");
5654
- setMobileFlow(false);
5655
- setDeeplinkUri(null);
5656
5748
  preSelectSourceStepRef.current = null;
5657
- resetHeadlessLogin();
5658
- }, [logout, polling, depositAmount, resetHeadlessLogin]);
5659
- const handleConfirmSign = react.useCallback(async () => {
5660
- const t = transfer ?? polling.transfer;
5661
- if (!t) return;
5662
- try {
5663
- const signedTransfer = await transferSigning.signTransfer(t.id);
5664
- setTransfer(signedTransfer);
5665
- clearMobileFlowState();
5666
- setStep("processing");
5667
- polling.startPolling(t.id);
5668
- } catch (err) {
5669
- captureException(err);
5670
- const msg = err instanceof Error ? err.message : "Failed to sign transfer";
5671
- setError(msg);
5672
- onError?.(msg);
5749
+ setAuthInput("");
5750
+ setOtpCode("");
5751
+ dispatch({ type: "LOGOUT", depositAmount });
5752
+ }, [logout, polling, depositAmount]);
5753
+ const pendingSelectSourceAction = authExecutor.pendingSelectSource;
5754
+ const selectSourceChoices = react.useMemo(() => {
5755
+ if (!pendingSelectSourceAction) return [];
5756
+ const options = pendingSelectSourceAction.metadata?.options ?? [];
5757
+ return buildSelectSourceChoices(options);
5758
+ }, [pendingSelectSourceAction]);
5759
+ const selectSourceRecommended = react.useMemo(() => {
5760
+ if (!pendingSelectSourceAction) return null;
5761
+ return pendingSelectSourceAction.metadata?.recommended ?? null;
5762
+ }, [pendingSelectSourceAction]);
5763
+ const handleSelectSourceChainChange = react.useCallback(
5764
+ (chainName) => {
5765
+ setSelectSourceChainName(chainName);
5766
+ const chain = selectSourceChoices.find((c) => c.chainName === chainName);
5767
+ if (!chain || chain.tokens.length === 0) return;
5768
+ const recommendedToken = selectSourceRecommended?.chainName === chainName ? selectSourceRecommended.tokenSymbol : null;
5769
+ const hasRecommended = !!recommendedToken && chain.tokens.some((t) => t.tokenSymbol === recommendedToken);
5770
+ setSelectSourceTokenSymbol(
5771
+ hasRecommended ? recommendedToken : chain.tokens[0].tokenSymbol
5772
+ );
5773
+ },
5774
+ [selectSourceChoices, selectSourceRecommended]
5775
+ );
5776
+ const handleConfirmSelectSource = react.useCallback(() => {
5777
+ authExecutor.resolveSelectSource({
5778
+ chainName: selectSourceChainName,
5779
+ tokenSymbol: selectSourceTokenSymbol
5780
+ });
5781
+ }, [authExecutor, selectSourceChainName, selectSourceTokenSymbol]);
5782
+ react.useEffect(() => {
5783
+ if (depositAmount != null) {
5784
+ dispatch({ type: "SYNC_AMOUNT", amount: depositAmount.toString() });
5673
5785
  }
5674
- }, [transfer, polling.transfer, polling.startPolling, transferSigning, onError]);
5675
- if (!ready) {
5676
- return /* @__PURE__ */ jsxRuntime.jsx(ScreenLayout, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { style: { textAlign: "center", padding: "48px 0", flex: 1, display: "flex", alignItems: "center", justifyContent: "center" }, children: /* @__PURE__ */ jsxRuntime.jsx(Spinner, { label: "Initializing..." }) }) });
5677
- }
5678
- if (step === "login" && !authenticated) {
5679
- return /* @__PURE__ */ jsxRuntime.jsx(
5680
- LoginScreen,
5681
- {
5682
- authInput,
5683
- onAuthInputChange: setAuthInput,
5684
- onSubmit: handleSendLoginCode,
5685
- sending: activeOtpStatus === "sending-code",
5686
- error,
5687
- onBack,
5688
- merchantInitials: merchantName ? merchantName.slice(0, 2).toUpperCase() : void 0
5689
- }
5690
- );
5691
- }
5692
- if (step === "otp-verify" && !authenticated) {
5693
- return /* @__PURE__ */ jsxRuntime.jsx(
5694
- OtpVerifyScreen,
5695
- {
5696
- maskedIdentifier: verificationTarget ? maskAuthIdentifier(verificationTarget) : "",
5697
- otpCode,
5698
- onOtpChange: (code) => {
5699
- setOtpCode(code);
5700
- setError(null);
5701
- },
5702
- onVerify: handleVerifyLoginCode,
5703
- onResend: handleResendLoginCode,
5704
- onBack: () => {
5705
- setVerificationTarget(null);
5706
- setOtpCode("");
5707
- setError(null);
5708
- setStep("login");
5709
- },
5710
- verifying: activeOtpStatus === "submitting-code",
5711
- error
5712
- }
5713
- );
5714
- }
5715
- if ((step === "login" || step === "otp-verify") && authenticated) {
5716
- return /* @__PURE__ */ jsxRuntime.jsx(ScreenLayout, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { style: { textAlign: "center", padding: "48px 0", flex: 1, display: "flex", alignItems: "center", justifyContent: "center" }, children: /* @__PURE__ */ jsxRuntime.jsx(Spinner, { label: "Verifying your passkey..." }) }) });
5717
- }
5718
- if (step === "verify-passkey") {
5719
- return /* @__PURE__ */ jsxRuntime.jsx(
5720
- VerifyPasskeyScreen,
5721
- {
5722
- onVerify: handleVerifyPasskeyViaPopup,
5723
- onBack: handleLogout,
5724
- verifying: verifyingPasskeyPopup,
5725
- error
5726
- }
5727
- );
5728
- }
5729
- if (step === "create-passkey") {
5730
- return /* @__PURE__ */ jsxRuntime.jsx(
5731
- CreatePasskeyScreen,
5732
- {
5733
- onCreatePasskey: handleRegisterPasskey,
5734
- onBack: handleLogout,
5735
- creating: registeringPasskey,
5736
- error,
5737
- popupFallback: passkeyPopupNeeded,
5738
- onCreatePasskeyViaPopup: handleCreatePasskeyViaPopup
5786
+ }, [depositAmount]);
5787
+ react.useEffect(() => {
5788
+ if (authenticated) return;
5789
+ if (activeOtpErrorMessage) dispatch({ type: "SET_ERROR", error: activeOtpErrorMessage });
5790
+ }, [activeOtpErrorMessage, authenticated]);
5791
+ react.useEffect(() => {
5792
+ if (state.step === "otp-verify" && /^\d{6}$/.test(otpCode.trim()) && activeOtpStatus !== "submitting-code") {
5793
+ handleVerifyLoginCode();
5794
+ }
5795
+ }, [otpCode, state.step, activeOtpStatus, handleVerifyLoginCode]);
5796
+ react.useEffect(() => {
5797
+ if (!ready || !authenticated) return;
5798
+ if (state.step !== "login" && state.step !== "otp-verify") return;
5799
+ let cancelled = false;
5800
+ dispatch({ type: "SET_ERROR", error: null });
5801
+ setAuthInput("");
5802
+ setOtpCode("");
5803
+ const restoreOrDeposit = async (credId, token) => {
5804
+ const persisted = loadMobileFlowState();
5805
+ let accts = [];
5806
+ try {
5807
+ accts = await fetchAccounts(apiBaseUrl, token, credId);
5808
+ if (cancelled) return;
5809
+ } catch {
5739
5810
  }
5740
- );
5741
- }
5742
- if (step === "wallet-picker") {
5743
- return /* @__PURE__ */ jsxRuntime.jsx(
5744
- WalletPickerScreen,
5745
- {
5746
- providers,
5747
- pendingConnections,
5748
- loading: creatingTransfer,
5749
- onSelectProvider: handleSelectProvider,
5750
- onContinueConnection: handleContinueConnection,
5751
- onBack: () => setStep(activeCredentialId ? "deposit" : "create-passkey")
5811
+ const resolved = resolvePostAuthStep({
5812
+ hasPasskey: true,
5813
+ accounts: accts,
5814
+ persistedMobileFlow: persisted,
5815
+ mobileSetupInProgress: false,
5816
+ connectingNewAccount: false
5817
+ });
5818
+ if (resolved.clearPersistedFlow) clearMobileFlowState();
5819
+ if (resolved.step === "deposit" && persisted && persisted.isSetup) {
5820
+ try {
5821
+ const existingTransfer = await fetchTransfer(apiBaseUrl, token, persisted.transferId);
5822
+ if (cancelled) return;
5823
+ if (existingTransfer.status === "AUTHORIZED") {
5824
+ await handleAuthorizedMobileReturn(existingTransfer, true);
5825
+ return;
5826
+ }
5827
+ } catch {
5828
+ }
5752
5829
  }
5753
- );
5754
- }
5755
- if (step === "open-wallet") {
5756
- const providerName = providers.find((p) => p.id === selectedProviderId)?.name ?? null;
5757
- return /* @__PURE__ */ jsxRuntime.jsx(
5758
- OpenWalletScreen,
5759
- {
5760
- walletName: providerName,
5761
- deeplinkUri: deeplinkUri ?? "",
5762
- loading: creatingTransfer || !deeplinkUri,
5763
- error: error || polling.error,
5764
- onRetryStatus: handleRetryMobileStatus,
5765
- onLogout: handleLogout
5830
+ if (resolved.step === "open-wallet" && persisted) {
5831
+ try {
5832
+ const existingTransfer = await fetchTransfer(apiBaseUrl, token, persisted.transferId);
5833
+ if (cancelled) return;
5834
+ const mobileResolution = resolveRestoredMobileFlow(
5835
+ existingTransfer.status,
5836
+ persisted.isSetup
5837
+ );
5838
+ if (mobileResolution.kind === "resume-setup-deposit") {
5839
+ await handleAuthorizedMobileReturn(existingTransfer, true);
5840
+ return;
5841
+ }
5842
+ if (mobileResolution.kind === "resume-confirm-sign") {
5843
+ await handleAuthorizedMobileReturn(existingTransfer, false);
5844
+ return;
5845
+ }
5846
+ if (mobileResolution.kind === "resume-success") {
5847
+ clearMobileFlowState();
5848
+ dispatch({ type: "MOBILE_RESUME_SUCCESS", transfer: existingTransfer });
5849
+ onComplete?.(existingTransfer);
5850
+ return;
5851
+ }
5852
+ if (mobileResolution.kind === "resume-failed") {
5853
+ clearMobileFlowState();
5854
+ dispatch({ type: "MOBILE_RESUME_FAILED", transfer: existingTransfer });
5855
+ return;
5856
+ }
5857
+ if (mobileResolution.kind === "resume-processing") {
5858
+ clearMobileFlowState();
5859
+ dispatch({ type: "MOBILE_RESUME_PROCESSING", transfer: existingTransfer });
5860
+ polling.startPolling(existingTransfer.id);
5861
+ return;
5862
+ }
5863
+ if (mobileResolution.kind === "resume-stale-setup") {
5864
+ clearMobileFlowState();
5865
+ if (!cancelled) dispatch({ type: "NAVIGATE", step: "wallet-picker" });
5866
+ return;
5867
+ }
5868
+ } catch (err) {
5869
+ if (cancelled) return;
5870
+ dispatch({
5871
+ type: "ENTER_MOBILE_FLOW",
5872
+ deeplinkUri: persisted.deeplinkUri,
5873
+ providerId: persisted.providerId,
5874
+ error: err instanceof Error ? err.message : "Unable to refresh wallet authorization status."
5875
+ });
5876
+ pollingTransferIdRef.current = persisted.transferId;
5877
+ mobileSetupFlowRef.current = persisted.isSetup;
5878
+ polling.startPolling(persisted.transferId);
5879
+ return;
5880
+ }
5881
+ dispatch({
5882
+ type: "ENTER_MOBILE_FLOW",
5883
+ deeplinkUri: persisted.deeplinkUri,
5884
+ providerId: persisted.providerId
5885
+ });
5886
+ pollingTransferIdRef.current = persisted.transferId;
5887
+ mobileSetupFlowRef.current = persisted.isSetup;
5888
+ polling.startPolling(persisted.transferId);
5889
+ return;
5766
5890
  }
5767
- );
5768
- }
5769
- if (step === "confirm-sign") {
5770
- const providerName = providers.find((p) => p.id === selectedProviderId)?.name ?? null;
5771
- return /* @__PURE__ */ jsxRuntime.jsx(
5772
- ConfirmSignScreen,
5773
- {
5774
- walletName: providerName,
5775
- signing: transferSigning.signing,
5776
- error: error || transferSigning.error,
5777
- onSign: handleConfirmSign,
5778
- onLogout: handleLogout
5891
+ dispatch({ type: "NAVIGATE", step: resolved.step });
5892
+ };
5893
+ const checkPasskey = async () => {
5894
+ try {
5895
+ const token = await getAccessToken();
5896
+ if (!token || cancelled) return;
5897
+ const { config } = await fetchUserConfig(apiBaseUrl, token);
5898
+ if (cancelled) return;
5899
+ const allPasskeys = config.passkeys ?? (config.passkey ? [config.passkey] : []);
5900
+ dispatch({
5901
+ type: "PASSKEY_CONFIG_LOADED",
5902
+ knownIds: allPasskeys.map((p) => p.credentialId),
5903
+ oneTapLimit: config.defaultAllowance ?? void 0
5904
+ });
5905
+ if (allPasskeys.length === 0) {
5906
+ dispatch({ type: "NAVIGATE", step: "create-passkey" });
5907
+ return;
5908
+ }
5909
+ if (state.activeCredentialId && allPasskeys.some((p) => p.credentialId === state.activeCredentialId)) {
5910
+ await restoreOrDeposit(state.activeCredentialId, token);
5911
+ return;
5912
+ }
5913
+ if (cancelled) return;
5914
+ if (isSafari() && isInCrossOriginIframe()) {
5915
+ dispatch({ type: "NAVIGATE", step: "verify-passkey" });
5916
+ return;
5917
+ }
5918
+ const credentialIds = allPasskeys.map((p) => p.credentialId);
5919
+ const matched = await findDevicePasskey(credentialIds);
5920
+ if (cancelled) return;
5921
+ if (matched) {
5922
+ dispatch({ type: "PASSKEY_ACTIVATED", credentialId: matched });
5923
+ window.localStorage.setItem(ACTIVE_CREDENTIAL_STORAGE_KEY, matched);
5924
+ reportPasskeyActivity(apiBaseUrl, token, matched).catch(() => {
5925
+ });
5926
+ await restoreOrDeposit(matched, token);
5927
+ return;
5928
+ }
5929
+ dispatch({ type: "NAVIGATE", step: "create-passkey" });
5930
+ } catch {
5931
+ if (!cancelled) dispatch({ type: "NAVIGATE", step: "create-passkey" });
5779
5932
  }
5780
- );
5781
- }
5782
- if (step === "deposit") {
5783
- if (loadingData) {
5784
- return /* @__PURE__ */ jsxRuntime.jsx(ScreenLayout, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { style: { textAlign: "center", padding: "48px 0", flex: 1, display: "flex", alignItems: "center", justifyContent: "center" }, children: /* @__PURE__ */ jsxRuntime.jsx(Spinner, { label: "Loading..." }) }) });
5933
+ };
5934
+ checkPasskey();
5935
+ return () => {
5936
+ cancelled = true;
5937
+ };
5938
+ }, [
5939
+ ready,
5940
+ authenticated,
5941
+ state.step,
5942
+ apiBaseUrl,
5943
+ getAccessToken,
5944
+ state.activeCredentialId,
5945
+ handleAuthorizedMobileReturn,
5946
+ onComplete,
5947
+ polling
5948
+ ]);
5949
+ react.useEffect(() => {
5950
+ const loadAction = resolveDataLoadAction({
5951
+ authenticated,
5952
+ step: state.step,
5953
+ accountsCount: state.accounts.length,
5954
+ hasActiveCredential: !!state.activeCredentialId,
5955
+ loading: loadingDataRef.current
5956
+ });
5957
+ if (loadAction === "reset") {
5958
+ loadingDataRef.current = false;
5959
+ dispatch({ type: "DATA_LOAD_END" });
5960
+ return;
5785
5961
  }
5786
- const parsedAmt = depositAmount != null ? depositAmount : 5;
5787
- return /* @__PURE__ */ jsxRuntime.jsx(
5788
- DepositScreen,
5789
- {
5790
- merchantName,
5791
- sourceName,
5792
- sourceAddress,
5793
- sourceVerified,
5794
- availableBalance: maxSourceBalance,
5795
- remainingLimit: selectedAccount?.remainingAllowance ?? oneTapLimit,
5796
- tokenCount,
5797
- initialAmount: parsedAmt,
5798
- processing: creatingTransfer,
5799
- error,
5800
- onDeposit: handlePay,
5801
- onChangeSource: () => setStep("wallet-picker"),
5802
- onSwitchWallet: () => setStep("wallet-picker"),
5803
- onBack: onBack ?? (() => handleLogout()),
5804
- onLogout: handleLogout,
5805
- onIncreaseLimit: handleIncreaseLimit,
5806
- increasingLimit
5807
- }
5808
- );
5809
- }
5810
- if (step === "processing") {
5811
- const polledStatus = polling.transfer?.status;
5812
- const transferPhase = creatingTransfer ? "creating" : polledStatus === "SENDING" || polledStatus === "SENT" ? "sent" : "verifying";
5813
- return /* @__PURE__ */ jsxRuntime.jsx(
5814
- TransferStatusScreen,
5815
- {
5816
- phase: transferPhase,
5817
- error: error || authExecutor.error || transferSigning.error || polling.error,
5818
- onLogout: handleLogout
5819
- }
5820
- );
5821
- }
5822
- if (step === "select-source") {
5823
- return /* @__PURE__ */ jsxRuntime.jsx(
5824
- SelectSourceScreen,
5825
- {
5826
- choices: selectSourceChoices,
5827
- selectedChainName: selectSourceChainName,
5828
- selectedTokenSymbol: selectSourceTokenSymbol,
5829
- recommended: selectSourceRecommended,
5830
- onChainChange: handleSelectSourceChainChange,
5831
- onTokenChange: setSelectSourceTokenSymbol,
5832
- onConfirm: () => {
5833
- authExecutor.resolveSelectSource({
5834
- chainName: selectSourceChainName,
5835
- tokenSymbol: selectSourceTokenSymbol
5962
+ if (loadAction === "wait") return;
5963
+ const credentialId = state.activeCredentialId;
5964
+ if (!credentialId) {
5965
+ loadingDataRef.current = false;
5966
+ dispatch({ type: "DATA_LOAD_END" });
5967
+ return;
5968
+ }
5969
+ let cancelled = false;
5970
+ loadingDataRef.current = true;
5971
+ const load = async () => {
5972
+ dispatch({ type: "DATA_LOAD_START" });
5973
+ try {
5974
+ const token = await getAccessToken();
5975
+ if (!token) throw new Error("Not authenticated");
5976
+ const [prov, accts, chn] = await Promise.all([
5977
+ fetchProviders(apiBaseUrl, token),
5978
+ fetchAccounts(apiBaseUrl, token, credentialId),
5979
+ fetchChains(apiBaseUrl, token)
5980
+ ]);
5981
+ if (cancelled) return;
5982
+ const parsedAmt = depositAmount != null ? depositAmount : 0;
5983
+ const defaults = computeSmartDefaults(accts, parsedAmt);
5984
+ const persisted = loadMobileFlowState();
5985
+ const resolved = resolvePostAuthStep({
5986
+ hasPasskey: !!state.activeCredentialId,
5987
+ accounts: accts,
5988
+ persistedMobileFlow: persisted,
5989
+ mobileSetupInProgress: mobileSetupFlowRef.current,
5990
+ connectingNewAccount: state.connectingNewAccount
5991
+ });
5992
+ const correctableSteps = ["deposit", "wallet-picker", "open-wallet"];
5993
+ dispatch({
5994
+ type: "DATA_LOADED",
5995
+ providers: prov,
5996
+ accounts: accts,
5997
+ chains: chn,
5998
+ defaults,
5999
+ fallbackProviderId: !defaults && prov.length > 0 ? prov[0].id : null,
6000
+ resolvedStep: correctableSteps.includes(state.step) ? resolved.step : void 0,
6001
+ clearMobileState: resolved.clearPersistedFlow
6002
+ });
6003
+ if (resolved.clearPersistedFlow) clearMobileFlowState();
6004
+ } catch (err) {
6005
+ if (!cancelled) {
6006
+ captureException(err);
6007
+ dispatch({
6008
+ type: "SET_ERROR",
6009
+ error: err instanceof Error ? err.message : "Failed to load data"
5836
6010
  });
5837
- },
5838
- onLogout: handleLogout
5839
- }
5840
- );
5841
- }
5842
- if (step === "success") {
5843
- const succeeded = transfer?.status === "COMPLETED";
5844
- const displayAmount = transfer?.amount?.amount ?? 0;
5845
- const displayCurrency = transfer?.amount?.currency ?? "USD";
5846
- return /* @__PURE__ */ jsxRuntime.jsx(
5847
- SuccessScreen,
5848
- {
5849
- amount: displayAmount,
5850
- currency: displayCurrency,
5851
- succeeded,
5852
- error,
5853
- merchantName,
5854
- sourceName,
5855
- remainingLimit: succeeded ? (() => {
5856
- const limit = selectedAccount?.remainingAllowance ?? oneTapLimit;
5857
- return limit > displayAmount ? limit - displayAmount : 0;
5858
- })() : void 0,
5859
- onDone: onDismiss ?? handleNewPayment,
5860
- onLogout: handleLogout,
5861
- autoCloseSeconds
6011
+ }
6012
+ } finally {
6013
+ if (!cancelled) {
6014
+ loadingDataRef.current = false;
6015
+ dispatch({ type: "DATA_LOAD_END" });
6016
+ }
5862
6017
  }
5863
- );
5864
- }
5865
- if (step === "low-balance") {
5866
- return /* @__PURE__ */ jsxRuntime.jsx(
5867
- DepositScreen,
5868
- {
5869
- merchantName,
5870
- sourceName,
5871
- sourceAddress,
5872
- sourceVerified,
5873
- availableBalance: 0,
5874
- remainingLimit: selectedAccount?.remainingAllowance ?? oneTapLimit,
5875
- tokenCount,
5876
- initialAmount: depositAmount ?? 5,
5877
- processing: false,
5878
- error,
5879
- onDeposit: handlePay,
5880
- onChangeSource: () => setStep("wallet-picker"),
5881
- onSwitchWallet: () => setStep("wallet-picker"),
5882
- onBack: onBack ?? (() => handleLogout()),
5883
- onLogout: handleLogout
6018
+ };
6019
+ load();
6020
+ return () => {
6021
+ cancelled = true;
6022
+ loadingDataRef.current = false;
6023
+ };
6024
+ }, [
6025
+ authenticated,
6026
+ state.step,
6027
+ state.accounts.length,
6028
+ apiBaseUrl,
6029
+ getAccessToken,
6030
+ state.activeCredentialId,
6031
+ depositAmount,
6032
+ state.connectingNewAccount
6033
+ ]);
6034
+ react.useEffect(() => {
6035
+ if (!polling.transfer) return;
6036
+ if (polling.transfer.status === "COMPLETED") {
6037
+ clearMobileFlowState();
6038
+ dispatch({ type: "TRANSFER_COMPLETED", transfer: polling.transfer });
6039
+ onComplete?.(polling.transfer);
6040
+ } else if (polling.transfer.status === "FAILED") {
6041
+ clearMobileFlowState();
6042
+ dispatch({ type: "TRANSFER_FAILED", transfer: polling.transfer, error: "Transfer failed." });
6043
+ }
6044
+ }, [polling.transfer, onComplete]);
6045
+ react.useEffect(() => {
6046
+ if (state.step !== "processing") {
6047
+ processingStartedAtRef.current = null;
6048
+ return;
6049
+ }
6050
+ if (!processingStartedAtRef.current) {
6051
+ processingStartedAtRef.current = Date.now();
6052
+ }
6053
+ const elapsedMs = Date.now() - processingStartedAtRef.current;
6054
+ const remainingMs = PROCESSING_TIMEOUT_MS - elapsedMs;
6055
+ const handleTimeout = () => {
6056
+ if (!hasProcessingTimedOut(processingStartedAtRef.current, Date.now())) return;
6057
+ const status = getTransferStatus(polling.transfer, state.transfer);
6058
+ const msg = buildProcessingTimeoutMessage(status);
6059
+ captureException(new Error(msg));
6060
+ polling.stopPolling();
6061
+ dispatch({ type: "PROCESSING_TIMEOUT", error: msg });
6062
+ onError?.(msg);
6063
+ };
6064
+ if (remainingMs <= 0) {
6065
+ handleTimeout();
6066
+ return;
6067
+ }
6068
+ const timeoutId = window.setTimeout(handleTimeout, remainingMs);
6069
+ return () => window.clearTimeout(timeoutId);
6070
+ }, [state.step, polling.transfer, state.transfer, polling.stopPolling, onError]);
6071
+ react.useEffect(() => {
6072
+ if (!state.mobileFlow) {
6073
+ handlingMobileReturnRef.current = false;
6074
+ return;
6075
+ }
6076
+ if (handlingMobileReturnRef.current) return;
6077
+ const polledTransfer = polling.transfer;
6078
+ if (!polledTransfer || polledTransfer.status !== "AUTHORIZED") return;
6079
+ void handleAuthorizedMobileReturn(polledTransfer, mobileSetupFlowRef.current);
6080
+ }, [state.mobileFlow, polling.transfer, handleAuthorizedMobileReturn]);
6081
+ react.useEffect(() => {
6082
+ if (!state.mobileFlow) return;
6083
+ if (handlingMobileReturnRef.current) return;
6084
+ const transferIdToResume = pollingTransferIdRef.current ?? state.transfer?.id;
6085
+ if (!transferIdToResume) return;
6086
+ if (!polling.isPolling) polling.startPolling(transferIdToResume);
6087
+ const handleVisibility = () => {
6088
+ if (document.visibilityState === "visible" && !handlingMobileReturnRef.current) {
6089
+ polling.startPolling(transferIdToResume);
5884
6090
  }
6091
+ };
6092
+ document.addEventListener("visibilitychange", handleVisibility);
6093
+ return () => document.removeEventListener("visibilitychange", handleVisibility);
6094
+ }, [state.mobileFlow, state.transfer?.id, polling.isPolling, polling.startPolling]);
6095
+ react.useEffect(() => {
6096
+ if (!pendingSelectSourceAction) {
6097
+ initializedSelectSourceActionRef.current = null;
6098
+ setSelectSourceChainName("");
6099
+ setSelectSourceTokenSymbol("");
6100
+ return;
6101
+ }
6102
+ if (initializedSelectSourceActionRef.current === pendingSelectSourceAction.id) return;
6103
+ const hasRecommended = !!selectSourceRecommended && selectSourceChoices.some(
6104
+ (chain) => chain.chainName === selectSourceRecommended.chainName && chain.tokens.some((t) => t.tokenSymbol === selectSourceRecommended.tokenSymbol)
5885
6105
  );
5886
- }
5887
- return null;
6106
+ if (hasRecommended && selectSourceRecommended) {
6107
+ setSelectSourceChainName(selectSourceRecommended.chainName);
6108
+ setSelectSourceTokenSymbol(selectSourceRecommended.tokenSymbol);
6109
+ } else if (selectSourceChoices.length > 0 && selectSourceChoices[0].tokens.length > 0) {
6110
+ setSelectSourceChainName(selectSourceChoices[0].chainName);
6111
+ setSelectSourceTokenSymbol(selectSourceChoices[0].tokens[0].tokenSymbol);
6112
+ } else {
6113
+ setSelectSourceChainName("Base");
6114
+ setSelectSourceTokenSymbol("USDC");
6115
+ }
6116
+ initializedSelectSourceActionRef.current = pendingSelectSourceAction.id;
6117
+ }, [pendingSelectSourceAction, selectSourceChoices, selectSourceRecommended]);
6118
+ react.useEffect(() => {
6119
+ if (pendingSelectSourceAction && state.step === "processing") {
6120
+ preSelectSourceStepRef.current = state.step;
6121
+ dispatch({ type: "NAVIGATE", step: "select-source" });
6122
+ } else if (!pendingSelectSourceAction && state.step === "select-source") {
6123
+ dispatch({ type: "NAVIGATE", step: preSelectSourceStepRef.current ?? "processing" });
6124
+ preSelectSourceStepRef.current = null;
6125
+ }
6126
+ }, [pendingSelectSourceAction, state.step]);
6127
+ const handlers = react.useMemo(() => ({
6128
+ onSendLoginCode: handleSendLoginCode,
6129
+ onVerifyLoginCode: handleVerifyLoginCode,
6130
+ onResendLoginCode: handleResendLoginCode,
6131
+ onBackFromOtp: () => {
6132
+ setOtpCode("");
6133
+ dispatch({ type: "BACK_TO_LOGIN" });
6134
+ },
6135
+ onRegisterPasskey: handleRegisterPasskey,
6136
+ onCreatePasskeyViaPopup: handleCreatePasskeyViaPopup,
6137
+ onVerifyPasskeyViaPopup: handleVerifyPasskeyViaPopup,
6138
+ onSelectProvider: handleSelectProvider,
6139
+ onContinueConnection: handleContinueConnection,
6140
+ onPay: handlePay,
6141
+ onIncreaseLimit: handleIncreaseLimit,
6142
+ onConfirmSign: handleConfirmSign,
6143
+ onRetryMobileStatus: handleRetryMobileStatus,
6144
+ onLogout: handleLogout,
6145
+ onNewPayment: handleNewPayment,
6146
+ onNavigate: (step) => dispatch({ type: "NAVIGATE", step }),
6147
+ onSetAuthInput: setAuthInput,
6148
+ onSetOtpCode: (code) => {
6149
+ setOtpCode(code);
6150
+ dispatch({ type: "SET_ERROR", error: null });
6151
+ },
6152
+ onSelectSourceChainChange: handleSelectSourceChainChange,
6153
+ onSetSelectSourceTokenSymbol: setSelectSourceTokenSymbol,
6154
+ onConfirmSelectSource: handleConfirmSelectSource
6155
+ }), [
6156
+ handleSendLoginCode,
6157
+ handleVerifyLoginCode,
6158
+ handleResendLoginCode,
6159
+ handleRegisterPasskey,
6160
+ handleCreatePasskeyViaPopup,
6161
+ handleVerifyPasskeyViaPopup,
6162
+ handleSelectProvider,
6163
+ handleContinueConnection,
6164
+ handlePay,
6165
+ handleIncreaseLimit,
6166
+ handleConfirmSign,
6167
+ handleRetryMobileStatus,
6168
+ handleLogout,
6169
+ handleNewPayment,
6170
+ handleSelectSourceChainChange,
6171
+ handleConfirmSelectSource
6172
+ ]);
6173
+ return /* @__PURE__ */ jsxRuntime.jsx(
6174
+ StepRenderer,
6175
+ {
6176
+ state,
6177
+ ready,
6178
+ authenticated,
6179
+ activeOtpStatus,
6180
+ pollingTransfer: polling.transfer,
6181
+ pollingError: polling.error,
6182
+ authExecutorError: authExecutor.error,
6183
+ transferSigningSigning: transferSigning.signing,
6184
+ transferSigningError: transferSigning.error,
6185
+ pendingConnections,
6186
+ sourceName,
6187
+ sourceAddress,
6188
+ sourceVerified,
6189
+ maxSourceBalance,
6190
+ tokenCount,
6191
+ selectedAccount,
6192
+ selectSourceChoices,
6193
+ selectSourceRecommended,
6194
+ authInput,
6195
+ otpCode,
6196
+ selectSourceChainName,
6197
+ selectSourceTokenSymbol,
6198
+ merchantName,
6199
+ onBack,
6200
+ onDismiss,
6201
+ autoCloseSeconds,
6202
+ depositAmount,
6203
+ handlers
6204
+ }
6205
+ );
5888
6206
  }
5889
6207
 
5890
6208
  exports.AdvancedSourceScreen = AdvancedSourceScreen;