2026-01-14 01:08:33 +01:00

81 lines
2.8 KiB
TypeScript

import { useEffect } from 'react'
import { useNostrAuth } from '@/hooks/useNostrAuth'
import { useAuthorPresentation } from '@/hooks/useAuthorPresentation'
import { Button } from '../ui'
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 (
<div className="border border-neon-green/50 rounded-lg p-6 bg-neon-green/10">
<h3 className="text-lg font-semibold text-neon-green mb-2">{t('presentation.success')}</h3>
<p className="text-cyber-accent mb-4">{t('presentation.successMessage')}</p>
{params.pubkey ? (
<div className="mt-4">
<a href={`/author/${params.pubkey}`}>
<Button variant="primary">
{t('presentation.manageSeries')}
</Button>
</a>
</div>
) : null}
</div>
)
}
function LoadingNotice(): React.ReactElement {
return (
<div className="text-center py-12">
<p className="text-cyber-accent/70">{t('common.loading')}</p>
</div>
)
}
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 <NoAccountView />
}
if (presentation.loadingPresentation) {
return <LoadingNotice />
}
if (state.success) {
return <SuccessNotice pubkey={props.pubkey} />
}
return (
<PresentationForm
draft={state.draft}
setDraft={state.setDraft}
validationError={state.validationError}
error={state.error}
loading={state.loading}
handleSubmit={state.handleSubmit}
deleting={state.deleting}
handleDelete={() => {
void state.handleDelete()
}}
hasExistingPresentation={presentation.existingPresentation !== null}
/>
)
}
function useAutoLoadPubkey(accountExists: boolean | null, pubkey: string | null, connect: () => Promise<void>): 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 <AuthorPresentationFormView pubkey={pubkey ?? null} profile={profile} />
}