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 } 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
}
function ArticleHeader({ article }: { article: Article }): React.ReactElement {
return (
{article.title}
{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
}): React.ReactElement {
return (
<>
{
void params.handleUnlock()
}}
/>
{
void params.handlePaymentComplete()
}}
/>
>
)
}
export function ArticleCard({ article, onUnlock }: ArticleCardProps): React.ReactElement {
const { pubkey, connect } = useNostrAuth()
const { showToast } = useToast()
const state = useArticleCardState({
article,
pubkey: pubkey ?? null,
connect,
onUnlock,
showToast,
})
return (
)
}