|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { Address, zeroAddress } from "viem"; |
| 5 | +import { useAccount, useReadContract } from "wagmi"; |
| 6 | +import { UserGroupIcon, CheckCircleIcon, XCircleIcon } from "@heroicons/react/24/solid"; |
| 7 | +import externalContracts from "~~/contracts/externalContracts"; |
| 8 | + |
| 9 | +interface BatchStatusIndicatorsProps { |
| 10 | + address?: Address; |
| 11 | +} |
| 12 | + |
| 13 | +export const BatchStatusIndicators = ({ address }: BatchStatusIndicatorsProps) => { |
| 14 | + const { address: connectedAddress } = useAccount(); |
| 15 | + const userAddress = address || connectedAddress; |
| 16 | + |
| 17 | + const [isInBatch, setIsInBatch] = useState<boolean | null>(null); |
| 18 | + const [isCheckedIn, setIsCheckedIn] = useState<boolean | null>(null); |
| 19 | + |
| 20 | + // Get contract details from externalContracts |
| 21 | + const batchRegistryContract = externalContracts[42161]?.BatchRegistry; |
| 22 | + |
| 23 | + // Check if address is in allowList (batch member) |
| 24 | + const { data: allowListStatus, isLoading: isLoadingAllowList } = useReadContract( |
| 25 | + userAddress && batchRegistryContract ? { |
| 26 | + address: batchRegistryContract.address, |
| 27 | + abi: batchRegistryContract.abi, |
| 28 | + functionName: "allowList", |
| 29 | + args: [userAddress], |
| 30 | + } : undefined |
| 31 | + ); |
| 32 | + |
| 33 | + // Check if address has checked in |
| 34 | + const { data: contractAddress, isLoading: isLoadingContractAddress } = useReadContract( |
| 35 | + userAddress && batchRegistryContract ? { |
| 36 | + address: batchRegistryContract.address, |
| 37 | + abi: batchRegistryContract.abi, |
| 38 | + functionName: "yourContractAddress", |
| 39 | + args: [userAddress], |
| 40 | + } : undefined |
| 41 | + ); |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + if (allowListStatus !== undefined) { |
| 47 | + setIsInBatch(!!allowListStatus); |
| 48 | + } |
| 49 | + |
| 50 | + if (contractAddress !== undefined) { |
| 51 | + setIsCheckedIn(contractAddress !== zeroAddress); |
| 52 | + } |
| 53 | + }, [allowListStatus, contractAddress]); |
| 54 | + |
| 55 | + if (!userAddress) return null; |
| 56 | + |
| 57 | + const isLoading = isLoadingAllowList || isLoadingContractAddress; |
| 58 | + |
| 59 | + return ( |
| 60 | + <div className="flex items-center space-x-2 mx-1"> |
| 61 | + {isLoading ? ( |
| 62 | + <div className="loading loading-spinner loading-xs"></div> |
| 63 | + ) : ( |
| 64 | + <> |
| 65 | + {/* Batch Membership Indicator */} |
| 66 | + <div className={`tooltip tooltip-bottom ${!isInBatch ? "tooltip-error" : ""}`} data-tip={isInBatch ? "Batch Member" : "Not a Batch Member"}> |
| 67 | + <div className={`flex items-center justify-center w-6 h-6 rounded-full ${isInBatch ? "bg-primary/20" : "bg-error/20"}`}> |
| 68 | + <UserGroupIcon |
| 69 | + className={`h-5 w-5 ${isInBatch ? "text-primary font-bold" : "text-error"}`} |
| 70 | + /> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + |
| 74 | + {/* Check-in Status Indicator */} |
| 75 | + <div className={`tooltip tooltip-bottom ${!isCheckedIn ? "tooltip-error" : ""}`} data-tip={isCheckedIn ? "Checked In" : "Not Checked In"}> |
| 76 | + <div className={`flex items-center justify-center w-6 h-6 rounded-full ${isCheckedIn ? "bg-success/20" : "bg-error/20"}`}> |
| 77 | + {isCheckedIn ? ( |
| 78 | + <CheckCircleIcon className="h-4 w-4 text-success" /> |
| 79 | + ) : ( |
| 80 | + <XCircleIcon className="h-4 w-4 text-error" /> |
| 81 | + )} |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + </> |
| 85 | + )} |
| 86 | + </div> |
| 87 | + ); |
| 88 | +}; |
0 commit comments