import { useCallback, useEffect, useRef, useState } from 'react'; import { ActivityIndicator, Animated, Easing, Keyboard, 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; } /** Feuille « Rechercher un client » : glisse depuis le bas, clavier ouvert une fois en place. État local : taper ne re-rend pas la caméra. */ const SHEET_OFFSET = 520; function ManualPanel({ onClose, onOpenCard }: { onClose: () => void; onOpenCard: (serial: string) => void }) { const c = useColors(); const progress = useRef(new Animated.Value(0)).current; const inputRef = useRef(null); const closing = useRef(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-9\s-]*$/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 (!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); }; }, [isSearch, term]); useEffect(() => { Animated.timing(progress, { toValue: 1, duration: 240, easing: Easing.out(Easing.cubic), useNativeDriver: true }).start(({ finished }) => { if (finished) inputRef.current?.focus(); }); }, [progress]); const close = useCallback(() => { if (closing.current) return; closing.current = true; Keyboard.dismiss(); Animated.timing(progress, { toValue: 0, duration: 180, easing: Easing.in(Easing.cubic), useNativeDriver: true }).start(onClose); }, [progress, onClose]); /** Recherche d'un client : numéro de carte, prénom ou e-mail (si le scan échoue ou sans caméra). */ return ( Rechercher un client Prénom, e-mail, ou numéro inscrit sous le QR code de sa carte. typedOk && onOpenCard(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 &&