import { useNavigate } from '@tanstack/react-router' import { Loader2 } from 'lucide-react' import { useEffect, useLayoutEffect, useRef } from 'react' import { Logo, LogoIcon } from '../../components/brand' import { Button } from '../../components/ui' import { cn } from '../../lib/utils' import { useAuth } from '../../context/AuthProvider' import { ConfirmationStep } from './ConfirmationStep' import { CountryStep } from './CountryStep' import { FirstGroupStep } from './FirstGroupStep' import { LanguageSwitcher } from './LanguageSwitcher' import { useOnboarding } from './OnboardingProvider' import { PaymentStep } from './PaymentStep' import { PlanStep } from './PlanStep' import { Stepper } from './Stepper' import { WelcomeStep } from './WelcomeStep' import { WhatsappStep } from './WhatsappStep' import { useTranslation } from 'react-i18next' export function OnboardingView() { const { user, isPending: authPending } = useAuth() const navigate = useNavigate() const { t } = useTranslation(['onboarding', 'common', 'groups']) const { step, steps, goTo, goNext, goBack, mpFeedback, waFeedback, createdGroup, setCreatedGroup, statusQuery, } = useOnboarding() /* Al cambiar de paso: se sabe hacia dónde se movió (para la animación) y el foco pasa al título del paso nuevo, así el lector de pantalla lo anuncia y el teclado sigue desde ahí. Si el paso ya enfocó su primer campo (`autoFocus`), se respeta. */ const stepIndex = steps.indexOf(step) const previousIndex = useRef(stepIndex) const direction = stepIndex < previousIndex.current ? 'back' : 'forward' const stepContainer = useRef(null) const isFirstStep = useRef(true) useLayoutEffect(() => { previousIndex.current = stepIndex }, [stepIndex]) useEffect(() => { if (isFirstStep.current) { isFirstStep.current = false return } window.scrollTo({ top: 0 }) const container = stepContainer.current if (!container || container.contains(document.activeElement)) return const heading = container.querySelector('h1, h2') if (!heading) return heading.tabIndex = -1 heading.focus({ preventScroll: true }) }, [step]) useEffect(() => { if (statusQuery.data?.completed) { void navigate({ to: '/' }) } }, [statusQuery.data, navigate]) if (authPending) { return (
) } if (!user) { return (

{t('signedOut.title')}

{t('signedOut.description')}

) } if (statusQuery.isPending) { return (
) } const firstName = user.name?.trim().split(/\s+/)[0] ?? t('fallbackName') return (
{/* Con Free no se muestran Cobros ni WhatsApp: el stepper refleja solo los pasos que se van a recorrer. */} ({ label: t(`steps.${id}`) }))} current={stepIndex} />
{step === 'welcome' ? : null} {step === 'country' ? : null} {step === 'plan' ? : null} {step === 'payment' ? ( ) : null} {step === 'whatsapp' ? ( ) : null} {step === 'group' ? ( { setCreatedGroup(group) // Reemplaza: el formulario ya no tiene que quedar en el historial // (volver a él crearía otro grupo). goTo('done', { replace: true }) }} /> ) : null} {step === 'done' && createdGroup ? : null}
) }