@b3dotfun/sdk 0.0.65-alpha.0 → 0.0.65-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.
Files changed (28) hide show
  1. package/dist/cjs/anyspend/react/components/AnySpend.js +37 -23
  2. package/dist/cjs/anyspend/react/components/AnySpendCustom.js +22 -9
  3. package/dist/cjs/anyspend/react/components/AnySpendCustomExactIn.js +6 -6
  4. package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.d.ts +5 -1
  5. package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.js +15 -9
  6. package/dist/cjs/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.d.ts +7 -5
  7. package/dist/cjs/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.js +13 -9
  8. package/dist/cjs/anyspend/react/hooks/useCryptoPaymentMethodState.d.ts +42 -0
  9. package/dist/cjs/anyspend/react/hooks/useCryptoPaymentMethodState.js +51 -0
  10. package/dist/esm/anyspend/react/components/AnySpend.js +37 -23
  11. package/dist/esm/anyspend/react/components/AnySpendCustom.js +22 -9
  12. package/dist/esm/anyspend/react/components/AnySpendCustomExactIn.js +6 -6
  13. package/dist/esm/anyspend/react/hooks/useAnyspendFlow.d.ts +5 -1
  14. package/dist/esm/anyspend/react/hooks/useAnyspendFlow.js +15 -9
  15. package/dist/esm/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.d.ts +7 -5
  16. package/dist/esm/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.js +13 -9
  17. package/dist/esm/anyspend/react/hooks/useCryptoPaymentMethodState.d.ts +42 -0
  18. package/dist/esm/anyspend/react/hooks/useCryptoPaymentMethodState.js +48 -0
  19. package/dist/types/anyspend/react/hooks/useAnyspendFlow.d.ts +5 -1
  20. package/dist/types/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.d.ts +7 -5
  21. package/dist/types/anyspend/react/hooks/useCryptoPaymentMethodState.d.ts +42 -0
  22. package/package.json +1 -1
  23. package/src/anyspend/react/components/AnySpend.tsx +49 -28
  24. package/src/anyspend/react/components/AnySpendCustom.tsx +28 -15
  25. package/src/anyspend/react/components/AnySpendCustomExactIn.tsx +7 -6
  26. package/src/anyspend/react/hooks/useAnyspendFlow.ts +23 -11
  27. package/src/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.ts +20 -12
  28. package/src/anyspend/react/hooks/useCryptoPaymentMethodState.ts +71 -0
