// "use client"; import type { Agent } from "@/types/agent"; import VRMDemo from "./vrm-demo"; import { PriceChart } from "./price-chart"; import { TokenData } from "./token-data"; import { AgentDescription } from "./agent-description"; import { SocialMediaButtons } from "./social-media-buttons"; import { AgentTags } from "./agent-tags"; import { AgentTiers } from "./agent-tiers"; import { Button } from "./ui/button"; import { MessageSquare, ArrowRightLeft } from "lucide-react"; import { Integrations } from "./integrations"; import { useEffect, useState } from "react"; import { useTokens } from "@/hooks/use-token"; import { AlertTriangle } from "lucide-react"; import { ConnectButton } from "@rainbow-me/rainbowkit"; import { useReserveToken } from "@/hooks/use-reserve-token"; import { useAccount, useReadContract } from "wagmi"; import { ERC721_ABI } from "@/utils/abi/erc721"; import { formatUnits } from "ethers"; import { ERC20_ABI } from "@/utils/abi/erc20"; import { AgentVrmDiagnosis } from "./agent-diagnosis"; import { AgentDemo } from "./agent-demo"; interface AgentDetailsProps { agent: Agent; } const AMICA_URL = process.env.NEXT_PUBLIC_AMICA_URL as string; const delay = (ms: number) => new Promise(res => setTimeout(res, ms)); export function AgentDetails({ agent }: AgentDetailsProps) { const [diagnosisPassed, setDiagnosisPassed] = useState(false); const [reserveAmount, setReserveAmount] = useState(""); const [talentShow, setTalentShow] = useState(false); const [talentRunning, setTalentRunning] = useState(false); const { isConnected, address } = useAccount(); const { stats, priceHistory, tokenAddress, loading, error } = useTokens(Number(agent.id)); const { write: reserveTokens, isLoading: reserving, isSuccess: reserveSuccess, error: reserveError } = useReserveToken(); const { data, isLoading: loadingAius, refetch: refetchAiusData } = useReadContract({ address: process.env.NEXT_PUBLIC_CONTRACT_ADDRESS! as `0x${string}`, abi: ERC721_ABI, functionName: "getAiusAndOwed", args: [BigInt(agent.id), address], query: { enabled: isConnected && !!address }, }); const [aius, owed] = (data as [bigint, bigint]) || [BigInt(0), BigInt(0)]; const { data: aiusAmount, refetch: refetchAiusAmount } = useReadContract({ address: process.env.NEXT_PUBLIC_AIUS_CONTRACT_ADDRESS! as `0x${string}`, abi: ERC20_ABI, functionName: "balanceOf", args: [address], query: { enabled: isConnected && !!address }, }); const formattedAius = aius ? formatUnits(aius, 18) : "0.0"; const formattedOwed = owed ? formatUnits(owed, 18) : "0.0"; const isPairNotCreated = error == "Pair not created"; // refetch aius and reserve amount after reserve, reload page if pair created useEffect(() => { if (reserveSuccess) { const refetchAndCheck = async () => { const { data: aiusAndOwedData } = await refetchAiusData(); await refetchAiusAmount(); setReserveAmount(""); const [newAius, newOwed] = (aiusAndOwedData as [bigint, bigint]) || [BigInt(0), BigInt(0)]; const formattedAius = parseFloat(formatUnits(newAius, 18)); if (formattedAius >= 100) { await delay(5000); window.location.reload(); // Refresh the page } }; refetchAndCheck(); } }, [refetchAiusData, reserveSuccess, refetchAiusAmount]); if (loading) { return (
{agent.token} | {agent.tier?.name} (Level {agent.tier?.level})
{isPairNotCreated && (