'use client' import { useState, useEffect } from 'react' import { ChevronDown, ChevronUp, Sparkles, Eye, ArrowRight, Link2, X } from 'lucide-react' import { Button } from '@/components/ui/button' import { cn } from '@/lib/utils' import { useLanguage } from '@/lib/i18n/LanguageProvider' 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 } } interface EditorConnectionsSectionProps { noteId: string onOpenNote?: (noteId: string) => void onCompareNotes?: (noteIds: string[]) => void onMergeNotes?: (noteIds: string[]) => void } export function EditorConnectionsSection({ noteId, onOpenNote, onCompareNotes, onMergeNotes }: EditorConnectionsSectionProps) { const { t } = useLanguage() const [connections, setConnections] = useState([]) const [isLoading, setIsLoading] = useState(false) const [isExpanded, setIsExpanded] = useState(true) const [isVisible, setIsVisible] = useState(true) useEffect(() => { const fetchConnections = async () => { setIsLoading(true) try { const res = await fetch(`/api/ai/echo/connections?noteId=${noteId}&limit=10`) if (!res.ok) { throw new Error('Failed to fetch connections') } const data: ConnectionsResponse = await res.json() setConnections(data.connections) // Show section if there are connections if (data.connections.length > 0) { setIsVisible(true) } else { setIsVisible(false) } } catch (error) { console.error('[EditorConnectionsSection] Failed to fetch:', error) } finally { setIsLoading(false) } } fetchConnections() }, [noteId]) // Don't render if no connections or if dismissed if (!isVisible || (connections.length === 0 && !isLoading)) { return null } return (
{/* Header with toggle */}
setIsExpanded(!isExpanded)} >
{t('memoryEcho.editorSection.title', { count: connections.length })}
{/* Connections list */} {isExpanded && (
{isLoading ? (
{t('memoryEcho.editorSection.loading')}
) : ( connections.map((conn) => { const similarityPercentage = Math.round(conn.similarity * 100) const title = conn.title || t('memoryEcho.comparison.untitled') return (

{title}

{similarityPercentage}%

{conn.content}

{onCompareNotes && ( )} {onMergeNotes && ( )}
) }) )}
)} {/* Footer actions */} {isExpanded && connections.length > 1 && (
{onMergeNotes && ( )}
)}
) }