"use client"; import { useState, useEffect } from "react"; import { toast } from "sonner"; import { useAccount } from "wagmi"; import EarnOpportunityCard from "./EarnOpportunityCard"; import ActivityHistory from "./ActivityHistory"; import TopPerformers from "./TopPerformers"; import ReferralCodeCard from "./ReferralCodeCard"; import BindInviteCard from "./BindInviteCard"; import TeamTVLCard from "./TeamTVLCard"; import DepositCard from "./DepositCard"; import { fetchTeamTVL, bindInviteCode, registerWallet, type TeamTVLData } from "@/lib/api/points"; import { useApp } from "@/contexts/AppContext"; export default function PointsCards() { const { t } = useApp(); const { address } = useAccount(); const [mounted, setMounted] = useState(false); const [teamTVL, setTeamTVL] = useState(null); const [loading, setLoading] = useState(true); const [inviteCode, setInviteCode] = useState(''); const [inviteUsedCount, setInviteUsedCount] = useState(0); const [inviteLoading, setInviteLoading] = useState(false); useEffect(() => { setMounted(true); }, []); // Register wallet and load user data when address connects useEffect(() => { if (address) { registerAndLoad(address); } }, [address]); async function registerAndLoad(walletAddress: string) { setInviteLoading(true); // Register wallet (creates user if not exists), get invite code const userData = await registerWallet(walletAddress); if (userData) { setInviteCode(userData.inviteCode); setInviteUsedCount(userData.usedCount); } setInviteLoading(false); // Load team TVL for this wallet setLoading(true); const teamData = await fetchTeamTVL(walletAddress); setTeamTVL(teamData); setLoading(false); } const handleCopy = () => { if (inviteCode) { navigator.clipboard.writeText(inviteCode); toast.success(t("points.copiedToClipboard")); } }; const handleShare = async () => { if (!inviteCode) return; const shareUrl = `${window.location.origin}?ref=${inviteCode}`; if (navigator.share) { try { await navigator.share({ url: shareUrl }); } catch { // user cancelled, do nothing } } else { navigator.clipboard.writeText(shareUrl); toast.success(t("points.shareLinkCopied")); } }; const handleApply = async (code: string) => { if (!code) { toast.error(t("points.pleaseEnterInviteCode")); return; } // TODO: Get signature from wallet const signature = "0x..."; // Placeholder const result = await bindInviteCode(code, signature, address); if (result.success) { toast.success(result.message || t("points.inviteCodeBoundSuccess")); if (address) registerAndLoad(address); // Reload data } else { toast.error(result.error || t("points.failedToBindInviteCode")); } }; return (
{/* First Row - Cards 1, 2, 3 */}
{/* Second Row - Card 4 */}
{}} />
{/* Third Row - Earn Opportunities */}
{}} /> {}} /> {}} />
{/* Fourth Row - Activity History and Top Performers */}
); }