Skip to content

Commit

Permalink
fix hook events
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyapotti committed May 15, 2024
1 parent b17e7eb commit 00f9428
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
const { children, config } = params;
const [web3Auth, setWeb3Auth] = useState<Web3Auth | null>(null);

const [isConnected, setConnected] = useState<boolean>(false);
const [isConnected, setIsConnected] = useState<boolean>(false);
const [provider, setProvider] = useState<IProvider | null>(null);
const [userInfo, setUserInfo] = useState<Partial<OpenloginUserInfo> | null>(null);
const [isMFAEnabled, setIsMFAEnabled] = useState<boolean>(false);
Expand Down Expand Up @@ -48,7 +48,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
setProvider(null);
setUserInfo(null);
setIsMFAEnabled(false);
setConnected(false);
setIsConnected(false);
setStatus(null);
};

Expand Down Expand Up @@ -83,30 +83,28 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
}
}, [web3Auth, isConnected]);

useEffect(() => {
if (status) {
setIsInitialized(status !== ADAPTER_EVENTS.NOT_READY);
}
}, [status]);

const initModal = useCallback(
async (modalParams: { modalConfig?: Record<string, ModalConfig> } = {}) => {
if (!web3Auth) throw WalletInitializationError.notReady();
await web3Auth.initModal(modalParams);
setStatus(web3Auth.status);
},
[web3Auth]
);

