import type { Page } from '@/types/nostr' import { t } from '@/lib/i18n' import { useNostrAuth } from '@/hooks/useNostrAuth' import { useEffect, useState } from 'react' import { objectCache } from '@/lib/objectCache' interface ArticlePagesProps { pages: Page[] articleId: string } export function ArticlePages({ pages, articleId }: ArticlePagesProps): React.ReactElement | null { const { pubkey } = useNostrAuth() const [hasPurchased, setHasPurchased] = useState(false) useEffect(() => { const checkPurchase = async (): Promise => { if (!pubkey || !articleId) { setHasPurchased(false) return } try { // Check if user has purchased this article from cache const purchases = await objectCache.getAll('purchase') const userPurchases = purchases.filter((p) => { if (typeof p !== 'object' || p === null) { return false } const purchase = p as { payerPubkey?: string; articleId?: string } return purchase.payerPubkey === pubkey && purchase.articleId === articleId }) setHasPurchased(userPurchases.length > 0) } catch (error) { console.error('Error checking purchase status:', error) setHasPurchased(false) } } void checkPurchase() }, [pubkey, articleId]) if (!pages || pages.length === 0) { return null } // If user hasn't purchased, show locked message if (!hasPurchased) { return (

{t('article.pages.title')}

{t('article.pages.locked.title')}

{t('article.pages.locked.message', { count: pages.length })}

) } // User has purchased, show all pages return (

{t('article.pages.title')}

{pages.map((page) => ( ))}
) } function PageDisplay({ page }: { page: Page }): React.ReactElement { return (

{t('page.number', { number: page.number })}

{t(`page.type.${page.type}`)}
{page.type === 'markdown' ? (
{page.content || {t('page.markdown.empty')}}
) : (
{page.content ? ( {t('page.image.alt', ) : (
{t('page.image.empty')}
)}
)}
) }