@swype-org/react-sdk 0.1.12 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1954,6 +1954,31 @@ function AdvancedSettings({
1954
1954
  )
1955
1955
  ] });
1956
1956
  }
1957
+
1958
+ // src/processingStatus.ts
1959
+ var PROCESSING_TIMEOUT_MS = 18e4;
1960
+ function resolvePreferredTransfer(polledTransfer, localTransfer) {
1961
+ return polledTransfer ?? localTransfer;
1962
+ }
1963
+ function getTransferStatus(polledTransfer, localTransfer) {
1964
+ const transfer = resolvePreferredTransfer(polledTransfer, localTransfer);
1965
+ return transfer?.status ?? "UNKNOWN";
1966
+ }
1967
+ function getDisplayTransferStatus(polledTransfer, localTransfer) {
1968
+ return getTransferStatus(polledTransfer, localTransfer).toUpperCase();
1969
+ }
1970
+ function getTransferIdSuffix(polledTransfer, localTransfer) {
1971
+ const transfer = resolvePreferredTransfer(polledTransfer, localTransfer);
1972
+ if (!transfer?.id) return "n/a";
1973
+ return transfer.id.slice(-8);
1974
+ }
1975
+ function hasProcessingTimedOut(processingStartedAtMs, nowMs) {
1976
+ if (!processingStartedAtMs) return false;
1977
+ return nowMs - processingStartedAtMs >= PROCESSING_TIMEOUT_MS;
1978
+ }
1979
+ function buildProcessingTimeoutMessage(status) {
1980
+ return `Payment is taking longer than expected (status: ${status}). Please try again.`;
1981
+ }
1957
1982
  var ACTIVE_CREDENTIAL_STORAGE_KEY = "swype_active_credential_id";
