@portkey/connect-web-wallet
Version:
3 lines (2 loc) • 4.89 kB
JavaScript
import{jsx as e,jsxs as t}from"react/jsx-runtime";import{Button as n,Drawer as o,Modal as r,ConfigProvider as a}from"antd";import{createContext as s,useContext as i,useReducer as l,useMemo as c,useCallback as p,useRef as d,useEffect as u,useState as f,useLayoutEffect as m}from"react";import{InpagePostStream as y,InitializeProvider as E}from"@portkey/iframe-provider";import w from"qs";import{TelegramPlatform as h,devices as T,sleep as W}from"@portkey/utils";import{NotificationEvents as g,MethodsBase as I,MethodsWallet as L}from"@portkey/provider-types";import{useMedia as v}from"react-use";function S(e,t){return{type:e,payload:t}}const k="SET_DIALOG_VISIBLE",C="DESTROY",A={type:k,actions:e=>S(k,{dialogVisible:e})},N={type:C,actions:()=>S(C)},O={},P=s(O);function _(){return i(P)}function b(e,{type:t,payload:n}){if(t===N.type)return{};{const{destroy:t}=n;return t?Object.assign({},n):Object.assign({},e,n)}}function x({children:t}){const[n,o]=l(b,O);return e(P.Provider,{value:c(()=>[{...n},{dispatch:o}],[n,o]),children:t})}function R(){const[,{dispatch:e}]=_();return p(e,[e])}function D(){const t=R(),o=p(()=>{t(A.actions(!0))},[t]);return e("div",{children:e(n,{onClick:o,children:"Open Dialog"})})}const M="SET_WALLET_PROVIDER",G="DESTROY",j={type:M,actions:e=>S(M,{provider:e,connectionStatus:"connecting"})};function B(){return null}const V={MAINNET:"https://portkey-web-wallet.portkey.finance",TESTNET:"https://test-portkey-web-wallet.portkey.finance/"};function Y(){const t=d(null),n=function(){const[,{dispatch:e}]=$();return p(e,[e])}(),o=R(),[{options:r,provider:a}]=$(),s=p(()=>{if(!t.current)return;const e=new y({name:"PORTKEY_WEB_WALLET_INGAGE",targetWindow:t.current.contentWindow});new E({connectionStream:e})},[]),i=p(e=>{o(A.actions(e))},[o]),l=p(e=>{e.on(g.WALLET_VISIBLE,i)},[i]),f=p(e=>{e.removeListener(g.WALLET_VISIBLE,i)},[i]);u(()=>()=>{a&&f(a)},[a,f]),u(()=>{const e=()=>{const e=window.PortkeyWebWallet;n(j.actions(e)),l(e)};return window.addEventListener("portkeyWebWalletInitEvent",e),()=>{window.addEventListener("portkeyWebWalletInitEvent",e)}},[n,l,f]);const m=c(()=>({...r,isTelegram:h.isTelegramPlatform()}),[r]),T=c(()=>V[r.networkType]?V[r.networkType]:V.MAINNET,[r.networkType]);return e("iframe",{ref:t,src:`${T}?${w.stringify(m)}`,style:{width:"100%",height:"700px"},onLoad:s})}function U(){const[{dialogVisible:t}]=_(),n=function(){const e=v("(max-width: 600px)");return c(()=>e||T.isMobileDevices()||h.isTelegramPlatform(),[e])}(),a=c(()=>e(Y,{}),[]);return n?e(o,{className:"portkey-connect-wallet-modal portkey-connect-wallet-drawer",placement:"bottom",height:"95vh",forceRender:!0,open:t,children:a}):e(r,{prefixCls:"portkey-connect-modal",className:"portkey-connect-wallet-modal",wrapClassName:"portkey-connect-wallet-modal-wrapper",forceRender:!0,open:t,style:{height:700},footer:null,closable:!1,children:a})}function q({children:t}){return e(a,{prefixCls:"portkey-connect",children:t})}function F(){return e(q,{children:e(U,{})})}const J={options:{theme:"light",networkType:"TESTNET",appId:"wallet-appId"},provider:null},K=s(J);function $(){return i(K)}function z(e,{type:t,payload:n}){return t===G?J:Object.assign({},e,n)}function H({children:n,options:o}){const[r,a]=l(z,J);return e(K.Provider,{value:c(()=>[{...r,options:o},{dispatch:a}],[o,r]),children:t(x,{children:[e(F,{}),e(B,{}),n]})})}const Q="PortkeyWebWalletWalletInfo";class X{static getWalletInfo(){const e=localStorage.getItem(Q);return e?JSON.parse(e):null}static setWalletInfo(e){return localStorage.setItem(Q,JSON.stringify(e))}static resetWalletInfo(){return localStorage.removeItem(Q)}}var Z,ee=((Z=ee||{}).APPLE="Apple",Z.GOOGLE="Google",Z.TELEGRAM="Telegram",Z);const te=()=>{const[{provider:e}]=$(),[t,n]=f(),o=(()=>{const[{provider:e}]=$(),t=R();return p(t=>{if(!e)throw"Wallet not init";return e?.isConnected(),e?.request(t)},[t,e])})(),r=p(async()=>{if(!e?.isConnected())throw"Please connect wallet";await W(1e3);const t=await o({method:I.WALLET_INFO});X.setWalletInfo(t),n(t)},[e,o]),a=p(async t=>{if(!e)throw"Please init wallet provider";const n=await o({method:L.GET_WALLET_STATE});if(n?.isConnected&&n?.isLogged)return o({method:I.ACCOUNTS});const a=await o({method:I.REQUEST_ACCOUNTS,payload:t});return r(),a},[r,e,o]),s=p(async()=>{if(!e?.isConnected())return null;const t=await o({method:L.WALLET_DISCONNECT});return X.resetWalletInfo(),n(null),t},[e,o]),i=p(async()=>{if(!e?.isConnected())throw"Please connect wallet";await o({method:L.WALLET_SHOW_ASSETS})},[e,o]);return m(()=>{const e=X.getWalletInfo();n(e)},[]),c(()=>({provider:e,connect:a,disconnect:s,showAsset:i,walletInfo:t}),[a,s,e,i,t])},ne=()=>X.getWalletInfo();export{Q as CWW_WALLET_INFO_KEY,D as DialogExample,H as PortkeyWebWalletProvider,ee as SocialLoginType,X as WalletInfoControl,ne as getCachedWalletInfo,te as useConnect,$ as useWebWallet};
//# sourceMappingURL=index.js.map