UNPKG

@web3auth/modal

Version:

Multi chain wallet aggregator for web3Auth

73 lines (69 loc) 2.6 kB
'use strict'; var noModal = require('@web3auth/no-modal'); var react = require('react'); const WalletServicesContext = /*#__PURE__*/react.createContext(null); function WalletServicesContextProvider({ children, context }) { const web3AuthContext = react.useContext(context); const { getPlugin, isInitialized, isConnected } = web3AuthContext; const [ready, setReady] = react.useState(false); const [connecting, setConnecting] = react.useState(false); const [walletServicesPlugin, setWalletServicesPlugin] = react.useState(null); react.useEffect(() => { if (isInitialized) { const plugin = getPlugin(noModal.EVM_PLUGINS.WALLET_SERVICES); setWalletServicesPlugin(plugin); } }, [isInitialized, getPlugin]); react.useEffect(() => { if (isConnected) { const plugin = getPlugin(noModal.EVM_PLUGINS.WALLET_SERVICES); setWalletServicesPlugin(plugin); // when rehydrating, the connectedListener may be registered after the connected event is emitted, we need to check the status here if ((plugin === null || plugin === void 0 ? void 0 : plugin.status) === noModal.CONNECTOR_STATUS.CONNECTED) setReady(true); } }, [isConnected, getPlugin, walletServicesPlugin]); react.useEffect(() => { const connectedListener = () => { setReady(true); setConnecting(false); }; const disconnectedListener = () => { setReady(false); setConnecting(false); }; const connectingListener = () => { setConnecting(true); }; if (walletServicesPlugin) { walletServicesPlugin.on(noModal.PLUGIN_EVENTS.CONNECTED, connectedListener); walletServicesPlugin.on(noModal.PLUGIN_EVENTS.DISCONNECTED, disconnectedListener); walletServicesPlugin.on(noModal.PLUGIN_EVENTS.CONNECTING, connectingListener); } return () => { if (walletServicesPlugin) { walletServicesPlugin.off(noModal.PLUGIN_EVENTS.CONNECTED, connectedListener); walletServicesPlugin.off(noModal.PLUGIN_EVENTS.DISCONNECTED, disconnectedListener); walletServicesPlugin.off(noModal.PLUGIN_EVENTS.CONNECTING, connectingListener); } }; }, [walletServicesPlugin]); const value = react.useMemo(() => { return { plugin: walletServicesPlugin, ready, connecting }; }, [walletServicesPlugin, ready, connecting]); return /*#__PURE__*/react.createElement(WalletServicesContext.Provider, { value }, children); } exports.WalletServicesContext = WalletServicesContext; exports.WalletServicesContextProvider = WalletServicesContextProvider;