'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { useLocale, useTranslations } from 'next-intl'; import { useSnackbar } from 'notistack'; import { Box, Stack, Typography } from '@mui/material'; import { AppButton, AppIcon, PatientForm, RelationSelect, StepperHeader } from '@/components'; import BrandMark from '@/components/auth/BrandMark'; import { ROUTES } from '@/constants'; import { useCreatePatient } from '@/services/patients'; import { RELATION_CODES } from '@/services/patients/constants'; import type { CreatePatientInput, Relation } from '@/services/patients/types'; const ONBOARDING_MAX_WIDTH = 520; // Distinct per-option glyph (the prior defect: all four relations shared the generic 'account' // icon) — 'elderly' fits a parent, 'favorite' a spouse, 'infant' a child, 'account' one's self. const RELATION_ICONS: Record = { parent: 'elderly', spouse: 'favorite', child: 'infant', self: 'account', }; type Phase = 'welcome' | 'relation' | 'patient'; /** * The chrome-free A3 → A4 first-run journey: a one-screen welcome moment, then pick who care is * for, then register the first patient. `FocusedLayout` (the route group above this) strips the * bottom nav/bell so there's nothing to tab away to mid-setup. The welcome screen doesn't count * as a stepper step; relation → patient does. The chosen relation pre-shapes the patient (hidden * on the A4 form since it's already chosen here). On save it creates the patient and lands on * Home (A5). */ export default function OnboardingScreen() { const t = useTranslations('onboarding'); const tc = useTranslations('common'); const router = useRouter(); const locale = useLocale(); const { enqueueSnackbar } = useSnackbar(); const createPatient = useCreatePatient(); const [phase, setPhase] = useState('welcome'); const [relation, setRelation] = useState(null); const relationOptions = RELATION_CODES.map((code) => ({ code, label: t(`relation_${code}`), icon: RELATION_ICONS[code] ?? 'account', })); const handleCreate = (input: CreatePatientInput) => { createPatient.mutate( { ...input, relation }, { onSuccess: () => { enqueueSnackbar(t('saved'), { variant: 'success' }); router.replace(`/${locale}${ROUTES.HOME}`); }, }, ); }; if (phase === 'welcome') { return ( {t('welcome_title')} {t('welcome_subtitle')} setPhase('relation')}> {t('welcome_cta')} ); } const activeStep = phase === 'relation' ? 0 : 1; return ( {phase === 'relation' ? ( {t('relation_title')} {t('relation_subtitle')} setRelation(code as Relation)} /> setPhase('patient')} endIcon={ ) : ( {t('patient_title')} {t('patient_subtitle')} setPhase('relation')} cancelLabel={tc('back')} /> )} ); }