Files
gruperly/apps/web/src/features/onboarding/OnboardingView.tsx
Jose Selesan abb18f1815 Language selector on onboarding screen
Fixed docker-compose
2026-11-10 18:27:15 -03:00

158 lines
5.4 KiB
TypeScript

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<HTMLDivElement>(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<HTMLElement>('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 (
<div className="flex min-h-dvh items-center justify-center">
<Loader2 className="size-6 animate-spin text-foreground/40" />
</div>
)
}
if (!user) {
return (
<div className="flex min-h-dvh items-center justify-center px-4">
<div className="w-full max-w-sm space-y-4 text-center">
<span className="mx-auto flex size-14 items-center justify-center rounded-2xl bg-accent-soft">
<LogoIcon className="size-8" />
</span>
<h1 className="text-2xl font-bold text-primary text-balance">{t('signedOut.title')}</h1>
<p className="text-sm text-foreground/60">
{t('signedOut.description')}
</p>
<Button
variant="primary"
className="w-full"
onClick={() => void navigate({ to: '/login' })}
>
{t('signedOut.action')}
</Button>
</div>
</div>
)
}
if (statusQuery.isPending) {
return (
<div className="flex min-h-dvh items-center justify-center">
<Loader2 className="size-6 animate-spin text-foreground/40" />
</div>
)
}
const firstName = user.name?.trim().split(/\s+/)[0] ?? t('fallbackName')
return (
<main className="mx-auto flex min-h-dvh w-full max-w-md flex-col px-4 pt-8 pb-[calc(3rem+env(safe-area-inset-bottom))] sm:pt-12">
<header className="relative mb-6 flex items-center justify-center">
<Logo className="h-6" />
<div className="absolute right-0">
<LanguageSwitcher />
</div>
</header>
{/* Con Free no se muestran Cobros ni WhatsApp: el stepper refleja solo
los pasos que se van a recorrer. */}
<Stepper
steps={steps.map((id) => ({ label: t(`steps.${id}`) }))}
current={stepIndex}
/>
<div
key={step}
ref={stepContainer}
className={cn(
'mt-8 motion-reduce:animate-none [&_h1]:outline-none [&_h2]:outline-none',
direction === 'back' ? 'animate-step-enter-back' : 'animate-step-enter',
)}
>
{step === 'welcome' ? <WelcomeStep name={firstName} onNext={goNext} /> : null}
{step === 'country' ? <CountryStep onNext={goNext} onBack={goBack} /> : null}
{step === 'plan' ? <PlanStep /> : null}
{step === 'payment' ? (
<PaymentStep feedback={mpFeedback} onNext={goNext} onBack={goBack} />
) : null}
{step === 'whatsapp' ? (
<WhatsappStep feedback={waFeedback} onNext={goNext} onBack={goBack} />
) : null}
{step === 'group' ? (
<FirstGroupStep
onBack={goBack}
onCompleted={(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 ? <ConfirmationStep group={createdGroup} /> : null}
</div>
</main>
)
}