import { Event } from 'nostr-tools' import { Highlighter } from 'lucide-react' import { nip19 } from 'nostr-tools' import logger from '@/lib/logger' import HighlightSourcePreview from '@/components/UniversalContent/HighlightSourcePreview' export default function Highlight({ event, className }: { event: Event className?: string }) { try { // Extract the source (e-tag, a-tag, or r-tag) with improved priority handling let source = null let sourceTag: string[] | undefined // Check for 'source' marker first (highest priority) for (const tag of event.tags) { if (tag[2] === 'source' || tag[3] === 'source') { sourceTag = tag break } } // If no 'source' marker found, process tags in priority order: e > a > r if (!sourceTag) { for (const tag of event.tags) { // Give 'e' tags highest priority if (tag[0] === 'e') { sourceTag = tag continue } // Give 'a' tags second priority (but don't override 'e' tags) if (tag[0] === 'a' && (!sourceTag || sourceTag[0] !== 'e')) { sourceTag = tag continue } // Give 'r' tags lowest priority if (tag[0] === 'r' && (!sourceTag || sourceTag[0] === 'r')) { sourceTag = tag continue } } } // Process the selected source tag if (sourceTag) { if (sourceTag[0] === 'e') { source = { type: 'event' as const, value: sourceTag[1], bech32: nip19.noteEncode(sourceTag[1]) } } else if (sourceTag[0] === 'a') { const [kind, pubkey, identifier] = sourceTag[1].split(':') const relay = sourceTag[2] source = { type: 'addressable' as const, value: sourceTag[1], bech32: nip19.naddrEncode({ kind: parseInt(kind), pubkey, identifier: identifier || '', relays: relay ? [relay] : [] }) } } else if (sourceTag[0] === 'r') { source = { type: 'url' as const, value: sourceTag[1], bech32: sourceTag[1] } } } // Extract the context (the main quote/full text being highlighted from) const contextTag = event.tags.find(tag => tag[0] === 'context') const context = contextTag?.[1] || event.content // Default to content if no context // The event.content is the highlighted portion const highlightedText = event.content return (
{/* Full quoted text with highlighted portion */} {context && (
{contextTag && highlightedText ? ( // If we have both context and highlighted text, show the highlight within the context
{context.split(highlightedText).map((part, index) => ( {part} {index < context.split(highlightedText).length - 1 && ( {highlightedText} )} ))}
) : ( // If no context tag, just show the content as a regular quote
"{context}"
)}
)} {/* Source preview card */} {source && (
)}
) } catch (error) { logger.error('Highlight component error', { error, eventId: event.id }) return (
Highlight Error:
{String(error)}
Content: {event.content}
Context: {event.tags.find(tag => tag[0] === 'context')?.[1] || 'No context found'}
) } }