import React,{useState} from 'react'; import {Alert,ActivityIndicator,Pressable,ScrollView,Text,TextInput,View} from 'react-native'; import {useRouter} from 'expo-router'; import {SafeAreaView} from 'react-native-safe-area-context'; import {createUserWithEmailAndPassword,signInWithEmailAndPassword,sendPasswordResetEmail,signOut} from '@react-native-firebase/auth'; import {auth} from '../src/firebase/client'; import {useAuth} from '../src/firebase/auth-state'; import {useAppState} from '../src/state/app-state'; import {theme} from '../src/design/theme'; export default function AccountScreen(){ const router=useRouter();const {user}=useAuth();const {syncStatus,syncError,retry}=useAppState(); const [email,setEmail]=useState(''),[password,setPassword]=useState(''),[register,setRegister]=useState(false),[busy,setBusy]=useState(false),[error,setError]=useState(null); async function submit(){setBusy(true);setError(null);try{if(register)await createUserWithEmailAndPassword(auth,email.trim(),password);else await signInWithEmailAndPassword(auth,email.trim(),password);setPassword('');}catch(e){const code=(e as {code?:string}).code;setError(code==='auth/network-request-failed'?'Connexion indisponible. Réessaie lorsque le réseau est disponible.':code==='auth/weak-password'?'Choisis un mot de passe d’au moins 6 caractères.':code==='auth/email-already-in-use'?'Un compte utilise déjà cette adresse.':'Connexion impossible. Vérifie ton adresse et ton mot de passe.');}finally{setBusy(false);}} const button=(title:string,action:()=>void)=> {title}; return router.canGoBack()?router.back():router.replace('/')}>RetourMon compte {user?<>{user.email}{syncStatus==='synced'?'Profil synchronisé':syncStatus==='pending'?'Modifications en attente':syncStatus==='loading'?'Chargement…':'Synchronisation indisponible'}{syncError&&{syncError}}{syncStatus==='error'&&button('Réessayer',retry)}{button('Se déconnecter',()=>{if(syncStatus==='pending'||syncStatus==='error'){Alert.alert('Modifications en attente','Elles resteront sur cet appareil, liées à ce compte.',[{text:'Annuler',style:'cancel'},{text:'Se déconnecter',onPress:()=>void signOut(auth).catch(()=>setError('Déconnexion impossible.'))}]);}else void signOut(auth).catch(()=>setError('Déconnexion impossible.'));})}:<>Connecte-toi pour retrouver ton profil et tes préférences. Les données du mode local restent sur cet appareil.{button(register?'Créer mon compte':'Se connecter',()=>void submit())}{setRegister(!register);setError(null);}}>{register?'J’ai déjà un compte':'Créer un compte'}{if(!email.trim()){setError('Renseigne ton adresse e-mail.');return;}setBusy(true);void sendPasswordResetEmail(auth,email.trim()).then(()=>Alert.alert('Demande envoyée','Si un compte correspond à cette adresse, tu recevras un lien de réinitialisation.')).catch(()=>setError('Impossible d’envoyer la demande. Vérifie ton adresse et réessaie.')).finally(()=>setBusy(false));}}>Mot de passe oublié} {busy&&}{error&&{error}} ; }