useEffect(() => {
const notReadyListener = () => setStatus(ADAPTER_STATUS.NOT_READY);
const notReadyListener = () => setStatus(web3Auth.status);
const readyListener = () => {
setStatus(web3Auth.status);
setIsInitialized(true);
};
const connectedListener = () => {
setStatus(web3Auth.status);
setConnected(true);
setIsInitialized(true);
setIsConnected(true);
};
const disconnectedListener = () => {
setStatus(web3Auth.status);
setConnected(false);
setIsConnected(false);
};
const connectingListener = () => {
setStatus(web3Auth.status);
Expand All @@ -118,6 +116,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
// web3Auth is initialized here.
setStatus(web3Auth.status);
web3Auth.on(ADAPTER_EVENTS.NOT_READY, notReadyListener);
web3Auth.on(ADAPTER_EVENTS.READY, readyListener);
web3Auth.on(ADAPTER_EVENTS.CONNECTED, connectedListener);
web3Auth.on(ADAPTER_EVENTS.DISCONNECTED, disconnectedListener);
web3Auth.on(ADAPTER_EVENTS.CONNECTING, connectingListener);
Expand All @@ -127,6 +126,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
return () => {
if (web3Auth) {
web3Auth.off(ADAPTER_EVENTS.NOT_READY, notReadyListener);
web3Auth.off(ADAPTER_EVENTS.READY, readyListener);
web3Auth.off(ADAPTER_EVENTS.CONNECTED, connectedListener);
web3Auth.off(ADAPTER_EVENTS.DISCONNECTED, disconnectedListener);
web3Auth.off(ADAPTER_EVENTS.CONNECTING, connectingListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
const { children, config } = params;
const [web3Auth, setWeb3Auth] = useState<Web3AuthNoModal | null>(null);

const [isConnected, setConnected] = useState<boolean>(false);
const [isConnected, setIsConnected] = useState<boolean>(false);
const [provider, setProvider] = useState<IProvider | null>(null);
const [userInfo, setUserInfo] = useState<Partial<OpenloginUserInfo> | null>(null);
const [isMFAEnabled, setIsMFAEnabled] = useState<boolean>(false);
Expand Down Expand Up @@ -49,7 +49,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
setProvider(null);
setUserInfo(null);
setIsMFAEnabled(false);
setConnected(false);
setIsConnected(false);
setStatus(null);
};

Expand Down Expand Up @@ -84,27 +84,25 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
}
}, [web3Auth, isConnected]);

useEffect(() => {
if (status) {
setIsInitialized(status !== ADAPTER_EVENTS.NOT_READY);
}
}, [status]);

const init = useCallback(async () => {
if (!web3Auth) throw WalletInitializationError.notReady();
await web3Auth.init();
setStatus(web3Auth.status);
}, [web3Auth]);

useEffect(() => {
const notReadyListener = () => setStatus(ADAPTER_STATUS.NOT_READY);
const readyListener = () => {
setStatus(web3Auth.status);
setIsInitialized(true);
};
const connectedListener = () => {
setStatus(web3Auth.status);
setConnected(true);
setIsInitialized(true);
setIsConnected(true);
};
const disconnectedListener = () => {
setStatus(web3Auth.status);
setConnected(false);
setIsConnected(false);
};
const connectingListener = () => {
setStatus(web3Auth.status);
Expand All @@ -116,6 +114,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
// web3Auth is initialized here.
setStatus(web3Auth.status);
web3Auth.on(ADAPTER_EVENTS.NOT_READY, notReadyListener);
web3Auth.on(ADAPTER_EVENTS.READY, readyListener);
web3Auth.on(ADAPTER_EVENTS.CONNECTED, connectedListener);
web3Auth.on(ADAPTER_EVENTS.DISCONNECTED, disconnectedListener);
web3Auth.on(ADAPTER_EVENTS.CONNECTING, connectingListener);
Expand All @@ -125,6 +124,7 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
return () => {
if (web3Auth) {
web3Auth.off(ADAPTER_EVENTS.NOT_READY, notReadyListener);
web3Auth.off(ADAPTER_EVENTS.READY, readyListener);
web3Auth.off(ADAPTER_EVENTS.CONNECTED, connectedListener);
web3Auth.off(ADAPTER_EVENTS.DISCONNECTED, disconnectedListener);
web3Auth.off(ADAPTER_EVENTS.CONNECTING, connectingListener);
Expand Down Expand Up @@ -168,7 +168,6 @@ export function Web3AuthInnerProvider(params: PropsWithChildren<Web3AuthProvider
async (chainConfig: CustomChainConfig) => {
if (!web3Auth) throw WalletInitializationError.notReady();
await web3Auth.addChain(chainConfig);

await web3Auth.switchChain({ chainId: chainConfig.chainId });
},
[web3Auth]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import { IWalletServicesContext } from "../interfaces";
export const WalletServicesContext = createContext<IWalletServicesContext>(null);

export function WalletServicesContextProvider<T extends IBaseWeb3AuthHookContext>({ children, context }: PropsWithChildren<{ context: Context<T> }>) {
const [isConnected, setIsConnected] = useState<boolean>(false);
const [isPluginConnected, setIsPluginConnected] = useState<boolean>(false);
const [walletServicesPlugin, setWalletServicesPlugin] = useState<WalletServicesPlugin>(null);
const web3AuthContext = useContext(context);

useEffect(() => {
const connectedListener = () => {
setIsConnected(true);
setIsPluginConnected(true);
};

const disconnectedListener = () => {
setIsConnected(false);
setIsPluginConnected(false);
};

const { getPlugin, isInitialized } = web3AuthContext;
Expand All @@ -42,34 +42,34 @@ export function WalletServicesContextProvider<T extends IBaseWeb3AuthHookContext

const showWalletConnectScanner = useCallback(async () => {
if (!walletServicesPlugin) throw WalletServicesPluginError.notInitialized();
if (!isConnected) throw WalletServicesPluginError.web3AuthNotConnected();
if (!isPluginConnected) throw WalletServicesPluginError.web3AuthNotConnected();

return walletServicesPlugin.showWalletConnectScanner();
}, [walletServicesPlugin, isConnected]);
}, [walletServicesPlugin, isPluginConnected]);

const showWalletUI = useCallback(async () => {
if (!walletServicesPlugin) throw WalletServicesPluginError.notInitialized();
if (!isConnected) throw WalletServicesPluginError.web3AuthNotConnected();
if (!isPluginConnected) throw WalletServicesPluginError.web3AuthNotConnected();

return walletServicesPlugin.showWalletUi();
}, [walletServicesPlugin, isConnected]);
}, [walletServicesPlugin, isPluginConnected]);

const showCheckout = useCallback(async () => {
if (!walletServicesPlugin) throw WalletServicesPluginError.notInitialized();
if (!isConnected) throw WalletServicesPluginError.web3AuthNotConnected();
if (!isPluginConnected) throw WalletServicesPluginError.web3AuthNotConnected();

return walletServicesPlugin.showCheckout();
}, [walletServicesPlugin, isConnected]);
}, [walletServicesPlugin, isPluginConnected]);

const value = useMemo(() => {
return {
plugin: walletServicesPlugin,
isConnected,
isConnected: isPluginConnected,
showWalletConnectScanner,
showCheckout,
showWalletUI,
};
}, [walletServicesPlugin, isConnected, showWalletConnectScanner, showCheckout, showWalletUI]);
}, [walletServicesPlugin, isPluginConnected, showWalletConnectScanner, showCheckout, showWalletUI]);

return createElement(WalletServicesContext.Provider, { value }, children);
}

0 comments on commit 00f9428

Please sign in to comment.