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;