@orderly.network/ui-transfer 3.1.8 → 3.1.9-alpha.1

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
@@ -2395,11 +2395,16 @@ function useDepositAction(options2) {
2395
2395
  const { quantity, approve, deposit, swapDeposit, onSuccess, needSwap } = options2;
2396
2396
  const [isMutating, setIsMutating] = react.useState(false);
2397
2397
  const [depositError, setDepositError] = react.useState("");
2398
+ const mutationLock = react.useRef(false);
2398
2399
  const ee = hooks.useEventEmitter();
2399
2400
  const { t } = i18n.useTranslation();
2400
- const doDeposit = react.useCallback(async () => {
2401
+ const runDeposit = react.useCallback(async () => {
2401
2402
  try {
2402
- await deposit();
2403
+ if (needSwap) {
2404
+ await swapDeposit?.();
2405
+ } else {
2406
+ await deposit();
2407
+ }
2403
2408
  setDepositError("");
2404
2409
  } catch (err) {
2405
2410
  console.error("orderly deposit error", err);
@@ -2422,57 +2427,94 @@ function useDepositAction(options2) {
2422
2427
  }
2423
2428
  throw err;
2424
2429
  }
2425
- }, [deposit, onSuccess, t, ee]);
2426
- const onDeposit = react.useCallback(async () => {
2430
+ }, [deposit, needSwap, swapDeposit, t]);
2431
+ const runApprove = react.useCallback(async () => {
2432
+ try {
2433
+ await approve(quantity);
2434
+ ui.toast.success(t("transfer.deposit.approve.success"));
2435
+ } catch (err) {
2436
+ console.error("approve error", err);
2437
+ ui.toast.error(
2438
+ err.message || err?.errorCode || t("transfer.deposit.approve.failed")
2439
+ );
2440
+ throw err;
2441
+ }
2442
+ }, [approve, quantity, t]);
2443
+ const isValidQuantity = react.useCallback(() => {
2427
2444
  const num = Number(quantity);
2428
2445
  if (isNaN(num) || num <= 0) {
2429
2446
  ui.toast.error(t("transfer.quantity.invalid"));
2430
- return;
2447
+ return false;
2448
+ }
2449
+ return true;
2450
+ }, [quantity, t]);
2451
+ const startMutation = react.useCallback(() => {
2452
+ if (mutationLock.current) {
2453
+ return false;
2431
2454
  }
2432
- if (isMutating) return;
2455
+ mutationLock.current = true;
2433
2456
  setIsMutating(true);
2457
+ return true;
2458
+ }, []);
2459
+ const finishMutation = react.useCallback(() => {
2460
+ mutationLock.current = false;
2461
+ setIsMutating(false);
2462
+ }, []);
2463
+ const onDeposit = react.useCallback(async () => {
2464
+ if (mutationLock.current) return;
2465
+ if (!isValidQuantity() || !startMutation()) {
2466
+ return;
2467
+ }
2434
2468
  try {
2435
- if (needSwap) {
2436
- await swapDeposit?.();
2437
- } else {
2438
- await doDeposit();
2439
- }
2469
+ await runDeposit();
2440
2470
  ui.toast.success(t("transfer.deposit.requested"));
2441
2471
  ee.emit("deposit:requested");
2442
2472
  onSuccess?.();
2443
2473
  } catch (err) {
2444
2474
  } finally {
2445
- setIsMutating(false);
2475
+ finishMutation();
2446
2476
  }
2447
- }, [quantity, isMutating, needSwap, doDeposit, swapDeposit, t]);
2477
+ }, [
2478
+ ee,
2479
+ finishMutation,
2480
+ isValidQuantity,
2481
+ onSuccess,
2482
+ runDeposit,
2483
+ startMutation,
2484
+ t
2485
+ ]);
2448
2486
  const onApprove = react.useCallback(async () => {
2449
- if (isMutating) return;
2450
- setIsMutating(true);
2487
+ if (!startMutation()) return;
2451
2488
  try {
2452
- await approve(quantity);
2453
- ui.toast.success(t("transfer.deposit.approve.success"));
2454
- } catch (err) {
2455
- console.error("approve error", err);
2456
- ui.toast.error(
2457
- err.message || err?.errorCode || t("transfer.deposit.approve.failed")
2458
- );
2459
- throw err;
2489
+ await runApprove();
2460
2490
  } finally {
2461
- setIsMutating(false);
2491
+ finishMutation();
2462
2492
  }
2463
- }, [approve, isMutating, quantity, t]);
2493
+ }, [finishMutation, runApprove, startMutation]);
2464
2494
  const onApproveAndDeposit = react.useCallback(async () => {
2465
- if (isMutating) return;
2466
- setIsMutating(true);
2495
+ if (mutationLock.current) return;
2496
+ if (!isValidQuantity() || !startMutation()) return;
2467
2497
  try {
2468
- await onApprove();
2469
- await onDeposit();
2498
+ await runApprove();
2499
+ await runDeposit();
2500
+ ui.toast.success(t("transfer.deposit.requested"));
2501
+ ee.emit("deposit:requested");
2502
+ onSuccess?.();
2470
2503
  } catch (err) {
2471
2504
  console.error("approve and deposit error", err);
2472
2505
  } finally {
2473
- setIsMutating(false);
2506
+ finishMutation();
2474
2507
  }
2475
- }, [isMutating, onApprove, onDeposit]);
2508
+ }, [
2509
+ ee,
2510
+ finishMutation,
2511
+ isValidQuantity,
2512
+ onSuccess,
2513
+ runApprove,
2514
+ runDeposit,
2515
+ startMutation,
2516
+ t
2517
+ ]);
2476
2518
  return {
2477
2519
  isMutating,
2478
2520
  depositError,