'use client' import { useEffect, useState } from 'react' import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog' import { ExternalLink, Link2, Columns2, GitMerge, Loader2, X } from 'lucide-react' import { useLanguage } from '@/lib/i18n/LanguageProvider' import { cn } from '@/lib/utils' import { sanitizeRichHtml } from '@/lib/sanitize-content' interface LinkedNotePreviewDialogProps { isOpen: boolean onClose: () => void noteId: string initialTitle?: string | null initialExcerpt?: string onInsertCitation?: () => void onCompare?: () => void onMerge?: () => void citationLoading?: boolean } function stripHtml(html: string): string { return html.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim() } function openNoteInNewTab(noteId: string) { window.open(`/home?openNote=${encodeURIComponent(noteId)}`, '_blank', 'noopener,noreferrer') } const linkActionClass = 'inline-flex items-center gap-1 text-[11px] font-semibold text-muted-foreground hover:text-foreground transition-colors hover:underline' export function LinkedNotePreviewDialog({ isOpen, onClose, noteId, initialTitle, initialExcerpt, onInsertCitation, onCompare, onMerge, citationLoading = false, }: LinkedNotePreviewDialogProps) { const { t } = useLanguage() const [title, setTitle] = useState(initialTitle ?? '') const [content, setContent] = useState('') const [isLoading, setIsLoading] = useState(false) const [loadError, setLoadError] = useState(false) useEffect(() => { if (!isOpen || !noteId) return let cancelled = false setIsLoading(true) setLoadError(false) setTitle(initialTitle ?? '') setContent('') void (async () => { try { const res = await fetch(`/api/notes/${noteId}`) if (!res.ok) throw new Error('fetch failed') const data = await res.json() if (cancelled) return if (data.success && data.data) { setTitle(data.data.title || t('memoryEcho.comparison.untitled')) setContent(data.data.content || '') } else { setLoadError(true) } } catch { if (!cancelled) setLoadError(true) } finally { if (!cancelled) setIsLoading(false) } })() return () => { cancelled = true } }, [isOpen, noteId, initialTitle, t]) const displayTitle = title || initialTitle || t('memoryEcho.comparison.untitled') const plainBody = content ? stripHtml(content) : (initialExcerpt ?? '') const isHtml = content.includes('<') return ( { if (!open) onClose() }}>
{displayTitle}
{isLoading && !plainBody && (
{t('memoryEcho.editorSection.loading')}
)} {loadError && !plainBody && (

{t('memoryEcho.preview.loadError')}

)} {content && isHtml && (
)} {!isHtml && plainBody && (
{plainBody}
)}
{onCompare && ( )} {onInsertCitation && ( )} {onMerge && ( )}
) }