1958
1983
  function isMobile() {
1959
1984
  if (typeof navigator === "undefined") return false;
@@ -2037,7 +2062,7 @@ function SwypePayment({
2037
2062
  useWalletConnector
2038
2063
  }) {
2039
2064
  const { apiBaseUrl, tokens, depositAmount } = useSwypeConfig();
2040
- const { ready, authenticated, login, getAccessToken } = usePrivy();
2065
+ const { ready, authenticated, login, logout, getAccessToken } = usePrivy();
2041
2066
  const [step, setStep] = useState("login");
2042
2067
  const [error, setError] = useState(null);
2043
2068
  const [providers, setProviders] = useState([]);
@@ -2065,6 +2090,7 @@ function SwypePayment({
2065
2090
  const [mobileFlow, setMobileFlow] = useState(false);
2066
2091
  const pollingTransferIdRef = useRef(null);
2067
2092
  const mobileSigningTransferIdRef = useRef(null);
2093
+ const processingStartedAtRef = useRef(null);
2068
2094
  const [selectSourceChainName, setSelectSourceChainName] = useState("");
2069
2095
  const [selectSourceTokenSymbol, setSelectSourceTokenSymbol] = useState("");
2070
2096
  const initializedSelectSourceActionRef = useRef(null);
@@ -2172,6 +2198,36 @@ function SwypePayment({
2172
2198
  setError("Transfer failed.");
2173
2199
  }
2174
2200
  }, [polling.transfer, onComplete]);
2201
+ useEffect(() => {
2202
+ if (step !== "processing") {
2203
+ processingStartedAtRef.current = null;
2204
+ return;
2205
+ }
2206
+ if (!processingStartedAtRef.current) {
2207
+ processingStartedAtRef.current = Date.now();
2208
+ }
2209
+ const elapsedMs = Date.now() - processingStartedAtRef.current;
2210
+ const remainingMs = PROCESSING_TIMEOUT_MS - elapsedMs;
2211
+ const handleTimeout = () => {
2212
+ if (!hasProcessingTimedOut(processingStartedAtRef.current, Date.now())) {
2213
+ return;
2214
+ }
2215
+ const status = getTransferStatus(polling.transfer, transfer);
2216
+ const msg = buildProcessingTimeoutMessage(status);
2217
+ polling.stopPolling();
2218
+ setStep("ready");
2219
+ setError(msg);
2220
+ onError?.(msg);
2221
+ };
2222
+ if (remainingMs <= 0) {
2223
+ handleTimeout();
2224
+ return;
2225
+ }
2226
+ const timeoutId = window.setTimeout(handleTimeout, remainingMs);
2227
+ return () => {
2228
+ window.clearTimeout(timeoutId);
2229
+ };
2230
+ }, [step, polling.transfer, transfer, polling.stopPolling, onError]);
2175
2231
  useEffect(() => {
2176
2232
  if (!mobileFlow) return;
2177
2233
  const polledTransfer = polling.transfer;
@@ -2263,6 +2319,7 @@ function SwypePayment({
2263
2319
  return;
2264
2320
  }
2265
2321
  setStep("processing");
2322
+ processingStartedAtRef.current = Date.now();
2266
2323
  setError(null);
2267
2324
  setCreatingTransfer(true);
2268
2325
  setMobileFlow(false);
@@ -2348,6 +2405,7 @@ function SwypePayment({
2348
2405
  setError(null);
2349
2406
  setAmount(depositAmount != null ? depositAmount.toString() : "");
2350
2407
  setMobileFlow(false);
2408
+ processingStartedAtRef.current = null;
2351
2409
  pollingTransferIdRef.current = null;
2352
2410
  mobileSigningTransferIdRef.current = null;
2353
2411
  setConnectingNewAccount(false);
@@ -2355,6 +2413,38 @@ function SwypePayment({
2355
2413
  setAdvancedSettings({ asset: null, chain: null });
2356
2414
  if (accounts.length > 0) setSelectedAccountId(accounts[0].id);
2357
2415
  };
2416
+ const handleLogout = useCallback(async () => {
2417
+ try {
2418
+ await logout();
2419
+ } catch {
2420
+ }
2421
+ if (typeof window !== "undefined") {
2422
+ window.localStorage.removeItem(ACTIVE_CREDENTIAL_STORAGE_KEY);
2423
+ }
2424
+ polling.stopPolling();
2425
+ setActiveCredentialId(null);
2426
+ setStep("login");
2427
+ setError(null);
2428
+ setTransfer(null);
2429
+ setCreatingTransfer(false);
2430
+ setRegisteringPasskey(false);
2431
+ setProviders([]);
2432
+ setAccounts([]);
2433
+ setChains([]);
2434
+ setSelectedAccountId(null);
2435
+ setSelectedWalletId(null);
2436
+ setSelectedProviderId(null);
2437
+ setConnectingNewAccount(false);
2438
+ setAmount(depositAmount != null ? depositAmount.toString() : "");
2439
+ setAdvancedSettings({ asset: null, chain: null });
2440
+ setMobileFlow(false);
2441
+ setSelectSourceChainName("");
2442
+ setSelectSourceTokenSymbol("");
2443
+ initializedSelectSourceActionRef.current = null;
2444
+ processingStartedAtRef.current = null;
2445
+ pollingTransferIdRef.current = null;
2446
+ mobileSigningTransferIdRef.current = null;
2447
+ }, [logout, polling, depositAmount]);
2358
2448
  const handleConnectNewAccount = (providerId) => {
2359
2449
  setSelectedProviderId(providerId);
2360
2450
  setSelectedAccountId(null);
@@ -2712,7 +2802,32 @@ function SwypePayment({
2712
2802
  const canPay = !isNaN(parsedAmount) && parsedAmount > 0 && !!sourceId && !loadingData;
2713
2803
  const noAccounts = !loadingData && accounts.length === 0;
2714
2804
  return /* @__PURE__ */ jsxs("div", { style: cardStyle, children: [
2715
- stepBadge("Review & pay"),
2805
+ /* @__PURE__ */ jsxs("div", { style: { position: "relative" }, children: [
2806
+ stepBadge("Review & pay"),
2807
+ /* @__PURE__ */ jsx(
2808
+ "button",
2809
+ {
2810
+ type: "button",
2811
+ onClick: handleLogout,
2812
+ style: {
2813
+ position: "absolute",
2814
+ top: 0,
2815
+ right: 0,
2816
+ background: "transparent",
2817
+ border: "none",
2818
+ color: tokens.textMuted,
2819
+ cursor: "pointer",
2820
+ fontSize: "0.75rem",
2821
+ fontWeight: 600,
2822
+ letterSpacing: "0.04em",
2823
+ textTransform: "uppercase",
2824
+ fontFamily: "inherit",
2825
+ padding: 0
2826
+ },
2827
+ children: "Logout"
2828
+ }
2829
+ )
2830
+ ] }),
2716
2831
  error && /* @__PURE__ */ jsx("div", { style: errorStyle, children: error }),
2717
2832
  loadingData ? /* @__PURE__ */ jsx("div", { style: { padding: "24px 0", textAlign: "center" }, children: /* @__PURE__ */ jsx(Spinner, { label: "Loading..." }) }) : /* @__PURE__ */ jsxs(Fragment, { children: [
2718
2833
  /* @__PURE__ */ jsxs(
@@ -3104,6 +3219,8 @@ function SwypePayment({
3104
3219
  }
3105
3220
  };
3106
3221
  const regMsg = getRegistrationMessage();
3222
+ const transferStatus = getDisplayTransferStatus(polling.transfer, transfer);
3223
+ const transferIdSuffix = getTransferIdSuffix(polling.transfer, transfer);
3107
3224
  const statusLabel = creatingTransfer ? "Creating transfer..." : mobileFlow ? "Waiting for authorization..." : authExecutor.executing && regMsg.label ? regMsg.label : authExecutor.executing ? "Authorizing..." : transferSigning.signing ? "Preparing transfer..." : polling.isPolling ? "Processing payment..." : "Please wait...";
3108
3225
  const statusDescription = creatingTransfer ? "Setting up your transfer..." : mobileFlow ? "Complete the authorization in your wallet app, then return here." : authExecutor.executing && regMsg.description ? regMsg.description : authExecutor.executing ? "Complete the wallet prompts to authorize this payment." : transferSigning.signing ? "Waiting for backend to prepare your transfer payload..." : polling.isPolling ? "Your payment is being processed. This usually takes a few moments." : "Hang tight...";
3109
3226
  return /* @__PURE__ */ jsx("div", { style: cardStyle, children: /* @__PURE__ */ jsxs("div", { style: { textAlign: "center", padding: "16px 0" }, children: [
@@ -3131,6 +3248,39 @@ function SwypePayment({
3131
3248
  children: statusDescription
3132
3249
  }
3133
3250
  ),
3251
+ /* @__PURE__ */ jsxs(
3252
+ "p",
3253
+ {
3254
+ style: {
3255
+ marginTop: "12px",
3256
+ marginBottom: 0,
3257
+ fontSize: "0.8rem",
3258
+ color: tokens.textSecondary
3259
+ },
3260
+ children: [
3261
+ "Current status: ",
3262
+ /* @__PURE__ */ jsx("strong", { style: { color: tokens.text }, children: transferStatus }),
3263
+ " \xB7 ",
3264
+ "Transfer: ",
3265
+ /* @__PURE__ */ jsx("span", { style: { color: tokens.textMuted }, children: transferIdSuffix })
3266
+ ]
3267
+ }
3268
+ ),
3269
+ polling.error && /* @__PURE__ */ jsxs(
3270
+ "p",
3271
+ {
3272
+ style: {
3273
+ marginTop: "8px",
3274
+ marginBottom: 0,
3275
+ fontSize: "0.75rem",
3276
+ color: tokens.textMuted
3277
+ },
3278
+ children: [
3279
+ "Last polling error: ",
3280
+ polling.error
3281
+ ]
3282
+ }
3283
+ ),
3134
3284
  !mobileFlow && authExecutor.results.length > 0 && /* @__PURE__ */ jsx("div", { style: { marginTop: "20px", textAlign: "left" }, children: authExecutor.results.map((r) => /* @__PURE__ */ jsxs(
3135
3285
  "div",
3136
3286
  {