d3ro-voice/apps/mobile-rn/src/screens/OnboardingScreen.tsx
2026-08-29 18:33:45 +09:00

578 lines
18 KiB
TypeScript

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<RootStackParamList, 'Onboarding'> & {
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<OnboardingAudience | null>(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<void> {
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 (
<View
style={[styles.screen, { backgroundColor: palette.bg.app }]}
testID="onboarding-screen"
accessibilityLabel={t('mobile.onboarding.accessibilityScreen')}
>
<View
style={[
styles.topBar,
{
paddingTop: insets.top + 8,
borderBottomColor: palette.border.subtle,
},
]}
>
<View>
<ThemeText variant="label" color="accent" style={styles.brand}>
D3RO VOICE
</ThemeText>
<ThemeText variant="small" color="muted">
{isReplay
? t('mobile.onboarding.replayLabel')
: t('mobile.onboarding.setupLabel')}
</ThemeText>
</View>
<View
style={styles.localeRow}
accessibilityRole="radiogroup"
accessibilityLabel={t('mobile.onboarding.languageLabel')}
>
{(['ko', 'en'] as const).map((candidate) => (
<Pressable
key={candidate}
accessibilityRole="radio"
accessibilityState={{ selected: locale === candidate }}
accessibilityLabel={candidate === 'ko' ? '한국어' : 'English'}
testID={`onboarding-language-${candidate}`}
onPress={() => 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,
},
]}
>
<ThemeText
variant="small"
color={locale === candidate ? 'accent' : 'muted'}
>
{candidate.toUpperCase()}
</ThemeText>
</Pressable>
))}
</View>
</View>
<ScrollView
style={styles.scroll}
contentContainerStyle={[
styles.content,
{ paddingBottom: Math.max(24, insets.bottom + 16) },
]}
showsVerticalScrollIndicator={false}
>
<View style={styles.progressRow}>
<ThemeText variant="label" color="muted">
{progress}
</ThemeText>
<View
style={[styles.progressTrack, { backgroundColor: palette.bg.inset }]}
accessibilityRole="progressbar"
accessibilityValue={{
min: 1,
max: SLIDES.length,
now: step + 1,
text: progress,
}}
>
<View
style={[
styles.progressFill,
{
backgroundColor: palette.accent.main,
width: `${((step + 1) / SLIDES.length) * 100}%`,
},
]}
/>
</View>
</View>
<View
testID={`onboarding-step-${step + 1}`}
accessibilityLiveRegion="polite"
>
<ThemeText variant="label" color="accent" style={styles.eyebrow}>
{t(slide.eyebrow)}
</ThemeText>
<ThemeText
variant="title"
color="primary"
accessibilityRole="header"
style={styles.title}
>
{t(slide.title)}
</ThemeText>
<ThemeText variant="body" color="secondary" style={styles.body}>
{t(slide.body)}
</ThemeText>
</View>
{step === 0 && (
<View
style={styles.audienceGrid}
accessibilityRole="radiogroup"
accessibilityLabel={t('mobile.onboarding.accountPathLabel')}
>
<AudienceChoice
selected={audience === 'existing'}
title={t('mobile.onboarding.existingUser')}
description={t('mobile.onboarding.existingUserDesc')}
testID="onboarding-audience-existing"
onPress={() => setAudience('existing')}
/>
<AudienceChoice
selected={audience === 'new'}
title={t('mobile.onboarding.newUser')}
description={t('mobile.onboarding.newUserDesc')}
testID="onboarding-audience-new"
onPress={() => setAudience('new')}
/>
</View>
)}
<ThemeCard style={styles.bulletCard}>
{slide.bullets.map((bullet, index) => (
<View
key={bullet}
style={[
styles.bulletRow,
index < slide.bullets.length - 1 && {
borderBottomColor: palette.border.subtle,
borderBottomWidth: 1,
},
]}
>
<View
style={[
styles.bulletNumber,
{ backgroundColor: palette.accent.dim },
]}
>
<ThemeText variant="small" color="accent">
{String(index + 1).padStart(2, '0')}
</ThemeText>
</View>
<ThemeText variant="body" color="primary" style={styles.bulletText}>
{t(bullet)}
</ThemeText>
</View>
))}
</ThemeCard>
{isLastStep && (
<View style={styles.themeSection}>
<ThemeText variant="label" color="muted" style={styles.themeLabel}>
{t('mobile.onboarding.themeLabel')}
</ThemeText>
<View
style={styles.themeGrid}
accessibilityRole="radiogroup"
accessibilityLabel={t('mobile.onboarding.themeLabel')}
>
{MOBILE_THEME_MODES.map((mode) => (
<ThemeChoice
key={mode}
mode={mode}
selected={preferences.themeMode === mode}
onPress={() => selectTheme(mode)}
/>
))}
</View>
</View>
)}
{(localSaveFailed || errorCode !== null) && (
<View
accessibilityRole="alert"
testID="onboarding-settings-error"
style={[
styles.errorBox,
{
borderColor: palette.tag.red,
backgroundColor: palette.bg.card,
},
]}
>
<ThemeText variant="small" color="danger">
{localSaveFailed
? t('mobile.preferences.cacheWriteError')
: t('mobile.preferences.syncWarning')}
</ThemeText>
</View>
)}
<View style={styles.actions}>
<View style={styles.primaryActions}>
{step > 0 && (
<ThemeButton
label={t('mobile.onboarding.back')}
variant="secondary"
onPress={() => setStep((current) => Math.max(0, current - 1))}
disabled={saving}
testID="onboarding-back"
accessibilityHint={t('mobile.onboarding.backHint')}
style={styles.flexButton}
/>
)}
<ThemeButton
label={isLastStep
? audience === 'new'
? t('mobile.onboarding.completeNew')
: t('mobile.onboarding.completeExisting')
: t('mobile.onboarding.next')}
onPress={() => {
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}
/>
</View>
{!isReplay && (
<ThemeButton
label={saving
? t('mobile.onboarding.saving')
: t('mobile.onboarding.skip')}
variant="quiet"
onPress={() => void finish('skipped')}
disabled={saving}
testID="onboarding-skip"
accessibilityHint={t('mobile.onboarding.skipHint')}
/>
)}
</View>
{syncStatus === 'offline' && (
<ThemeText
variant="small"
color="muted"
accessibilityRole="alert"
style={styles.offlineNote}
>
{t('mobile.preferences.offlineSaved')}
</ThemeText>
)}
</ScrollView>
</View>
)
}
function AudienceChoice({
selected,
title,
description,
testID,
onPress,
}: {
selected: boolean
title: string
description: string
testID: string
onPress: () => void
}): React.ReactElement {
const { palette } = useMobilePreferences()
return (
<Pressable
accessibilityRole="radio"
accessibilityState={{ selected }}
accessibilityLabel={`${title}. ${description}`}
testID={testID}
onPress={onPress}
style={({ pressed }) => [
styles.audienceChoice,
{
borderColor: selected ? palette.accent.main : palette.border.default,
backgroundColor: selected ? palette.accent.dim : palette.bg.card,
opacity: pressed ? 0.74 : 1,
},
]}
>
<ThemeText variant="heading" color={selected ? 'accent' : 'primary'}>
{title}
</ThemeText>
<ThemeText variant="small" color="secondary" style={styles.choiceDescription}>
{description}
</ThemeText>
</Pressable>
)
}
function ThemeChoice({
mode,
selected,
onPress,
}: {
mode: MobileThemeMode
selected: boolean
onPress: () => void
}): React.ReactElement {
const { t } = useI18n()
const { palette } = useMobilePreferences()
const labelKey: Record<MobileThemeMode, TranslationKey> = {
system: 'mobile.theme.system',
light: 'mobile.theme.light',
dark: 'mobile.theme.dark',
}
return (
<Pressable
accessibilityRole="radio"
accessibilityState={{ selected }}
accessibilityLabel={t(labelKey[mode])}
testID={`onboarding-theme-${mode}`}
onPress={onPress}
style={({ pressed }) => [
styles.themeChoice,
{
borderColor: selected ? palette.accent.main : palette.border.default,
backgroundColor: selected ? palette.accent.dim : palette.bg.card,
opacity: pressed ? 0.72 : 1,
},
]}
>
<ThemeText variant="small" color={selected ? 'accent' : 'primary'}>
{t(labelKey[mode])}
</ThemeText>
</Pressable>
)
}
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 },
})