and for connectionâŚ
// import { useWeb3Auth } from â@web3auth/modal-react-hooksâ;
import { useWeb3Auth } from â@web3auth/modal/reactâ;
import Image from ânext/imageâ;
import Link from ânext/linkâ;
import { usePathname } from ânext/navigationâ;
import { FC, useEffect, useState } from âreactâ;
import { Button } from â@/components/shared/buttons/buttonâ;
import ProfileDropdown from â@/components/shared/dropdowns/profile-dropdownâ;
import { toaster } from â@/components/shared/toasterâ;
import { Typography } from â@/components/shared/typographyâ;
import { content } from â@/constantsâ;
import { useConnectWallet } from â@/hooks/api/patient/useConnectWalletâ;
import { useAppSelector } from â@/redux/hooksâ;
import {
copyAddressToClipboard,
handleGetPrivateKey,
handleGetPublicKey,
handleWalletAddress,
handleWalletBalance,
truncateAddress,
} from â@/utils/web3-auth-helpersâ;
import MobileHeader from ââŚ/mobile-headerâ;
const MainHeader: FC = () => {
const pathname = usePathname();
const [showInfo, setShowInfo] = useState(false);
const showMobileHeader = useAppSelector((state) => state.app.showMobileHeader);
const userData = useAppSelector((state) => state.auth.user);
// â
const [walletAddress, setWalletAddress] = useState<string | null>(null);
const [walletBalance, setWalletBalance] = useState<string | null>(null);
const [walletPublicKey, setWalletPublicKey] = useState<string | null>(null);
const { web3Auth, provider, isConnected, status } = useWeb3Auth();
const { mutate } = useConnectWallet();
const getWalletAddress = async () => {
const walletAddress = await handleWalletAddress(provider);
setWalletAddress(walletAddress ?? null);
return walletAddress ?? null;
};
const getPublicKey = async () => {
const publicKey = await handleGetPublicKey(provider);
setWalletPublicKey(publicKey ?? null);
return publicKey ?? null;
};
const getWalletBalance = async () => {
const balanceInEth = await handleWalletBalance(provider, walletAddress);
setWalletBalance(balanceInEth ?? null);
};
useEffect(() => {
if (isConnected) {
if (
walletAddress &&
walletPublicKey &&
walletBalance &&
userData?.walletAddress === ââ &&
userData?.walletPublicKey === ââ
) {
mutate({
publicKey: walletPublicKey,
walletAddress,
});
} else {
getWalletAddress();
getPublicKey();
getWalletBalance();
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
isConnected,
walletAddress,
walletPublicKey,
walletBalance,
provider,
userData?.walletAddress,
userData?.walletPublicKey,
]);
const handleConnect = async () => {
try {
if (!isConnected) {
await web3Auth?.connect();
toaster(âWallet connectedâ, âsuccessâ);
} else {
copyAddressToClipboard(walletAddress || ââ);
await web3Auth?.logout();
setWalletAddress(null);
}
} catch (error: any) {
if (error?.message?.toLowerCase().includes(âuser closedâ)) {
toaster(âWallet connection cancelled by userâ, âerrorâ);
} else {
console.error(âWallet Connection error:â, error);
}
}
};
return (
{showMobileHeader && (
)}
<Image alt=âlogoâ src={â/assets/images/simple-layout-logo.svgâ} height={350} width={350} />
{content?.mainHeaderLinks.map((item, index) => (
<Typography
size=âmdâ
as=âpâ
className={font-semibold hover:text-light-blue ${pathname === item.href ? 'text-light-blue !font-bold' : 'text-dark-blue'}
}
>
{item.href === â/my-otrâ ? (
<>
my OTR
</>
) : item.href === â/earn-from-my-otrâ ? (
<>
Earn From my OTR
</>
) : (
item.label
)}
))}
{status !== âreadyâ && walletBalance === null ? (
) : status === âconnectedâ && walletBalance !== null ? (
POL: {walletBalance}
) : (
)}
<Button
onClick={handleConnect}
type=âbuttonâ
variant=âsolidâ
className={max-w-[130px] !rounded-lg text-nowrap !font-normal !text-md !w-fit !px-6 !py-3 ${!isConnected ? 'xs:!px-6 xl:!px-6' : 'lg:!px-3 xl:!px-6'}
}
>
{isConnected ? truncateAddress(walletAddress) : âConnect Walletâ}
{false && (
<button
className=âcursor-pointer px-2 py-1 rounded-md border border-gray-300 hover:bg-gray-100 transitionâ
onClick={() => handleGetPrivateKey(provider)}
>
Private Key
<button
className=âcursor-pointer px-2 py-1 rounded-md border border-gray-300 hover:bg-gray-100 transitionâ
onClick={() => handleGetPublicKey(provider)}
>
Public Key
)}
);
};
export default MainHeader;