import React, { useEffect } from "react"; import { NavLink } from "react-router"; import AppBar from '@mui/material/AppBar'; import Box from '@mui/material/Box'; import Toolbar from '@mui/material/Toolbar'; import Button from "@mui/material/Button"; import LoginIcon from "@mui/icons-material/Login"; import { Typography, Menu, MenuItem, IconButton, Avatar, Tooltip } from "@mui/material"; import useAuth from "~/hooks/useAuth"; import type { PaymentChannelInfo } from './Util'; import { approveCLRTokenSpending, openPaymentChannel, closePaymentChannel, getPaymentChannelInfo } from './Contracts/Connections'; export default function ButtonAppBar() { const { auth, setAuth } = useAuth(); const account = auth?.accounts?.[0]; const isAuthed = Boolean(account); const [paymentChannelInfo, setPaymentChannelInfo] = React.useState(null); const [registered, setRegistered] = React.useState(false); const [anchorEl, setAnchorEl] = React.useState(null); const open = Boolean(anchorEl); const handleOpen = (e: React.MouseEvent) => setAnchorEl(e.currentTarget); const handleClose = () => setAnchorEl(null); const handleLogout = () => { if (setAuth) { setAuth({ providerWithInfo: undefined as any, accounts: [] } as any); } handleClose(); }; const register = async () => { const clr_deposit_amount = BigInt(100) * BigInt(1e18); // 100 CLR await approveCLRTokenSpending(auth.providerWithInfo.provider, account!); await openPaymentChannel(auth.providerWithInfo.provider, account!, clr_deposit_amount); setRegistered(true); } const deRegister = async () => { await closePaymentChannel(auth.providerWithInfo.provider, account!); setRegistered(false); } useEffect(() => { getPaymentChannelInfo(auth.providerWithInfo ? auth.providerWithInfo.provider : undefined, account!).then((info) => { setPaymentChannelInfo(info); if(registered !== info.isActive) { setRegistered(info.isActive); } console.log("Payment Channel Info:", info); }); }, [account, auth.providerWithInfo?.provider,registered]); return ( {} Home Faucet About {} {} {} {!isAuthed && ( )} {isAuthed && ( <> {(paymentChannelInfo && paymentChannelInfo.isActive) ? (DeRegister) : (Register)} Logout )} ); }