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 state = useSponsoringFormState() const onSubmit = (e: React.FormEvent): void => { e.preventDefault() void handleSubmit({ pubkey, author, setInstructions: state.setInstructions, setError: state.setError, setLoading: state.setLoading }) } if (!pubkey) { return { void connect() }} /> } if (!author.mainnetAddress) { return } if (state.instructions) { return ( { state.setInstructions(null) onSuccess?.() }} onCancel={() => { state.setInstructions(null) onCancel?.() }} /> ) } return ( ) } async function handleSubmit(params: { pubkey: string | null author: AuthorPresentationArticle setInstructions: (value: SponsoringInstructionsState) => void setError: (value: string | null) => void setLoading: (value: boolean) => void }): Promise { if (!params.pubkey || !params.author.mainnetAddress) { return } await submitSponsoring({ pubkey: params.pubkey, author: params.author, setInstructions: params.setInstructions, setError: params.setError, setLoading: params.setLoading, }) } type SponsoringInstructionsState = { authorAddress: string platformAddress: string authorBtc: string platformBtc: string } function useSponsoringFormState(): { text: string setText: (value: string) => void loading: boolean setLoading: (value: boolean) => void error: string | null setError: (value: string | null) => void instructions: SponsoringInstructionsState | null setInstructions: (value: SponsoringInstructionsState | null) => void } { const [text, setText] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [instructions, setInstructions] = useState(null) return { text, setText, loading, setLoading, error, setError, instructions, setInstructions } } async function submitSponsoring(params: { pubkey: string author: AuthorPresentationArticle setInstructions: (value: SponsoringInstructionsState) => void setError: (value: string | null) => void setLoading: (value: boolean) => void }): Promise { params.setLoading(true) params.setError(null) try { const privateKey = nostrService.getPrivateKey() if (!privateKey) { params.setError(t('sponsoring.form.error.noPrivateKey')) return } const result = await sponsoringPaymentService.createSponsoringPayment({ authorPubkey: params.author.pubkey, authorMainnetAddress: params.author.mainnetAddress ?? '', amount: 0.046 }) if (!result.success) { params.setError(result.error ?? t('sponsoring.form.error.paymentFailed')) return } console.warn('Sponsoring payment info:', { authorAddress: result.authorAddress, platformAddress: result.platformAddress, authorAmount: result.split.authorSats, platformAmount: result.split.platformSats, totalAmount: result.split.totalSats }) params.setInstructions({ authorAddress: result.authorAddress, platformAddress: result.platformAddress, authorBtc: (result.split.authorSats / 100_000_000).toFixed(8), platformBtc: (result.split.platformSats / 100_000_000).toFixed(8) }) } catch (submitError) { params.setError(submitError instanceof Error ? submitError.message : t('sponsoring.form.error.paymentFailed')) } finally { params.setLoading(false) } } function SponsoringConnectRequired(params: { onConnect: () => void }): React.ReactElement { return (

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

) } function SponsoringNoAddress(): React.ReactElement { return (

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

) } function SponsoringInstructions(params: { instructions: SponsoringInstructionsState; onClose: () => void; onCancel: () => void }): React.ReactElement { return (

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

{t('sponsoring.form.instructions', { authorAddress: params.instructions.authorAddress, platformAddress: params.instructions.platformAddress, authorAmount: params.instructions.authorBtc, platformAmount: params.instructions.platformBtc })}

) } function SponsoringFormView(params: { text: string setText: (value: string) => void loading: boolean error: string | null onSubmit: (e: React.FormEvent) => void onCancel?: () => void }): React.ReactElement { return (

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

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