UNPKG

@daimo/pay

Version:

Seamless crypto payments. Onboard users from any chain, any coin into your app with one click.

61 lines (58 loc) 2.86 kB
import { ExternalPaymentOptions } from '@daimo/pay-common'; import { useState, useEffect } from 'react'; const DEFAULT_EXTERNAL_PAYMENT_OPTIONS = Object.values(ExternalPaymentOptions).filter((opt) => // Solana and ExternalChains are handled in the SelectMethod component. opt !== ExternalPaymentOptions.Solana && opt !== ExternalPaymentOptions.ExternalChains && opt !== ExternalPaymentOptions.Daimo); function useExternalPaymentOptions({ trpc, filterIds, platform, usdRequired, mode, }) { const [options, setOptions] = useState(new Map()); const [loading, setLoading] = useState(false); useEffect(() => { const refreshExternalPaymentOptions = async (usd, mode) => { if (!platform) return; setLoading(true); try { const newOptions = await trpc.getExternalPaymentOptions.query({ platform, mode, usdRequired: usd, }); // Filter out options not in options JSON const enabledExtPaymentOptions = filterIds || DEFAULT_EXTERNAL_PAYMENT_OPTIONS; const hasAllPaymentApps = enabledExtPaymentOptions.includes(ExternalPaymentOptions.AllPaymentApps); const hasAllExchanges = enabledExtPaymentOptions.includes(ExternalPaymentOptions.AllExchanges); const filteredOptions = newOptions.filter((option) => enabledExtPaymentOptions.includes(option.id) || (hasAllPaymentApps && option.optionType === "zkp2p") || (hasAllExchanges && option.optionType === "exchange")); const optionsByType = new Map(); filteredOptions.forEach((option) => { const { optionType } = option; if (!optionsByType.has(optionType)) { optionsByType.set(optionType, []); } optionsByType.get(optionType).push(option); }); setOptions(optionsByType); } catch (error) { console.error(error); } finally { setLoading(false); } }; if (usdRequired != null && mode != null) { refreshExternalPaymentOptions(usdRequired, mode); } // TODO: this is an ugly way to handle polling/refresh // Notice the load-bearing JSON.stringify() to prevent a visible infinite // refresh glitch on the SelectMethod screen. Replace this useEffect(). // // eslint-disable-next-line react-hooks/exhaustive-deps }, [usdRequired, JSON.stringify(filterIds), platform, mode, trpc]); return { options, loading }; } export { useExternalPaymentOptions }; //# sourceMappingURL=useExternalPaymentOptions.js.map