import { useState } from 'react' import { nostrService } from '@/lib/nostr' import { useNostrAuth } from '@/hooks/useNostrAuth' import { t } from '@/lib/i18n' import { buildReviewTipZapRequestTags } from '@/lib/zapRequestBuilder' import { calculateReviewSplit } from '@/lib/platformCommissions' import type { Review } from '@/types/nostr' import type { Article } from '@/types/nostr' interface ReviewTipFormProps { review: Review article: Article onSuccess?: () => void onCancel?: () => void } export function ReviewTipForm({ review, article, onSuccess, onCancel }: ReviewTipFormProps): 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 } setLoading(true) setError(null) try { const privateKey = nostrService.getPrivateKey() if (!privateKey) { setError(t('reviewTip.form.error.noPrivateKey')) return } const split = calculateReviewSplit() // Build zap request tags let category: 'science-fiction' | 'scientific-research' | undefined if (article.category === 'author-presentation') { category = undefined } else if (article.category === 'science-fiction' || article.category === 'scientific-research') { category = article.category } else { category = undefined } const zapRequestTags = buildReviewTipZapRequestTags({ articleId: article.id, reviewId: review.id, authorPubkey: article.pubkey, reviewerPubkey: review.reviewerPubkey, ...(category ? { category } : {}), ...(article.seriesId ? { seriesId: article.seriesId } : {}), ...(text.trim() ? { text: text.trim() } : {}), }) // Create zap request event (kind 9734) and publish it // This zap request will be used by Alby to create the zap payment await nostrService.createZapRequest(review.reviewerPubkey, review.id, split.total, zapRequestTags) // Note: The actual zap payment is handled by Alby/WebLN when the user confirms // The zap request event (kind 9734) contains all the necessary information // Alby will use this to create the zap receipt (kind 9735) after payment // The user needs to complete the zap payment via Alby after this zap request is published setText('') onSuccess?.() } catch (submitError) { setError(submitError instanceof Error ? submitError.message : t('reviewTip.form.error.paymentFailed')) } finally { setLoading(false) } } if (!pubkey) { return (

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

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

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

{t('reviewTip.form.description', { amount: split.total, reviewer: split.reviewer, platform: split.platform })}