89 lines
2.5 KiB
TypeScript
89 lines
2.5 KiB
TypeScript
import { useState } from 'react'
|
|
import { nostrService } from '@/lib/nostr'
|
|
import { articlePublisher } from '@/lib/articlePublisher'
|
|
import type { Article } from '@/types/nostr'
|
|
|
|
interface AuthorPresentationDraft {
|
|
presentation: string
|
|
contentDescription: string
|
|
mainnetAddress: string
|
|
pictureUrl?: string
|
|
}
|
|
|
|
export function useAuthorPresentation(pubkey: string | null) {
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [success, setSuccess] = useState(false)
|
|
|
|
const publishPresentation = async (draft: AuthorPresentationDraft): Promise<void> => {
|
|
if (!pubkey) {
|
|
setError('Clé publique non disponible')
|
|
return
|
|
}
|
|
|
|
setLoading(true)
|
|
setError(null)
|
|
|
|
try {
|
|
const privateKey = nostrService.getPrivateKey()
|
|
if (!privateKey) {
|
|
setError('Clé privée requise pour publier. Veuillez vous connecter avec un portefeuille Nostr qui fournit des capacités de signature.')
|
|
setLoading(false)
|
|
return
|
|
}
|
|
|
|
// Create presentation article
|
|
const title = `Présentation de ${nostrService.getPublicKey()?.substring(0, 16)}...`
|
|
const preview = draft.presentation.substring(0, 200)
|
|
const fullContent = `${draft.presentation}\n\n---\n\nDescription du contenu :\n${draft.contentDescription}`
|
|
|
|
const result = await articlePublisher.publishPresentationArticle(
|
|
{
|
|
title,
|
|
preview,
|
|
content: fullContent,
|
|
presentation: draft.presentation,
|
|
contentDescription: draft.contentDescription,
|
|
mainnetAddress: draft.mainnetAddress,
|
|
...(draft.pictureUrl ? { pictureUrl: draft.pictureUrl } : {}),
|
|
},
|
|
pubkey,
|
|
privateKey
|
|
)
|
|
|
|
if (result.success) {
|
|
setSuccess(true)
|
|
} else {
|
|
setError(result.error ?? 'Erreur lors de la publication')
|
|
}
|
|
} catch (e) {
|
|
const errorMessage = e instanceof Error ? e.message : 'Erreur inconnue'
|
|
console.error('Error publishing presentation:', e)
|
|
setError(errorMessage)
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
const checkPresentationExists = async (): Promise<Article | null> => {
|
|
if (!pubkey) {
|
|
return null
|
|
}
|
|
|
|
try {
|
|
return await articlePublisher.getAuthorPresentation(pubkey)
|
|
} catch (e) {
|
|
console.error('Error checking presentation:', e)
|
|
return null
|
|
}
|
|
}
|
|
|
|
return {
|
|
loading,
|
|
error,
|
|
success,
|
|
publishPresentation,
|
|
checkPresentationExists,
|
|
}
|
|
}
|