import { useState } from 'react' import { nostrService } from '@/lib/nostr' import { useNostrAuth } from '@/hooks/useNostrAuth' import { t } from '@/lib/i18n' import { sponsoringPaymentService } from '@/lib/sponsoringPayment' import type { AuthorPresentationArticle } from '@/types/nostr' interface SponsoringFormProps { author: AuthorPresentationArticle onSuccess?: () => void onCancel?: () => void } export function SponsoringForm({ author, onSuccess, onCancel }: SponsoringFormProps): React.ReactElement { const { pubkey, connect } = useNostrAuth() const [text, setText] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const handleSubmit = async (e: React.FormEvent): Promise => { e.preventDefault() if (!pubkey) { await connect() return } if (!author.mainnetAddress) { setError(t('sponsoring.form.error.noAddress')) return } setLoading(true) setError(null) try { const privateKey = nostrService.getPrivateKey() if (!privateKey) { setError(t('sponsoring.form.error.noPrivateKey')) return } // Create sponsoring payment request const result = await sponsoringPaymentService.createSponsoringPayment({ authorPubkey: author.pubkey, authorMainnetAddress: author.mainnetAddress, amount: 0.046, // Fixed amount for sponsoring }) if (!result.success) { setError(result.error ?? t('sponsoring.form.error.paymentFailed')) return } // Note: Sponsoring is done via Bitcoin mainnet, not Lightning zap // The user needs to create a Bitcoin transaction with two outputs: // 1. Author address: result.split.authorSats // 2. Platform address: result.split.platformSats // After the transaction is confirmed, we can create a zap receipt for tracking // Store payment info for later verification // The user will need to provide the transaction ID after payment console.log('Sponsoring payment info:', { authorAddress: result.authorAddress, platformAddress: result.platformAddress, authorAmount: result.split.authorSats, platformAmount: result.split.platformSats, totalAmount: result.split.totalSats, }) // Show instructions to user alert(t('sponsoring.form.instructions', { authorAddress: result.authorAddress, platformAddress: result.platformAddress, authorAmount: (result.split.authorSats / 100_000_000).toFixed(8), platformAmount: (result.split.platformSats / 100_000_000).toFixed(8), })) setText('') onSuccess?.() } catch (submitError) { setError(submitError instanceof Error ? submitError.message : t('sponsoring.form.error.paymentFailed')) } finally { setLoading(false) } } if (!pubkey) { return (

{t('sponsoring.form.connectRequired')}

) } if (!author.mainnetAddress) { return (

{t('sponsoring.form.error.noAddress')}

) } return (
{ void handleSubmit(e) }} className="border border-neon-cyan/30 rounded-lg p-4 bg-cyber-dark space-y-4">

{t('sponsoring.form.title')}

{t('sponsoring.form.description', { amount: '0.046' })}