104 lines
3.6 KiB
TypeScript
104 lines
3.6 KiB
TypeScript
'use client'
|
|
|
|
import { useAccount, useReadContract, useConnect } from 'wagmi'
|
|
import { CUSD_ADDRESS, CUSD_ABI, weiToCUSD } from '@/lib/celo'
|
|
import { formatCUSD, shortenAddress } from '@/lib/format'
|
|
import { useEffect, useState } from 'react'
|
|
import sdk from '@farcaster/miniapp-sdk'
|
|
import NotificationBell from '@/components/notifications/NotificationBell'
|
|
import Link from 'next/link'
|
|
import { Search, Trophy } from 'lucide-react'
|
|
|
|
export default function Header() {
|
|
const { address, isConnected } = useAccount()
|
|
const { connect, connectors, isPending } = useConnect()
|
|
const [userProfile, setUserProfile] = useState<{ displayName?: string; username?: string } | null>(null)
|
|
|
|
// Get cUSD balance
|
|
const { data: balance } = useReadContract({
|
|
address: CUSD_ADDRESS,
|
|
abi: CUSD_ABI,
|
|
functionName: 'balanceOf',
|
|
args: address ? [address] : undefined,
|
|
})
|
|
|
|
// Get Farcaster user context
|
|
useEffect(() => {
|
|
const getContext = async () => {
|
|
try {
|
|
const context = await sdk.context
|
|
if (context?.user) {
|
|
setUserProfile({
|
|
displayName: context.user.displayName,
|
|
username: context.user.username,
|
|
})
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to get context:', error)
|
|
}
|
|
}
|
|
getContext()
|
|
}, [])
|
|
|
|
const displayBalance = balance ? formatCUSD(weiToCUSD(balance as bigint)) : '0.00 cUSD'
|
|
|
|
// Get the Farcaster MiniApp connector (should be the first/only connector)
|
|
const farcasterConnector = connectors[0]
|
|
|
|
const handleConnect = () => {
|
|
if (farcasterConnector) {
|
|
connect({ connector: farcasterConnector })
|
|
}
|
|
}
|
|
|
|
return (
|
|
<header className="bg-gradient-to-r from-celo-green to-celo-yellow p-4 shadow-lg">
|
|
<div className="max-w-4xl mx-auto flex justify-between items-center">
|
|
<div>
|
|
<h1 className="text-2xl font-bold text-white flex items-center gap-2">
|
|
<span className="text-3xl">📊</span>
|
|
<span>PollCelo</span>
|
|
</h1>
|
|
<p className="text-white/80 text-sm">Daily Poll, Daily Celo</p>
|
|
</div>
|
|
|
|
{isConnected ? (
|
|
<div className="flex items-center gap-3">
|
|
<Link href="/discover" className="text-white/80 hover:text-white transition">
|
|
<Search size={20} />
|
|
</Link>
|
|
<Link href="/leaderboard" className="text-white/80 hover:text-white transition">
|
|
<Trophy size={20} />
|
|
</Link>
|
|
<NotificationBell />
|
|
<div className="bg-white/20 backdrop-blur-sm rounded-xl p-3 text-white">
|
|
{userProfile && (
|
|
<p className="font-semibold text-sm">
|
|
{userProfile.displayName || `@${userProfile.username}`}
|
|
</p>
|
|
)}
|
|
<p className="text-xs opacity-90">{shortenAddress(address!)}</p>
|
|
<p className="font-bold text-sm mt-1">{displayBalance}</p>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<button
|
|
onClick={handleConnect}
|
|
disabled={isPending || !farcasterConnector}
|
|
className="bg-white/20 backdrop-blur-sm rounded-xl px-4 py-2 text-white hover:bg-white/30 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
|
|
>
|
|
{isPending ? (
|
|
<span className="flex items-center gap-2">
|
|
<span className="animate-spin">⏳</span>
|
|
Connecting...
|
|
</span>
|
|
) : (
|
|
<span className="text-sm font-semibold">Connect Wallet</span>
|
|
)}
|
|
</button>
|
|
)}
|
|
</div>
|
|
</header>
|
|
)
|
|
}
|