@@ -5,10 +5,12 @@ import { useConnectedWalletDisplay } from "./useConnectedWalletDisplay";
5
5
  interface UseAutoSelectCryptoPaymentMethodParams {
6
6
  /** Current payment type (crypto or fiat) */
7
7
  paymentType?: "crypto" | "fiat";
8
- /** Currently selected payment method */
8
+ /** Auto-selected payment method based on balance (not used in hook logic, but part of state management) */
9
+ cryptoPaymentMethod: CryptoPaymentMethodType;
10
+ /** Function to update the auto-selected payment method */
11
+ setCryptoPaymentMethod: (method: CryptoPaymentMethodType) => void;
12
+ /** User explicitly selected payment method (NONE means no explicit selection) */
9
13
  selectedCryptoPaymentMethod: CryptoPaymentMethodType;
10
- /** Function to update the selected payment method */
11
- setSelectedCryptoPaymentMethod: (method: CryptoPaymentMethodType) => void;
12
14
  /** Whether user has enough balance to pay */
13
15
  hasEnoughBalance: boolean;
14
16
  /** Whether balance is still loading */
@@ -20,7 +22,7 @@ interface UseAutoSelectCryptoPaymentMethodParams {
20
22
  * based on available wallets and balance.
21
23
  *
22
24
  * Auto-selection logic:
23
- * - Only auto-selects when payment method is NONE (doesn't override user choices)
25
+ * - Only auto-selects when selectedCryptoPaymentMethod is NONE (user hasn't explicitly chosen)
24
26
  * - If EOA/Wagmi wallet connected + has balance → CONNECT_WALLET
25
27
  * - If EOA/Wagmi wallet connected + insufficient balance → TRANSFER_CRYPTO
26
28
  * - If only Global wallet available → GLOBAL_WALLET
@@ -28,8 +30,9 @@ interface UseAutoSelectCryptoPaymentMethodParams {
28
30
  */
29
31
  export function useAutoSelectCryptoPaymentMethod({
30
32
  paymentType = "crypto",
33
+ cryptoPaymentMethod: _cryptoPaymentMethod,
34
+ setCryptoPaymentMethod,
31
35
  selectedCryptoPaymentMethod,
32
- setSelectedCryptoPaymentMethod,
33
36
  hasEnoughBalance,
34
37
  isBalanceLoading,
35
38
  }: UseAutoSelectCryptoPaymentMethodParams) {
@@ -37,8 +40,13 @@ export function useAutoSelectCryptoPaymentMethod({
37
40
  const { suggestedPaymentMethod } = useConnectedWalletDisplay(selectedCryptoPaymentMethod);
38
41
 
39
42
  useEffect(() => {
40
- // Only auto-select when on crypto payment type and payment method is NONE
41
- if (paymentType !== "crypto" || selectedCryptoPaymentMethod !== CryptoPaymentMethodType.NONE) {
43
+ // Only auto-select when on crypto payment type
44
+ if (paymentType !== "crypto") {
45
+ return;
46
+ }
47
+
48
+ // Only auto-switch if user hasn't explicitly selected a payment method
49
+ if (selectedCryptoPaymentMethod !== CryptoPaymentMethodType.NONE) {
42
50
  return;
43
51
  }
44
52
 
@@ -48,25 +56,25 @@ export function useAutoSelectCryptoPaymentMethod({
48
56
  // Otherwise, default to TRANSFER_CRYPTO if balance is insufficient
49
57
  if (!isBalanceLoading) {
50
58
  if (hasEnoughBalance && suggestedPaymentMethod === CryptoPaymentMethodType.CONNECT_WALLET) {
51
- setSelectedCryptoPaymentMethod(CryptoPaymentMethodType.CONNECT_WALLET);
59
+ setCryptoPaymentMethod(CryptoPaymentMethodType.CONNECT_WALLET);
52
60
  } else if (!hasEnoughBalance && suggestedPaymentMethod === CryptoPaymentMethodType.CONNECT_WALLET) {
53
61
  // Wallet connected but insufficient balance - suggest transfer
54
- setSelectedCryptoPaymentMethod(CryptoPaymentMethodType.TRANSFER_CRYPTO);
62
+ setCryptoPaymentMethod(CryptoPaymentMethodType.TRANSFER_CRYPTO);
55
63
  } else {
56
64
  // Use suggested method (e.g., GLOBAL_WALLET)
57
- setSelectedCryptoPaymentMethod(suggestedPaymentMethod);
65
+ setCryptoPaymentMethod(suggestedPaymentMethod);
58
66
  }
59
67
  } else {
60
68
  // Balance still loading, use suggested method
61
- setSelectedCryptoPaymentMethod(suggestedPaymentMethod);
69
+ setCryptoPaymentMethod(suggestedPaymentMethod);
62
70
  }
63
71
  }
64
72
  }, [
65
73
  paymentType,
74
+ setCryptoPaymentMethod,
66
75
  selectedCryptoPaymentMethod,
67
76
  suggestedPaymentMethod,
68
77
  hasEnoughBalance,
69
78
  isBalanceLoading,
70
- setSelectedCryptoPaymentMethod,
71
79
  ]);
72
80
  }
@@ -0,0 +1,71 @@
1
+ import { useState } from "react";
2
+ import { CryptoPaymentMethodType } from "../components/common/CryptoPaymentMethod";
3
+
4
+ interface UseCryptoPaymentMethodStateResult {
5
+ /** Auto-selected payment method based on balance */
6
+ cryptoPaymentMethod: CryptoPaymentMethodType;
7
+ /** Function to update the auto-selected payment method */
8
+ setCryptoPaymentMethod: (method: CryptoPaymentMethodType) => void;
9
+ /** User explicitly selected payment method (NONE means no explicit selection) */
10
+ selectedCryptoPaymentMethod: CryptoPaymentMethodType;
11
+ /** Function to update the user-selected payment method */
12
+ setSelectedCryptoPaymentMethod: (method: CryptoPaymentMethodType) => void;
13
+ /** Effective payment method (user selection takes priority over auto-selection) */
14
+ effectiveCryptoPaymentMethod: CryptoPaymentMethodType;
15
+ /** Reset both payment method states to NONE */
16
+ resetPaymentMethods: () => void;
17
+ }
18
+
19
+ /**
20
+ * Custom hook to manage crypto payment method state with dual-state system:
21
+ *
22
+ * - `cryptoPaymentMethod`: Auto-selected based on wallet availability and balance
23
+ * - `selectedCryptoPaymentMethod`: Explicitly selected by user
24
+ * - `effectiveCryptoPaymentMethod`: User selection takes priority over auto-selection
25
+ *
26
+ * This allows automatic payment method suggestions while respecting explicit user choices.
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * const {
31
+ * cryptoPaymentMethod,
32
+ * setCryptoPaymentMethod,
33
+ * selectedCryptoPaymentMethod,
34
+ * setSelectedCryptoPaymentMethod,
35
+ * effectiveCryptoPaymentMethod,
36
+ * resetPaymentMethods
37
+ * } = useCryptoPaymentMethodState();
38
+ *
39
+ * // Use effectiveCryptoPaymentMethod for display
40
+ * // Use setSelectedCryptoPaymentMethod when user explicitly selects
41
+ * // Call resetPaymentMethods when switching tabs or going back
42
+ * ```
43
+ */
44
+ export function useCryptoPaymentMethodState(): UseCryptoPaymentMethodStateResult {
45
+ // cryptoPaymentMethod: auto-selected based on balance
46
+ const [cryptoPaymentMethod, setCryptoPaymentMethod] = useState<CryptoPaymentMethodType>(CryptoPaymentMethodType.NONE);
47
+
48
+ // selectedCryptoPaymentMethod: explicitly selected by user (NONE means no explicit selection)
49
+ const [selectedCryptoPaymentMethod, setSelectedCryptoPaymentMethod] = useState<CryptoPaymentMethodType>(
50
+ CryptoPaymentMethodType.NONE,
51
+ );
52
+
53
+ // The effective payment method (user selection takes priority over auto-selection)
54
+ const effectiveCryptoPaymentMethod =
55
+ selectedCryptoPaymentMethod !== CryptoPaymentMethodType.NONE ? selectedCryptoPaymentMethod : cryptoPaymentMethod;
56
+
57
+ // Helper function to reset both states
58
+ const resetPaymentMethods = () => {
59
+ setCryptoPaymentMethod(CryptoPaymentMethodType.NONE);
60
+ setSelectedCryptoPaymentMethod(CryptoPaymentMethodType.NONE);
61
+ };
62
+
63
+ return {
64
+ cryptoPaymentMethod,
65
+ setCryptoPaymentMethod,
66
+ selectedCryptoPaymentMethod,
67
+ setSelectedCryptoPaymentMethod,
68
+ effectiveCryptoPaymentMethod,
69
+ resetPaymentMethods,
70
+ };
71
+ }