@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 1.83 kB
JavaScript
import{ofetch as e}from"ofetch";import{useState as t,useRef as n,useEffect as a}from"react";import{M as r,v as o,w as u,x as i}from"./context-DLtU3JQy.mjs";import{u as s}from"./internal-context-Z-fyxadS.mjs";import{a as c}from"./privy-context-DrMxzgOR.mjs";import{O as d}from"./analytics-mkkvFRju.mjs";const m="moonpay";function l(e){return parseFloat(e)}function y(l,y=!1){let[f,p]=t(null),{createAnalyticsEvent:g}=s(),{data:h,navigate:v,setModalData:A}=c(),F=h?.funding,w=n(0);return a((()=>{let t=setInterval((async()=>{if(l)try{let[n]=await async function(t,n){return e(`${n?r:o}/transactions/ext/${t}`,{query:{apiKey:n?u:i}})}(l,y),a="waitingAuthorization"===n.status&&"credit_debit_card"===n.paymentMethod?"pending":n.status;if(["failed","completed","awaitingAuthorization"].includes(a)&&(g({eventName:d,payload:{status:a,provider:m,paymentMethod:n.paymentMethod,cardPaymentType:n.cardPaymentType,currency:n.currency?.code,baseCurrencyAmount:n.baseCurrencyAmount,quoteCurrencyAmount:n.quoteCurrencyAmount,feeAmount:n.feeAmount,extraFeeAmount:n.extraFeeAmount,networkFeeAmount:n.networkFeeAmount,isSandbox:y}}),clearInterval(t)),"failed"===a||"serviceFailure"===a)return A({funding:{...F,errorMessage:"Something went wrong adding funds from Moonpay. Please try again or use another method to fund your wallet."},solanaFundingData:h?.solanaFundingData}),void v("FundingMethodSelectionScreen");p(a)}catch(e){404!==e.response?.status&&(w.current+=1),w.current>=3&&(g({eventName:d,payload:{status:"serviceFailure",provider:m}}),clearInterval(t),A({funding:{...F,errorMessage:"Something went wrong adding funds from Moonpay. Please try again or use another method to fund your wallet."},solanaFundingData:h?.solanaFundingData}),v("FundingMethodSelectionScreen"))}}),3e3);return()=>clearInterval(t)}),[l,w]),f}export{l as a,y as u};