import type { Article } from '@/types/nostr' import { useNostrAuth } from '@/hooks/useNostrAuth' import { useArticlePayment } from '@/hooks/useArticlePayment' import { ArticlePreview } from './ArticlePreview' import { PaymentModal } from './PaymentModal' import { Card, Badge } from './ui' import { useToast } from './ui/ToastContainer' import { t } from '@/lib/i18n' import Link from 'next/link' interface ArticleCardProps { article: Article onUnlock?: (article: Article) => void allArticles?: Article[] unlockedArticles?: Set } function ArticleHeader({ article }: { article: Article }): React.ReactElement { return (

{article.title}

{article.paid && ( {t('article.unlocked.badge')} )}
{t('publication.viewAuthor')}
) } function ArticleMeta({ article, error, paymentInvoice, onClose, onPaymentComplete, }: { article: Article error: string | null paymentInvoice: ReturnType['paymentInvoice'] onClose: () => void onPaymentComplete: () => void }): React.ReactElement { return ( <> {error &&

{error}

}
{t('publication.published', { date: new Date(article.createdAt * 1000).toLocaleDateString() })}
{paymentInvoice && ( )} ) } interface UseArticleCardStateParams { article: Article pubkey: string | null connect: (() => Promise) | undefined onUnlock: ((article: Article) => void) | undefined showToast: (message: string, variant?: 'success' | 'info' | 'warning' | 'error', duration?: number) => void } function useArticleCardState(params: UseArticleCardStateParams): { loading: boolean error: string | null paymentInvoice: ReturnType['paymentInvoice'] handleUnlock: () => Promise handlePaymentComplete: () => Promise handleCloseModal: () => void } { return useArticlePayment({ article: params.article, pubkey: params.pubkey, onUnlockSuccess: () => { params.showToast(t('article.unlock.success'), 'success') params.onUnlock?.(params.article) }, connect: params.connect, showToast: params.showToast, }) } function ArticleCardContent(params: { article: Article loading: boolean error: string | null paymentInvoice: ReturnType['paymentInvoice'] handleUnlock: () => Promise handlePaymentComplete: () => Promise handleCloseModal: () => void allArticles?: Article[] unlockedArticles?: Set }): React.ReactElement { return ( <>
{ void params.handleUnlock() }} {...(params.allArticles !== undefined ? { allArticles: params.allArticles } : {})} {...(params.unlockedArticles !== undefined ? { unlockedArticles: params.unlockedArticles } : {})} />
{ void params.handlePaymentComplete() }} /> ) } export function ArticleCard({ article, onUnlock, allArticles, unlockedArticles }: ArticleCardProps): React.ReactElement { const { pubkey, connect } = useNostrAuth() const { showToast } = useToast() const state = useArticleCardState({ article, pubkey: pubkey ?? null, connect, onUnlock, showToast, }) const cardClassName = article.paid ? 'mb-0 border-2 border-neon-green/40 shadow-[0_0_5px_#00ff41,0_0_10px_#00ff41]' : 'mb-0' return ( ) }