@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 8.97 kB
JavaScript
"use strict";var e=require("react/jsx-runtime"),n=require("@heroicons/react/24/outline/CreditCardIcon"),r=require("@heroicons/react/24/outline/QrCodeIcon"),t=require("@heroicons/react/24/solid/ArrowsRightLeftIcon"),a=require("react"),i=require("viem/utils"),o=require("./ModalHeader-C5Wuo3-1.js"),s=require("./FundWalletMethodHeader-BTGxq_KO.js"),d=require("./ErrorBanner-dOLclNms.js"),u=require("./InfoBanner-D5_XYHKY.js"),l=require("./GooglePay-BGccKVeZ.js"),c=require("./context-Cg3IEi80.js"),h=require("./useActiveWallet-BfMD5hzE.js"),p=require("./internal-context-BJv4f_bO.js"),g=require("./privy-context-CrM9_s6G.js"),f=require("./useWallets-dikDynAG.js"),m=require("./WalletCards-BfpeHH5n.js"),y=require("./getErc20TokenInfo-D0BUvlN2.js"),C=require("@privy-io/js-sdk-core"),x=require("@privy-io/popup"),j=require("./index-B6eKyGL-.js"),q=require("./styles-B1M_BS_A.js");function v(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("styled-components"),require("@heroicons/react/24/outline/ArrowLeftIcon"),require("@heroicons/react/24/outline/ArrowRightIcon"),require("@heroicons/react/24/outline/QuestionMarkCircleIcon"),require("@heroicons/react/24/outline/XMarkIcon"),require("@heroicons/react/24/outline/ExclamationCircleIcon"),require("@heroicons/react/24/outline/InformationCircleIcon"),require("tinycolor2"),require("zustand"),require("react-device-detect"),require("./use-export-wallet-_EIyxyle.js"),require("eventemitter3"),require("./events-context-Di6--rDg.js"),require("viem"),require("ofetch"),require("@privy-io/are-addresses-equal"),require("./analytics-C6C_4JmG.js"),require("./LinkPasskeyScreen-D9Y8PpKL.js"),require("lucide-react"),require("./TodoList-Cii_KrAk.js"),require("./ScreenLayout-C4QHZMbl.js"),require("./Screen-DWnRbzSA.js"),require("./index-ByFhjjzH.js");var F=/*#__PURE__*/v(n),w=/*#__PURE__*/v(r),b=/*#__PURE__*/v(t);const M=e=>{let[n,r]=a.useState();return a.useEffect((()=>{e().then((e=>{r(e)})).catch((()=>{}))}),[]),n};function I(e){let n=S[e];if(!n)throw new p.PrivyClientError(`Unsupported chainId: ${e} for Coinbase Onramp`);return n}let S={[C.mainnet.id]:"ethereum",[C.base.id]:"base",[C.optimism.id]:"optimism",[C.polygon.id]:"polygon",[C.arbitrum.id]:"arbitrum",[C.avalanche.id]:"avacchain",[C.monadMainnet.id]:"monad"};const W=(e,n,r,t,a,i)=>new Promise((async(o,s)=>{let d=x.trigger();if(!d)return void s(Error("Unable to initialize flow"));let u="ethereum"===n.chainType?I(n.chain.id):"solana",l=n.isUSDC?"USDC":"ethereum"===n.chainType?C.toCoinbaseAssetId(n.chain.id,"native-currency"):"SOL",c=await e.initCoinbaseOnRamp({addresses:[{address:n.address,blockchains:[u]}],assets:[l]}),{url:h}=C.getCoinbaseOnRampUrl({appId:e.getAppId(),input:c,amount:n.amount,blockchain:u,asset:l,experience:i});d.location=h.toString();let p={...a?.funding,showAlternateFundingMethod:!0};n.usingDefaultFundingMethod&&(p.usingDefaultFundingMethod=!1),r({funding:p,solanaFundingData:a?.solanaFundingData,coinbaseOnrampStatus:{popup:d}}),t("CoinbaseOnrampStatusScreen"),e.createAnalyticsEvent({eventName:"sdk_fiat_on_ramp_started",payload:{provider:"coinbase-onramp",value:n.amount,chainType:n.chainType,chainId:"ethereum"===n.chainType?n.chain.id:n.chain}}),setTimeout((()=>{r({funding:p,solanaFundingData:a?.solanaFundingData,coinbaseOnrampStatus:{partnerUserId:c.partner_user_id,popup:d}})}),5e3),o()})),T=async(e,n,r,t,a,i,o,s)=>{let d=x.trigger();if(!d)throw Error("Unable to initialize flow");let u="ethereum"===n.chainType?C.chainToMoonpayCurrency(n.chain.id,t):n.isUSDC?"USDC_SOL":"SOL",{signedUrl:l,externalTransactionId:c}=await e.signMoonpayOnRampUrl({address:n.address,useSandbox:r.fundingMethodConfig.moonpay.useSandbox??!1,config:{uiConfig:{accentColor:r.appearance.palette.accent,theme:r.appearance.palette.colorScheme},paymentMethod:s,currencyCode:u,quoteCurrencyAmount:j.amountToMoonpayAmount(n.amount)}});e.createAnalyticsEvent({eventName:"sdk_fiat_on_ramp_started",payload:{provider:"moonpay",value:n.amount,chainType:n.chainType,chainId:"ethereum"===n.chainType?n.chain.id:n.chain}}),d.location=l;let h={...o?.funding,showAlternateFundingMethod:!0};n.usingDefaultFundingMethod&&(h.usingDefaultFundingMethod=!1),a({moonpayStatus:{},funding:h,solanaFundingData:o?.solanaFundingData}),i("MoonpayStatusScreen"),setTimeout((()=>{a({moonpayStatus:{externalTransactionId:c},funding:h,solanaFundingData:o?.solanaFundingData})}),8e3)};let A=async e=>"undefined"!=typeof window&&"PaymentRequest"in window&&await new window.PaymentRequest([{supportedMethods:e}],{id:"0",total:{label:"Item",amount:{currency:"USD",value:"1.00"}}}).canMakePayment();const D=()=>A("https://apple.com/apple-pay"),k=()=>A("https://google.com/pay"),E={component:()=>{let{wallets:n}=f.useWallets(),{connectors:r}=p.usePrivyInternal(),t=r.filter(h.isSolanaWalletConnector).flatMap((e=>e.wallets)),{navigate:x,data:j,setModalData:v}=g.usePrivyModal(),{client:I}=p.usePrivyInternal(),S=c.useAppConfig(),A=j?.funding,E=M(D),L=M(k),O="solana"===A.chainType,P=O?void 0:A,U=a.useMemo((()=>((e,n,r,t,a,i)=>{let o,s,d="solana"===r.chainType,u=d?void 0:r,l=r.isUSDC?"USDC":u?.erc20Address?void 0:"native-currency",c=!!d||l&&C.isSupportedChainIdForMoonpay(Number(r.chain.id),l),h=!!d||l&&C.isSupportedChainIdForCoinbaseOnramp(Number(r.chain.id),l),p=[];for(let o of(r.preferredCardProvider&&r.supportedOptions.sort((e=>e.provider===r.preferredCardProvider?-1:1)),r.supportedOptions))"card"===o.method&&"coinbase"===o.provider&&h&&p.push((()=>W(n,r,t,a,i,"buy"))),"card"===o.method&&"moonpay"===o.provider&&c&&l&&p.push((()=>T(n,r,e,l,t,a,i,"credit_debit_card")));for(let e of r.supportedOptions)"exchange"===e.method&&"coinbase"===e.provider&&h&&(o=()=>W(n,r,t,a,i,"buy"));for(let e of i?.funding?.supportedOptions??[])"wallets"===e.method&&(s=()=>a("TransferFromWalletScreen"));return{onFundWithCard:p,onFundWithExchange:o,onFundWithWallet:s}})(S,I,A,v,x,j)),[S,I,A,j,v,x]),_=O?t.find((({address:e})=>e===A.address)):n.find((({address:e})=>i.getAddress(e)===i.getAddress(A.address))),B=h.useExternalWalletListing(_?.walletClientType||"unknown"),R=B?.name||"wallet",N=_&&"privy"!==_.walletClientType?R:S.name,z=a.useMemo((()=>A.uiConfig?.landing?.title?A.uiConfig?.landing?.title:`Add funds to your ${N?.toLowerCase().endsWith("wallet")?N:N+" wallet"}`),[A.uiConfig?.landing?.title,N]);a.useEffect((()=>{if(A?.defaultFundingMethod&&A.usingDefaultFundingMethod)switch(v({funding:{...A,usingDefaultFundingMethod:!1},solanaFundingData:j?.solanaFundingData}),A?.defaultFundingMethod){case"card":U.onFundWithCard[0]&&U.onFundWithCard[0]();break;case"exchange":U.onFundWithExchange&&U.onFundWithExchange();break;case"wallet":U.onFundWithWallet&&U.onFundWithWallet();break;case"manual":x("ManualTransferScreen")}}),[]),a.useEffect((()=>{P?.erc20Address&&!P.erc20ContractInfo&&y.getErc20TokenInfo({address:P.erc20Address,chain:P.chain,rpcConfig:S.rpcConfig,privyAppId:S.id}).then((e=>{v({...j,funding:{...P,erc20ContractInfo:e?{symbol:e.symbol,decimals:e.decimals}:void 0}})})).catch(console.error)}),[P?.erc20Address,P?.chain]);let G=!(!P?.erc20Address||P?.erc20ContractInfo);/*#__PURE__*/return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(s.t,{}),/*#__PURE__*/e.jsx("h3",{children:z}),/*#__PURE__*/e.jsxs(q.FundingMethodContainer,{children:[A.errorMessage&&/*#__PURE__*/e.jsx(d.ErrorBanner,{theme:S.appearance.palette.colorScheme,children:A.errorMessage}),U.onFundWithCard?.[0]&&/*#__PURE__*/e.jsxs(h.LoginMethodButton,{disabled:G,onClick:U.onFundWithCard[0],children:[/*#__PURE__*/e.jsx(q.IconContainer,{children:/*#__PURE__*/e.jsx(F.default,{style:{width:24}})}),"Pay with card",E?/*#__PURE__*/e.jsx(l.ApplePay,{style:{marginLeft:"auto",maxWidth:"100%",width:"auto",height:"0.875rem"}}):L?/*#__PURE__*/e.jsx(l.GooglePay,{style:{marginLeft:"auto",maxWidth:"100%",width:"auto",height:"0.875rem"}}):null]}),U.onFundWithExchange&&/*#__PURE__*/e.jsxs(h.LoginMethodButton,{disabled:G,onClick:U.onFundWithExchange,children:[/*#__PURE__*/e.jsx(q.IconContainer,{children:/*#__PURE__*/e.jsx(b.default,{style:{width:24}})}),"Transfer from an exchange"]}),U.onFundWithWallet&&/*#__PURE__*/e.jsxs(h.LoginMethodButton,{disabled:G,onClick:U.onFundWithWallet,children:[/*#__PURE__*/e.jsx(q.IconContainer,{children:/*#__PURE__*/e.jsx(m.WalletCards,{style:{width:24}})}),"Transfer from wallet"]}),/*#__PURE__*/e.jsxs(h.LoginMethodButton,{disabled:G,onClick:()=>x("ManualTransferScreen"),children:[/*#__PURE__*/e.jsx(q.IconContainer,{children:/*#__PURE__*/e.jsx(w.default,{style:{width:24}})}),"Receive funds"]}),A?.showAlternateFundingMethod&&U.onFundWithCard?.[1]&&/*#__PURE__*/e.jsx(u.InfoBanner,{theme:S.appearance.palette.colorScheme,children:/*#__PURE__*/e.jsxs(q.InfoButtonText,{children:["Having trouble or facing location restrictions?"," ",/*#__PURE__*/e.jsx(q.InfoButtonLink,{onClick:U.onFundWithCard[1],children:"Try a different provider."})]})})]}),/*#__PURE__*/e.jsx(o.BlobbyFooter,{})]})}};exports.FundingMethodSelectionScreen=E,exports.default=E;