import { useEffect, useMemo, useState } from 'react' import { Card, Loader } from '../../components/common' import { endpoints, getErrorMessage } from '../../api/client' import { t } from '../../lib/i18n' import type { ContentPlan, PostStructure, Reporter, SavedContentPlan } from '../../types' import type { GenerationConfig } from '../../components/generator' import { ConversationPhaseStep } from './ConversationPhaseStep' import { createEmptyContentPlan } from './conversation-utils' interface IntentStepProps { config: GenerationConfig reporter: Reporter | null savedPlan: SavedContentPlan | null sessionId: string | null initialState: Record onSessionChange: (sessionId: string) => void onStateChange: (state: Record) => void onContinue: (state: Record) => void onBack: () => void onSkip: () => void submitting?: boolean } function intentFieldValue(value: unknown): string { if (!value) return '—' if (typeof value === 'string') return value || '—' if (Array.isArray(value)) return value.length > 0 ? value.join(', ') : '—' return '—' } export function IntentStep({ config, reporter, savedPlan, sessionId, initialState, onSessionChange, onStateChange, onContinue, onBack, onSkip, submitting = false, }: IntentStepProps) { const [structure, setStructure] = useState(null) const [loading, setLoading] = useState(true) const [loadError, setLoadError] = useState(null) useEffect(() => { let cancelled = false void (async () => { setLoading(true) setLoadError(null) try { const res = await endpoints.getStructure(config.postType) if (!cancelled) { setStructure(res.data as PostStructure) } } catch (error) { if (!cancelled) { setLoadError(getErrorMessage(error, t('Unknown error'))) } } finally { if (!cancelled) { setLoading(false) } } })() return () => { cancelled = true } }, [config.postType]) const context = useMemo(() => ({ post_type: structure ? { slug: structure.post_type, label: structure.post_type_label, meta_fields: structure.meta_fields || [], } : null, topic: config.topic, count: config.count, length: config.length, generation_type: config.generationType, reporter, sprint_mode: true, article_count: config.count, saved_content_plan: savedPlan ? { summary: savedPlan.summary, content_plan: savedPlan.content_plan, still_valid: savedPlan.still_valid, } : null, }), [config.count, config.generationType, config.length, config.topic, reporter, savedPlan, structure]) if (loading) { return (
) } if (loadError) { return (

{t('Error loading content types')}

{loadError}

) } return ( { const plan = ((draftState.content_plan as ContentPlan | undefined) || createEmptyContentPlan()) return (
{t('Collected Intent')}
{t('Audience')}: {intentFieldValue(plan.audience?.primary)}
{t('Goal')}: {intentFieldValue(plan.goal?.primary)}
{t('Focus Areas')}: {intentFieldValue(plan.focus_areas)}
{t('Research Scope')}:{' '} {plan.research_scope?.directive || '—'}
{plan.must_include && plan.must_include.length > 0 && (
{t('Must Include')}: {plan.must_include.join(', ')}
)} {plan.avoid && plan.avoid.length > 0 && (
{t('Avoid')}: {plan.avoid.join(', ')}
)}
{t('Summary')}: {plan.summary || '—'}
) }} /> ) }