'use client' import { memo, useState, useEffect, useRef, useCallback } from 'react' import { Sparkles } from 'lucide-react' import { cn } from '@/lib/utils' import { useLanguage } from '@/lib/i18n/LanguageProvider' import { getConnectionsCount } from '@/lib/connections-cache' interface ConnectionsBadgeProps { noteId: string onClick?: () => void className?: string } export const ConnectionsBadge = memo(function ConnectionsBadge({ noteId, onClick, className }: ConnectionsBadgeProps) { const { t } = useLanguage() const [connectionCount, setConnectionCount] = useState(0) const [isHovered, setIsHovered] = useState(false) const containerRef = useRef(null) const fetchedRef = useRef(false) const fetchConnections = useCallback(async () => { if (fetchedRef.current) return fetchedRef.current = true try { const count = await getConnectionsCount(noteId) setConnectionCount(count) } catch { setConnectionCount(0) } }, [noteId]) useEffect(() => { const el = containerRef.current if (!el) return const observer = new IntersectionObserver( ([entry]) => { if (entry.isIntersecting) { fetchConnections() observer.disconnect() } }, { rootMargin: '200px' } ) observer.observe(el) return () => observer.disconnect() }, [fetchConnections]) if (connectionCount === 0) { return } const plural = connectionCount > 1 ? 's' : '' const badgeText = t('memoryEcho.connectionsBadge', { count: connectionCount, plural }) return (
setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} title={badgeText} > {badgeText}
) })