2026-01-13 14:49:19 +01:00

41 lines
1.3 KiB
TypeScript

import { useEffect, useState } from 'react'
import type { Article } from '@/types/nostr'
export function useExistingPresentation(params: {
pubkey: string | null
checkPresentationExists: () => Promise<Article | null>
}): { existingPresentation: Article | null; loadingPresentation: boolean } {
const [existingPresentation, setExistingPresentation] = useState<Article | null>(null)
const [loadingPresentation, setLoadingPresentation] = useState(true)
useEffect(() => {
void loadExistingPresentation({
pubkey: params.pubkey,
checkPresentationExists: params.checkPresentationExists,
setExistingPresentation,
setLoadingPresentation,
})
}, [params.pubkey, params.checkPresentationExists])
return { existingPresentation, loadingPresentation }
}
async function loadExistingPresentation(params: {
pubkey: string | null
checkPresentationExists: () => Promise<Article | null>
setExistingPresentation: (value: Article | null) => void
setLoadingPresentation: (value: boolean) => void
}): Promise<void> {
if (!params.pubkey) {
params.setLoadingPresentation(false)
return
}
try {
params.setExistingPresentation(await params.checkPresentationExists())
} catch (e) {
console.error('[AuthorPresentationEditor] Error loading presentation:', e)
} finally {
params.setLoadingPresentation(false)
}
}