import { useEffect } from 'react'
import { useNostrAuth } from '@/hooks/useNostrAuth'
import { useAuthorPresentation } from '@/hooks/useAuthorPresentation'
import { t } from '@/lib/i18n'
import { NoAccountView } from './NoAccountView'
import { PresentationForm } from './PresentationForm'
import { useExistingPresentation } from './useExistingPresentation'
import { useAuthorPresentationState } from './useAuthorPresentationState'
function SuccessNotice(params: { pubkey: string | null }): React.ReactElement {
return (
{t('presentation.success')}
{t('presentation.successMessage')}
{params.pubkey ? (
) : null}
)
}
function LoadingNotice(): React.ReactElement {
return (
)
}
function AuthorPresentationFormView(props: { pubkey: string | null; profile: { name?: string; pubkey: string } | null }): React.ReactElement {
const { checkPresentationExists } = useAuthorPresentation(props.pubkey)
const presentation = useExistingPresentation({ pubkey: props.pubkey, checkPresentationExists })
const state = useAuthorPresentationState(props.pubkey, props.profile?.name, presentation.existingPresentation)
if (!props.pubkey) {
return
}
if (presentation.loadingPresentation) {
return
}
if (state.success) {
return
}
return (
{
void state.handleDelete()
}}
hasExistingPresentation={presentation.existingPresentation !== null}
/>
)
}
function useAutoLoadPubkey(accountExists: boolean | null, pubkey: string | null, connect: () => Promise): void {
useEffect(() => {
if (accountExists === true && !pubkey) {
void connect()
}
}, [accountExists, pubkey, connect])
}
export function AuthorPresentationEditor(): React.ReactElement {
const { pubkey, profile, accountExists, connect } = useNostrAuth()
useAutoLoadPubkey(accountExists, pubkey ?? null, connect)
return
}