52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { useState } from 'react'
|
|
import { articlePublisher } from '@/lib/articlePublisher'
|
|
import { nostrService } from '@/lib/nostr'
|
|
import type { ArticleDraft } from '@/lib/articlePublisher'
|
|
|
|
export function useArticlePublishing(pubkey: string | null) {
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [success, setSuccess] = useState(false)
|
|
|
|
const publishArticle = async (draft: ArticleDraft): Promise<string | null> => {
|
|
if (!pubkey) {
|
|
setError('Please connect with Nostr first')
|
|
return null
|
|
}
|
|
|
|
if (!draft.title.trim() || !draft.preview.trim() || !draft.content.trim()) {
|
|
setError('Please fill in all fields')
|
|
return null
|
|
}
|
|
|
|
setLoading(true)
|
|
setError(null)
|
|
|
|
try {
|
|
const privateKey = nostrService.getPrivateKey()
|
|
const result = await articlePublisher.publishArticle(draft, pubkey, privateKey ?? undefined)
|
|
|
|
if (result.success) {
|
|
setSuccess(true)
|
|
return result.articleId
|
|
}
|
|
|
|
setError(result.error ?? 'Failed to publish article')
|
|
return null
|
|
} catch (e) {
|
|
setError(e instanceof Error ? e.message : 'Failed to publish article')
|
|
return null
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
return {
|
|
loading,
|
|
error,
|
|
success,
|
|
publishArticle,
|
|
}
|
|
}
|
|
|