'use client'; import { useAttestations } from '@/identity/hooks/useAttestations'; import { COINBASE_VERIFIED_ACCOUNT_SCHEMA_ID } from '@/identity/constants'; import type { BadgeProps } from '@/identity/types'; import { badgeSvg } from '@/internal/svg/badgeSvg'; import { zIndex } from '@/styles/constants'; import { cn, pressable, text } from '@/styles/theme'; import { useMemo, useState } from 'react'; import { useOnchainKit } from '../../useOnchainKit'; import { useIdentityContext } from './IdentityProvider'; type ExtractAttestationNameParams = { decodedDataJson?: string; id?: string; attester?: string; expirationTime?: number; recipient?: string; revocationTime?: number; revoked?: boolean; schemaId?: string; time?: number; }; /** * Badge component. */ export function Badge({ className, tooltip = false }: BadgeProps) { const [showTooltip, setShowTooltip] = useState(false); const { address, schemaId: contextSchemaId } = useIdentityContext(); const { chain } = useOnchainKit(); const attestations = useAttestations({ address, chain, schemaId: tooltip ? (contextSchemaId ?? COINBASE_VERIFIED_ACCOUNT_SCHEMA_ID) : null, }); // Get tooltip text from tooltip prop or attestation const displayText = useMemo(() => { if (!tooltip) { return null; } return typeof tooltip === 'string' ? tooltip : extractAttestationName(attestations[0]); }, [tooltip, attestations]); const ariaLabel = useMemo(() => { if (displayText) { return `Verification badge: ${displayText}`; } return 'Verification badge'; }, [displayText]); const badgeSize = '12px'; return (
setShowTooltip(true), onMouseLeave: () => setShowTooltip(false), })} > {badgeSvg} {showTooltip && tooltip && (
{displayText}
)}
); } /** * Extracts the attestation name from an attestation object */ function extractAttestationName( attestation?: ExtractAttestationNameParams, ): string { if (!attestation?.decodedDataJson) { return 'Verified'; } try { const decodedData = JSON.parse(attestation.decodedDataJson); if (Array.isArray(decodedData) && decodedData[0]?.name) { return decodedData[0].name; } const value = Object.values(decodedData)[0]; if (typeof value === 'string') { return value; } if ( value && typeof value === 'object' && 'value' in value && typeof value.value === 'string' ) { return value.value; } } catch { // If parsing fails, return default } return 'Verified'; }