story-research-zapwall/hooks/useAuthorPresentation.ts
2026-01-05 23:37:29 +01:00

130 lines
3.8 KiB
TypeScript

import { useState } from 'react'
import { nostrService } from '@/lib/nostr'
import { articlePublisher } from '@/lib/articlePublisher'
import type { Article } from '@/types/nostr'
import type { NostrProfile } from '@/types/nostr'
interface AuthorPresentationDraft {
authorName: string
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
}
// Update Nostr profile (kind 0) with author name and picture
const profileUpdates: Partial<NostrProfile> = {
name: draft.authorName.trim(),
...(draft.pictureUrl ? { picture: draft.pictureUrl } : {}),
}
try {
await nostrService.updateProfile(profileUpdates)
} catch (e) {
console.error('Error updating profile:', e)
// Continue with article publication even if profile update fails
}
// Create presentation article
const title = `Présentation de ${draft.authorName.trim()}`
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
}
}
const deletePresentation = async (articleId: string): Promise<void> => {
if (!pubkey) {
throw new Error('Clé publique non disponible')
}
setLoading(true)
setError(null)
try {
const privateKey = nostrService.getPrivateKey()
if (!privateKey) {
throw new Error('Clé privée requise pour supprimer. Veuillez vous connecter avec un portefeuille Nostr qui fournit des capacités de signature.')
}
const { deleteArticleEvent } = await import('@/lib/articleMutations')
await deleteArticleEvent(articleId, pubkey, privateKey)
} catch (e) {
const errorMessage = e instanceof Error ? e.message : 'Erreur inconnue'
console.error('Error deleting presentation:', e)
setError(errorMessage)
throw e
} finally {
setLoading(false)
}
}
return {
loading,
error,
success,
publishPresentation,
checkPresentationExists,
deletePresentation,
}
}