'use client' import { useState, useEffect } from 'react' import { Sparkles } from 'lucide-react' import { cn } from '@/lib/utils' import { useLanguage } from '@/lib/i18n/LanguageProvider' interface ConnectionsBadgeProps { noteId: string onClick?: () => void className?: string } interface ConnectionData { noteId: string title: string | null content: string createdAt: Date similarity: number daysApart: number } interface ConnectionsResponse { connections: ConnectionData[] pagination: { total: number page: number limit: number totalPages: number hasNext: boolean hasPrev: boolean } } export function ConnectionsBadge({ noteId, onClick, className }: ConnectionsBadgeProps) { const { t } = useLanguage() const [connectionCount, setConnectionCount] = useState(0) const [isLoading, setIsLoading] = useState(false) const [isHovered, setIsHovered] = useState(false) useEffect(() => { const fetchConnections = async () => { setIsLoading(true) try { const res = await fetch(`/api/ai/echo/connections?noteId=${noteId}&limit=1`) if (!res.ok) { throw new Error('Failed to fetch connections') } const data: ConnectionsResponse = await res.json() setConnectionCount(data.pagination.total || 0) } catch (error) { console.error('[ConnectionsBadge] Failed to fetch connections:', error) setConnectionCount(0) } finally { setIsLoading(false) } } fetchConnections() }, [noteId]) // Don't render if no connections or still loading if (connectionCount === 0 || isLoading) { return null } const plural = connectionCount > 1 ? 's' : '' const badgeText = t('memoryEcho.connectionsBadge', { count: connectionCount, plural }) return (
setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} title={badgeText} > {badgeText}
) }