import { useMemo, useState } from 'react' import { Pressable, ScrollView, StyleSheet, View, } from 'react-native' import { useSafeAreaInsets } from 'react-native-safe-area-context' import type { NativeStackScreenProps } from '@react-navigation/native-stack' import { useI18n, type TranslationKey } from '@d3ro/i18n' import type { RootStackParamList } from '../../App' import { MOBILE_THEME_MODES, type MobileThemeMode, } from '../theme/mobile-theme' import { ThemeButton, ThemeCard, ThemeText, } from '../theme/themed-components' import { useMobilePreferences, type SupportedMobileLocale, } from '../lib/preferences-context' import { useAuth } from '../lib/auth-context' type OnboardingProps = NativeStackScreenProps & { onFreshFinish?: (audience: OnboardingAudience) => void } type OnboardingAudience = 'existing' | 'new' export type OnboardingExit = 'main' | 'back' | 'none' export function resolveOnboardingExit( localSaved: boolean, authenticated: boolean, isReplay: boolean, canGoBack: boolean, ): OnboardingExit { if (!localSaved) return 'none' if (isReplay) return canGoBack ? 'back' : 'none' return authenticated ? 'main' : 'none' } interface TutorialSlide { eyebrow: TranslationKey title: TranslationKey body: TranslationKey bullets: readonly TranslationKey[] } const SLIDES: readonly TutorialSlide[] = [ { eyebrow: 'mobile.onboarding.stepWelcome', title: 'mobile.onboarding.welcomeTitle', body: 'mobile.onboarding.welcomeBody', bullets: [ 'mobile.onboarding.welcomeBulletRecord', 'mobile.onboarding.welcomeBulletSync', 'mobile.onboarding.welcomeBulletAccount', ], }, { eyebrow: 'mobile.onboarding.stepPrivacy', title: 'mobile.onboarding.privacyTitle', body: 'mobile.onboarding.privacyBody', bullets: [ 'mobile.onboarding.privacyBulletPermission', 'mobile.onboarding.privacyBulletCloud', 'mobile.onboarding.privacyBulletControl', ], }, { eyebrow: 'mobile.onboarding.stepWorkflow', title: 'mobile.onboarding.workflowTitle', body: 'mobile.onboarding.workflowBody', bullets: [ 'mobile.onboarding.workflowBulletRecord', 'mobile.onboarding.workflowBulletTranscribe', 'mobile.onboarding.workflowBulletShare', ], }, { eyebrow: 'mobile.onboarding.stepExplore', title: 'mobile.onboarding.exploreTitle', body: 'mobile.onboarding.exploreBody', bullets: [ 'mobile.onboarding.exploreBulletTabs', 'mobile.onboarding.exploreBulletImport', 'mobile.onboarding.exploreBulletPlan', ], }, { eyebrow: 'mobile.onboarding.stepPersonalize', title: 'mobile.onboarding.personalizeTitle', body: 'mobile.onboarding.personalizeBody', bullets: [ 'mobile.onboarding.personalizeBulletSystem', 'mobile.onboarding.personalizeBulletSync', 'mobile.onboarding.personalizeBulletReplay', ], }, ] as const export default function OnboardingScreen({ navigation, route, onFreshFinish, }: OnboardingProps): React.ReactElement { const insets = useSafeAreaInsets() const { t, locale, setLocale } = useI18n() const { user } = useAuth() const { preferences, palette, syncStatus, errorCode, updatePreferences, completeOnboarding, } = useMobilePreferences() const [step, setStep] = useState(0) const [audience, setAudience] = useState(null) const [saving, setSaving] = useState(false) const [localSaveFailed, setLocalSaveFailed] = useState(false) const isReplay = route.params?.replay === true const isLastStep = step === SLIDES.length - 1 const slide = SLIDES[step] const progress = useMemo( () => `${step + 1} / ${SLIDES.length}`, [step], ) function selectLocale(nextLocale: SupportedMobileLocale): void { if (locale === nextLocale) return setLocale(nextLocale) void updatePreferences({ locale: nextLocale }) } function selectTheme(nextTheme: MobileThemeMode): void { if (preferences.themeMode === nextTheme) return void updatePreferences({ themeMode: nextTheme }) } async function finish(outcome: 'completed' | 'skipped'): Promise { if (saving) return const selectedAudience = audience ?? 'existing' setSaving(true) setLocalSaveFailed(false) const result = await completeOnboarding(outcome) if (!result.localSaved) { setLocalSaveFailed(true) setSaving(false) return } onFreshFinish?.(selectedAudience) const exit = resolveOnboardingExit(true, user !== null, isReplay, navigation.canGoBack()) // Authenticated fresh completion changes the root navigator branch. The // parent observes needsOnboarding true -> false and resets to Main after // the new branch mounts; replacing here targets the already-stale // onboarding navigator and produces an unhandled REPLACE warning. if (exit === 'back') navigation.goBack() } return ( D3RO VOICE {isReplay ? t('mobile.onboarding.replayLabel') : t('mobile.onboarding.setupLabel')} {(['ko', 'en'] as const).map((candidate) => ( selectLocale(candidate)} style={({ pressed }) => [ styles.localeButton, { borderColor: locale === candidate ? palette.accent.main : palette.border.default, backgroundColor: locale === candidate ? palette.accent.dim : 'transparent', opacity: pressed ? 0.72 : 1, }, ]} > {candidate.toUpperCase()} ))} {progress} {t(slide.eyebrow)} {t(slide.title)} {t(slide.body)} {step === 0 && ( setAudience('existing')} /> setAudience('new')} /> )} {slide.bullets.map((bullet, index) => ( {String(index + 1).padStart(2, '0')} {t(bullet)} ))} {isLastStep && ( {t('mobile.onboarding.themeLabel')} {MOBILE_THEME_MODES.map((mode) => ( selectTheme(mode)} /> ))} )} {(localSaveFailed || errorCode !== null) && ( {localSaveFailed ? t('mobile.preferences.cacheWriteError') : t('mobile.preferences.syncWarning')} )} {step > 0 && ( setStep((current) => Math.max(0, current - 1))} disabled={saving} testID="onboarding-back" accessibilityHint={t('mobile.onboarding.backHint')} style={styles.flexButton} /> )} { if (isLastStep) { void finish('completed') } else { setStep((current) => Math.min(SLIDES.length - 1, current + 1)) } }} disabled={saving || (step === 0 && audience === null)} testID={isLastStep ? 'onboarding-complete' : 'onboarding-next'} accessibilityHint={isLastStep ? t('mobile.onboarding.completeHint') : t('mobile.onboarding.nextHint')} style={styles.flexButton} /> {!isReplay && ( void finish('skipped')} disabled={saving} testID="onboarding-skip" accessibilityHint={t('mobile.onboarding.skipHint')} /> )} {syncStatus === 'offline' && ( {t('mobile.preferences.offlineSaved')} )} ) } function AudienceChoice({ selected, title, description, testID, onPress, }: { selected: boolean title: string description: string testID: string onPress: () => void }): React.ReactElement { const { palette } = useMobilePreferences() return ( [ styles.audienceChoice, { borderColor: selected ? palette.accent.main : palette.border.default, backgroundColor: selected ? palette.accent.dim : palette.bg.card, opacity: pressed ? 0.74 : 1, }, ]} > {title} {description} ) } function ThemeChoice({ mode, selected, onPress, }: { mode: MobileThemeMode selected: boolean onPress: () => void }): React.ReactElement { const { t } = useI18n() const { palette } = useMobilePreferences() const labelKey: Record = { system: 'mobile.theme.system', light: 'mobile.theme.light', dark: 'mobile.theme.dark', } return ( [ styles.themeChoice, { borderColor: selected ? palette.accent.main : palette.border.default, backgroundColor: selected ? palette.accent.dim : palette.bg.card, opacity: pressed ? 0.72 : 1, }, ]} > {t(labelKey[mode])} ) } const styles = StyleSheet.create({ screen: { flex: 1 }, topBar: { minHeight: 76, paddingHorizontal: 20, paddingBottom: 12, borderBottomWidth: 1, flexDirection: 'row', alignItems: 'flex-end', justifyContent: 'space-between', gap: 12, }, brand: { letterSpacing: 2.6, marginBottom: 4 }, localeRow: { flexDirection: 'row', gap: 8 }, localeButton: { minWidth: 52, minHeight: 48, alignItems: 'center', justifyContent: 'center', borderRadius: 9, borderWidth: 1, }, scroll: { flex: 1 }, content: { paddingHorizontal: 20, paddingTop: 22 }, progressRow: { flexDirection: 'row', alignItems: 'center', gap: 12, marginBottom: 32 }, progressTrack: { height: 4, borderRadius: 2, flex: 1, overflow: 'hidden' }, progressFill: { height: 4, borderRadius: 2 }, eyebrow: { letterSpacing: 2.4, marginBottom: 10 }, title: { fontSize: 31, lineHeight: 37, letterSpacing: -0.8, marginBottom: 14 }, body: { fontSize: 15, lineHeight: 23, marginBottom: 24 }, audienceGrid: { gap: 10, marginBottom: 14 }, audienceChoice: { minHeight: 88, borderWidth: 1, borderRadius: 14, padding: 15 }, choiceDescription: { lineHeight: 18, marginTop: 7 }, bulletCard: { padding: 0, marginBottom: 20 }, bulletRow: { minHeight: 68, flexDirection: 'row', alignItems: 'center', gap: 13, padding: 14 }, bulletNumber: { width: 36, height: 36, borderRadius: 18, alignItems: 'center', justifyContent: 'center', }, bulletText: { flex: 1, lineHeight: 21 }, themeSection: { marginBottom: 20 }, themeLabel: { letterSpacing: 2, marginBottom: 10 }, themeGrid: { flexDirection: 'row', gap: 8 }, themeChoice: { flex: 1, minHeight: 52, alignItems: 'center', justifyContent: 'center', borderWidth: 1, borderRadius: 10, }, errorBox: { borderWidth: 1, borderRadius: 10, padding: 12, marginBottom: 14 }, actions: { gap: 6 }, primaryActions: { flexDirection: 'row', gap: 10 }, flexButton: { flex: 1 }, offlineNote: { textAlign: 'center', lineHeight: 18, marginTop: 12 }, })