import React, { useMemo, useRef } from 'react' import ReactMarkdown from 'react-markdown' import remarkGfm from 'remark-gfm' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faSpinner } from '@fortawesome/free-solid-svg-icons' import { Highlight } from '../types/highlights' import { readingTime } from 'reading-time-estimator' import { hexToRgb } from '../utils/colorHelpers' import ReaderHeader from './ReaderHeader' import { HighlightVisibility } from './HighlightsPanel' import { useMarkdownToHTML } from '../hooks/useMarkdownToHTML' import { useHighlightedContent } from '../hooks/useHighlightedContent' import { useHighlightInteractions } from '../hooks/useHighlightInteractions' interface ContentPanelProps { loading: boolean title?: string html?: string markdown?: string selectedUrl?: string image?: string highlights?: Highlight[] showHighlights?: boolean highlightStyle?: 'marker' | 'underline' highlightColor?: string onHighlightClick?: (highlightId: string) => void selectedHighlightId?: string highlightVisibility?: HighlightVisibility currentUserPubkey?: string followedPubkeys?: Set // For highlight creation onTextSelection?: (text: string) => void onClearSelection?: () => void } const ContentPanel: React.FC = ({ loading, title, html, markdown, selectedUrl, image, highlights = [], showHighlights = true, highlightStyle = 'marker', highlightColor = '#ffff00', onHighlightClick, selectedHighlightId, highlightVisibility = { nostrverse: true, friends: true, mine: true }, currentUserPubkey, followedPubkeys = new Set(), onTextSelection, onClearSelection }) => { const markdownPreviewRef = useRef(null) const renderedMarkdownHtml = useMarkdownToHTML(markdown) const { finalHtml, relevantHighlights } = useHighlightedContent({ html, markdown, renderedMarkdownHtml, highlights, showHighlights, highlightStyle, selectedUrl, highlightVisibility, currentUserPubkey, followedPubkeys }) const { contentRef, handleMouseUp } = useHighlightInteractions({ onHighlightClick, selectedHighlightId, onTextSelection, onClearSelection }) const readingStats = useMemo(() => { const content = markdown || html || '' if (!content) return null const textContent = content.replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ') return readingTime(textContent) }, [html, markdown]) const hasHighlights = relevantHighlights.length > 0 if (!selectedUrl) { return (

Select a bookmark to read its content.

) } if (loading) { return (
) } const highlightRgb = hexToRgb(highlightColor) return (
{/* Hidden markdown preview to convert markdown to HTML */} {markdown && (
{markdown}
)} {markdown || html ? ( markdown ? ( renderedMarkdownHtml && finalHtml ? (
) : (
) ) : (
) ) : (

No readable content found for this URL.

)}
) } export default ContentPanel