import React, { useMemo, useState, useEffect, useRef } from 'react' import ReactMarkdown from 'react-markdown' import remarkGfm from 'remark-gfm' import rehypeRaw from 'rehype-raw' import rehypePrism from 'rehype-prism-plus' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import 'prismjs/themes/prism-tomorrow.css' import { faSpinner, faCheck, faEllipsisH, faExternalLinkAlt, faMobileAlt } from '@fortawesome/free-solid-svg-icons' import { nip19 } from 'nostr-tools' import { getNostrUrl } from '../config/nostrGateways' import { RELAYS } from '../config/relays' import { RelayPool } from 'applesauce-relay' import { IAccount } from 'applesauce-accounts' import { NostrEvent } from 'nostr-tools' 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' import { UserSettings } from '../services/settingsService' import { createEventReaction, createWebsiteReaction, hasMarkedEventAsRead, hasMarkedWebsiteAsRead } from '../services/reactionService' import AuthorCard from './AuthorCard' import { faBooks } from '../icons/customIcons' interface ContentPanelProps { loading: boolean title?: string html?: string markdown?: string selectedUrl?: string image?: string summary?: string published?: number highlights?: Highlight[] showHighlights?: boolean highlightStyle?: 'marker' | 'underline' highlightColor?: string onHighlightClick?: (highlightId: string) => void selectedHighlightId?: string highlightVisibility?: HighlightVisibility currentUserPubkey?: string followedPubkeys?: Set settings?: UserSettings relayPool?: RelayPool | null activeAccount?: IAccount | null currentArticle?: NostrEvent | null // For highlight creation onTextSelection?: (text: string) => void onClearSelection?: () => void } const ContentPanel: React.FC = ({ loading, title, html, markdown, selectedUrl, image, summary, published, highlights = [], showHighlights = true, highlightStyle = 'marker', highlightColor = '#ffff00', settings, relayPool, activeAccount, currentArticle, onHighlightClick, selectedHighlightId, highlightVisibility = { nostrverse: true, friends: true, mine: true }, currentUserPubkey, followedPubkeys = new Set(), onTextSelection, onClearSelection }) => { const [isMarkedAsRead, setIsMarkedAsRead] = useState(false) const [isCheckingReadStatus, setIsCheckingReadStatus] = useState(false) const [showCheckAnimation, setShowCheckAnimation] = useState(false) const [showArticleMenu, setShowArticleMenu] = useState(false) const articleMenuRef = useRef(null) const { renderedHtml: renderedMarkdownHtml, previewRef: markdownPreviewRef, processedMarkdown } = useMarkdownToHTML(markdown, relayPool) const { finalHtml, relevantHighlights } = useHighlightedContent({ html, markdown, renderedMarkdownHtml, highlights, showHighlights, highlightStyle, selectedUrl, highlightVisibility, currentUserPubkey, followedPubkeys }) const { contentRef, handleSelectionEnd } = useHighlightInteractions({ onHighlightClick, selectedHighlightId, onTextSelection, onClearSelection }) // Close menu when clicking outside useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (articleMenuRef.current && !articleMenuRef.current.contains(event.target as Node)) { setShowArticleMenu(false) } } if (showArticleMenu) { document.addEventListener('mousedown', handleClickOutside) return () => { document.removeEventListener('mousedown', handleClickOutside) } } }, [showArticleMenu]) 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 // Determine if we're on a nostr-native article (/a/) or external URL (/r/) const isNostrArticle = selectedUrl && selectedUrl.startsWith('nostr:') // Get article links for menu const getArticleLinks = () => { if (!currentArticle) return null const dTag = currentArticle.tags.find(t => t[0] === 'd')?.[1] || '' const relayHints = RELAYS.filter(r => !r.includes('localhost') && !r.includes('127.0.0.1') ).slice(0, 3) const naddr = nip19.naddrEncode({ kind: 30023, pubkey: currentArticle.pubkey, identifier: dTag, relays: relayHints }) return { portal: getNostrUrl(naddr), native: `nostr:${naddr}` } } const articleLinks = getArticleLinks() const handleMenuToggle = () => { setShowArticleMenu(!showArticleMenu) } const handleOpenPortal = () => { if (articleLinks) { window.open(articleLinks.portal, '_blank', 'noopener,noreferrer') } setShowArticleMenu(false) } const handleOpenNative = () => { if (articleLinks) { window.location.href = articleLinks.native } setShowArticleMenu(false) } // Check if article is already marked as read when URL/article changes useEffect(() => { const checkReadStatus = async () => { if (!activeAccount || !relayPool || !selectedUrl) { setIsMarkedAsRead(false) return } setIsCheckingReadStatus(true) try { let hasRead = false if (isNostrArticle && currentArticle) { hasRead = await hasMarkedEventAsRead( currentArticle.id, activeAccount.pubkey, relayPool ) } else { hasRead = await hasMarkedWebsiteAsRead( selectedUrl, activeAccount.pubkey, relayPool ) } setIsMarkedAsRead(hasRead) } catch (error) { console.error('Failed to check read status:', error) } finally { setIsCheckingReadStatus(false) } } checkReadStatus() }, [selectedUrl, currentArticle, activeAccount, relayPool, isNostrArticle]) const handleMarkAsRead = () => { if (!activeAccount || !relayPool || isMarkedAsRead) { return } // Instantly update UI with checkmark animation setIsMarkedAsRead(true) setShowCheckAnimation(true) // Reset animation after it completes setTimeout(() => { setShowCheckAnimation(false) }, 600) // Fire-and-forget: publish in background without blocking UI ;(async () => { try { if (isNostrArticle && currentArticle) { await createEventReaction( currentArticle.id, currentArticle.pubkey, currentArticle.kind, activeAccount, relayPool ) console.log('✅ Marked nostr article as read') } else if (selectedUrl) { await createWebsiteReaction( selectedUrl, activeAccount, relayPool ) console.log('✅ Marked website as read') } } catch (error) { console.error('Failed to mark as read:', error) // Revert UI state on error setIsMarkedAsRead(false) } })() } 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 && (
( {alt} ) }} > {processedMarkdown || markdown}
)} {markdown || html ? ( <> {markdown ? ( renderedMarkdownHtml && finalHtml ? (
) : (
) ) : (
)} {/* Article menu for nostr-native articles */} {isNostrArticle && currentArticle && articleLinks && (
{showArticleMenu && (
)}
)} {/* Mark as Read button */} {activeAccount && (
)} {/* Author info card for nostr-native articles */} {isNostrArticle && currentArticle && (
)} ) : (

No readable content found for this URL.

)}
) } export default ContentPanel