import { useState } from 'react' import { ImageUploadField } from './ImageUploadField' import { publishSeries } from '@/lib/articleMutations' import { useNostrAuth } from '@/hooks/useNostrAuth' import { nostrService } from '@/lib/nostr' import { t } from '@/lib/i18n' import type { ArticleDraft } from '@/lib/articlePublisherTypes' interface CreateSeriesModalProps { isOpen: boolean onClose: () => void onSuccess: () => void authorPubkey: string } interface SeriesDraft { title: string description: string preview: string coverUrl: string category: ArticleDraft['category'] } export function CreateSeriesModal({ isOpen, onClose, onSuccess, authorPubkey }: CreateSeriesModalProps): React.ReactElement | null { const { pubkey, isUnlocked } = useNostrAuth() const [draft, setDraft] = useState({ title: '', description: '', preview: '', coverUrl: '', category: 'science-fiction', }) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) if (!isOpen) { return null } const privateKey = nostrService.getPrivateKey() const canPublish = pubkey === authorPubkey && isUnlocked && privateKey !== null const handleSubmit = async (e: React.FormEvent): Promise => { e.preventDefault() if (!canPublish) { setError(t('series.create.error.notAuthor')) return } if (!draft.title.trim() || !draft.description.trim() || !draft.preview.trim()) { setError(t('series.create.error.missingFields')) return } setLoading(true) setError(null) try { if (!privateKey) { setError(t('series.create.error.notAuthor')) return } await publishSeries({ title: draft.title, description: draft.description, preview: draft.preview, ...(draft.coverUrl ? { coverUrl: draft.coverUrl } : {}), category: draft.category, authorPubkey, authorPrivateKey: privateKey, }) // Reset form setDraft({ title: '', description: '', preview: '', coverUrl: '', category: 'science-fiction', }) onSuccess() onClose() } catch (submitError) { setError(submitError instanceof Error ? submitError.message : t('series.create.error.publishFailed')) } finally { setLoading(false) } } const handleClose = (): void => { if (!loading) { setDraft({ title: '', description: '', preview: '', coverUrl: '', category: 'science-fiction', }) setError(null) onClose() } } return (

{t('series.create.title')}

{!canPublish && (

{t('series.create.error.notAuthor')}

)}
{ void handleSubmit(e) }} className="space-y-4">
setDraft({ ...draft, title: e.target.value })} className="w-full px-3 py-2 bg-cyber-darker border border-cyber-accent/30 rounded text-cyber-light focus:border-neon-cyan focus:outline-none" required disabled={loading || !canPublish} />