import { useState } from 'react' import { publishReview } from '@/lib/articleMutations' import { nostrService } from '@/lib/nostr' import { useNostrAuth } from '@/hooks/useNostrAuth' import { t } from '@/lib/i18n' import type { Article } from '@/types/nostr' interface ReviewFormProps { article: Article onSuccess?: () => void onCancel?: () => void } export function ReviewForm({ article, onSuccess, onCancel }: ReviewFormProps): React.ReactElement { const { pubkey, connect } = useNostrAuth() const [content, setContent] = useState('') const [title, setTitle] = useState('') 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 (!content.trim()) { setError(t('review.form.error.contentRequired')) return } setLoading(true) setError(null) try { const privateKey = nostrService.getPrivateKey() if (!privateKey) { setError(t('review.form.error.noPrivateKey')) return } const category = article.category ?? 'science-fiction' const seriesId = article.seriesId ?? '' await publishReview({ articleId: article.id, seriesId, category, authorPubkey: article.pubkey, reviewerPubkey: pubkey, content: content.trim(), title: title.trim() || undefined, text: text.trim() || undefined, authorPrivateKey: privateKey, }) setContent('') setTitle('') setText('') onSuccess?.() } catch (e) { setError(e instanceof Error ? e.message : t('review.form.error.publishFailed')) } finally { setLoading(false) } } if (!pubkey) { return (

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

) } return (

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

{ setTitle(e.target.value) }} placeholder={t('review.form.title.placeholder')} className="w-full px-3 py-2 bg-cyber-darker border border-neon-cyan/30 rounded text-cyber-accent focus:border-neon-cyan focus:outline-none" />