import { useEffect, useRef, useState } from 'react'; import { ActivityIndicator, Animated, Easing, KeyboardAvoidingView, Platform, Pressable, ScrollView, StyleSheet, TextInput, View } from 'react-native'; import { CameraView, useCameraPermissions } from 'expo-camera'; import { router } from 'expo-router'; import * as Haptics from 'expo-haptics'; import { useKeepAwake } from 'expo-keep-awake'; import { playScanOk } from '../src/sounds'; import { SafeAreaView } from 'react-native-safe-area-context'; import { api } from '../src/api'; import { describe } from '../src/errors'; import { Button, Screen, T } from '../src/ui'; import { font, useColors } from '../src/theme'; import { Icon } from '../src/icons'; /** Les numéros de carte commencent par « MEP » (voir le back-end). */ const SERIAL = /^MEP(?:[A-Z2-9]{8}|[A-Z2-9]{16})$/; const FRAME = 260; const DIM = 'rgba(0,0,0,0.55)'; /** Saisie manuelle : espaces et tirets ignorés (« MEP ABCD EFGH… » est affiché sous le QR de la carte). */ const normalize = (v: string) => v.toUpperCase().replace(/[\s-]/g, ''); /** Résultat de la recherche d'un client (e-mail masqué par le serveur). */ interface Found { serial: string; firstName: string; email: string; balance: number; } export default function Scan() { const c = useColors(); const [permission, requestPermission] = useCameraPermissions(); const [torch, setTorch] = useState(false); const [invalid, setInvalid] = useState(false); const handled = useRef(false); // Mode comptoir : l'écran ne se met pas en veille pendant le scan. useKeepAwake(); const [manual, setManual] = useState(false); const [typed, setTyped] = useState(''); const [found, setFound] = useState(null); const [searching, setSearching] = useState(false); const [searchError, setSearchError] = useState(null); const typedOk = SERIAL.test(normalize(typed)); const term = typed.trim(); const isSearch = !typedOk && !/^MEP[A-Z0-9s-]*$/i.test(term) && term.length >= 2; // Recherche par prénom ou e-mail, au fil de la frappe (300 ms après la dernière touche). useEffect(() => { if (!manual || !isSearch) { setFound(null); setSearchError(null); return; } let cancelled = false; setSearching(true); const t = setTimeout(async () => { try { const list = await api.get(`/api/staff/cards?q=${encodeURIComponent(term)}`); if (!cancelled) { setFound(list); setSearchError(null); } } catch (e) { if (!cancelled) setSearchError(describe(e).detail); } finally { if (!cancelled) setSearching(false); } }, 300); return () => { cancelled = true; clearTimeout(t); }; }, [manual, isSearch, term]); function openCard(serial: string) { handled.current = true; Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success); playScanOk(); router.replace({ pathname: '/client/[serial]', params: { serial } }); } /** Recherche d'un client : numéro de carte, prénom ou e-mail (si le scan échoue ou sans caméra). */ const manualPanel = ( setManual(false)} accessibilityLabel="Fermer la recherche" /> Rechercher un client Prénom, e-mail, ou numéro inscrit sous le QR code de sa carte. typedOk && openCard(normalize(typed))} accessibilityLabel="Prénom, e-mail ou numéro de carte" style={{ borderWidth: 1, borderColor: c.border, borderRadius: 14, padding: 14, fontSize: 17, color: c.text, backgroundColor: c.bg, fontFamily: font.semibold }} /> {typedOk &&