@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 1.87 kB
JavaScript
import{useState as e,useEffect as t,useContext as n,useMemo as l,useCallback as r}from"react";import{u as a,k as s}from"./events-context-ClaboIDc.mjs";import{ad as o,$ as i,F as c,cd as d,p as u,ce as f,c9 as m,cf as p}from"./EmbeddedWalletConnectingScreen-D50stmJi.mjs";import{styled as w}from"styled-components";import{aa as g}from"./internal-context-eGSwROSk.mjs";const v=n=>{let[l,r]=e("auto");return t((()=>{let e=new ResizeObserver((e=>{r(e[0]?.contentRect.height??"auto")}));return n.current&&e.observe(n.current),()=>{n.current&&e.unobserve(n.current)}}),[n.current]),l};function W(e){let{logout:t}=n(o);return a("logout",e),{logout:t}}const h=/*#__PURE__*/w.div.withConfig({displayName:"Column",componentId:"sc-cfc52180-0"})(["display:flex;flex-direction:column;"]);function y(e){let{login:t}=n(o);return a("login",e),{login:t}}function b(e){let{connectWallet:t}=n(o);return a("connectWallet",e),{connectWallet:t}}const x=e=>{a("fundWallet",e);let{fundWallet:t}=g();return{fundWallet:({address:e,options:n})=>t(e,n)}},C=()=>{let e=s(),{wallets:n}=i(),{connectors:a}=g(),o=l((()=>a.filter(c).flatMap((e=>e.wallets))),[a]),w=d((e=>e.wallet));t((()=>{if(!e.id)return;let t=u.get(f(e.id));d.setState({wallet:t})}),[e.id]);let v=l((()=>{if(!w)return;let[,e]=w.split(":");return n.find((t=>t.address===e))||o.find((t=>t.address===e))}),[w,n,o]),W=l((()=>v?"ethereum"===v.type?v.chainId:"solana":void 0),[v]);return{connect:async t=>{t?.reset&&(u.del(f(e.id)),d.setState({wallet:void 0}));let{wallet:l}=await p();if(!l)return{};let[,r]=l.split(":"),a=n.find((e=>e.address===r));if(a)return{wallet:a,network:a.chainId};let s=o.find((e=>e.address===r));return s?{wallet:s,network:"solana"}:{}},setActiveWallet:r((t=>{m({address:t.address,client:t.walletClientType,appId:e.id})}),[e.id]),wallet:v,network:W}};export{h as C,W as a,y as b,b as c,x as d,C as e,v as u};