@swype-org/react-sdk 0.1.11 → 0.1.13

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;
@@ -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;
@@ -2194,19 +2250,22 @@ function SwypePayment({
2194
2250
  void sign();
2195
2251
  }, [mobileFlow, polling.transfer, transferSigning, onError]);
2196
2252
  useEffect(() => {
2197
- if (!mobileFlow || !polling.isPolling) return;
2253
+ if (!mobileFlow) return;
2254
+ const transferIdToResume = pollingTransferIdRef.current ?? transfer?.id;
2255
+ if (!transferIdToResume) return;
2256
+ if (!polling.isPolling) {
2257
+ polling.startPolling(transferIdToResume);
2258
+ }
2198
2259
  const handleVisibility = () => {
2199
2260
  if (document.visibilityState === "visible") {
2200
- if (pollingTransferIdRef.current) {
2201
- polling.startPolling(pollingTransferIdRef.current);
2202
- }
2261
+ polling.startPolling(transferIdToResume);
2203
2262
  }
2204
2263
  };
2205
2264
  document.addEventListener("visibilitychange", handleVisibility);
2206
2265
  return () => {
2207
2266
  document.removeEventListener("visibilitychange", handleVisibility);
2208
2267
  };
2209
- }, [mobileFlow, polling]);
2268
+ }, [mobileFlow, transfer?.id, polling.isPolling, polling.startPolling]);
2210
2269
  const pendingSelectSourceAction = authExecutor.pendingSelectSource;
2211
2270
  const selectSourceChoices = useMemo(() => {
2212
2271
  if (!pendingSelectSourceAction) return [];
@@ -2260,6 +2319,7 @@ function SwypePayment({
2260
2319
  return;
2261
2320
  }
2262
2321
  setStep("processing");
2322
+ processingStartedAtRef.current = Date.now();
2263
2323
  setError(null);
2264
2324
  setCreatingTransfer(true);
2265
2325
  setMobileFlow(false);
@@ -2345,6 +2405,7 @@ function SwypePayment({
2345
2405
  setError(null);
2346
2406
  setAmount(depositAmount != null ? depositAmount.toString() : "");
2347
2407
  setMobileFlow(false);
2408
+ processingStartedAtRef.current = null;
2348
2409
  pollingTransferIdRef.current = null;
2349
2410
  mobileSigningTransferIdRef.current = null;
2350
2411
  setConnectingNewAccount(false);
@@ -3101,6 +3162,8 @@ function SwypePayment({
3101
3162
  }
3102
3163
  };
3103
3164
  const regMsg = getRegistrationMessage();
3165
+ const transferStatus = getDisplayTransferStatus(polling.transfer, transfer);
3166
+ const transferIdSuffix = getTransferIdSuffix(polling.transfer, transfer);
3104
3167
  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...";
3105
3168
  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...";
3106
3169
  return /* @__PURE__ */ jsx("div", { style: cardStyle, children: /* @__PURE__ */ jsxs("div", { style: { textAlign: "center", padding: "16px 0" }, children: [
@@ -3128,6 +3191,39 @@ function SwypePayment({
3128
3191
  children: statusDescription
3129
3192
  }
3130
3193
  ),
3194
+ /* @__PURE__ */ jsxs(
3195
+ "p",
3196
+ {
3197
+ style: {
3198
+ marginTop: "12px",
3199
+ marginBottom: 0,
3200
+ fontSize: "0.8rem",
3201
+ color: tokens.textSecondary
3202
+ },
3203
+ children: [
3204
+ "Current status: ",
3205
+ /* @__PURE__ */ jsx("strong", { style: { color: tokens.text }, children: transferStatus }),
3206
+ " \xB7 ",
3207
+ "Transfer: ",
3208
+ /* @__PURE__ */ jsx("span", { style: { color: tokens.textMuted }, children: transferIdSuffix })
3209
+ ]
3210
+ }
3211
+ ),
3212
+ polling.error && /* @__PURE__ */ jsxs(
3213
+ "p",
3214
+ {
3215
+ style: {
3216
+ marginTop: "8px",
3217
+ marginBottom: 0,
3218
+ fontSize: "0.75rem",
3219
+ color: tokens.textMuted
3220
+ },
3221
+ children: [
3222
+ "Last polling error: ",
3223
+ polling.error
3224
+ ]
3225
+ }
3226
+ ),
3131
3227
  !mobileFlow && authExecutor.results.length > 0 && /* @__PURE__ */ jsx("div", { style: { marginTop: "20px", textAlign: "left" }, children: authExecutor.results.map((r) => /* @__PURE__ */ jsxs(
3132
3228
  "div",
3133
3229
  {