import { useEffect, useRef, useState } from 'react';
import { Animated, Easing, KeyboardAvoidingView, Platform, Pressable, StyleSheet, TextInput, View } from 'react-native';
import { CameraView, useCameraPermissions } from 'expo-camera';
import { router } from 'expo-router';
import * as Haptics from 'expo-haptics';
import { SafeAreaView } from 'react-native-safe-area-context';
import { Button, Screen, T } from '../src/ui';
import { font, useColors } from '../src/theme';
/** Les numéros de carte commencent par « MEP » (voir le back-end). */
const SERIAL = /^MEP[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, '');
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);
const [manual, setManual] = useState(false);
const [typed, setTyped] = useState('');
const typedOk = SERIAL.test(normalize(typed));
function openCard(serial: string) {
handled.current = true;
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
router.replace({ pathname: '/client/[serial]', params: { serial } });
}
/** Panneau de saisie du numéro de carte (si le scan échoue ou sans caméra). */
const manualPanel = (
setManual(false)} accessibilityLabel="Fermer la saisie" />
Numéro de la carte
Inscrit sous le QR code de la carte du client : MEP suivi de 16 caractères.
typedOk && openCard(normalize(typed))}
accessibilityLabel="Numéro de la carte"
style={{ borderWidth: 1, borderColor: c.border, borderRadius: 14, padding: 14, fontSize: 18, letterSpacing: 1, color: c.text, backgroundColor: c.bg, fontFamily: font.semibold }}
/>
{typed.length > 0 && !typedOk && {normalize(typed).length} / 19 caractères}
);
const line = useRef(new Animated.Value(0)).current;
// Ligne de balayage : montre que la lecture est en cours.
useEffect(() => {
const anim = Animated.loop(
Animated.sequence([
Animated.timing(line, { toValue: 1, duration: 1600, easing: Easing.inOut(Easing.quad), useNativeDriver: true }),
Animated.timing(line, { toValue: 0, duration: 1600, easing: Easing.inOut(Easing.quad), useNativeDriver: true }),
]),
);
anim.start();
return () => anim.stop();
}, [line]);
// Le message « QR code inconnu » disparaît tout seul.
useEffect(() => {
if (!invalid) return;
const t = setTimeout(() => setInvalid(false), 2500);
return () => clearTimeout(t);
}, [invalid]);
if (!permission) return null;
if (!permission.granted) {
return (
📷
Accès à la caméra
La caméra sert uniquement à lire le QR code de la carte de fidélité du client.
{permission.canAskAgain ? (
) : (
Autorisez la caméra pour MonEspacePro dans les réglages du téléphone.
)}
setManual(true)} />
router.back()} />
{manual && manualPanel}
);
}
const corner = (pos: object) => ;
return (
{
if (handled.current) return;
const serial = data.trim().toUpperCase();
if (!SERIAL.test(serial)) {
if (!invalid) Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning);
setInvalid(true);
return;
}
openCard(serial);
}}
/>
{/* Voile sombre autour du cadre de visée */}
{corner({ top: 0, left: 0, borderTopWidth: 5, borderLeftWidth: 5, borderTopLeftRadius: 24 })}
{corner({ top: 0, right: 0, borderTopWidth: 5, borderRightWidth: 5, borderTopRightRadius: 24 })}
{corner({ bottom: 0, left: 0, borderBottomWidth: 5, borderLeftWidth: 5, borderBottomLeftRadius: 24 })}
{corner({ bottom: 0, right: 0, borderBottomWidth: 5, borderRightWidth: 5, borderBottomRightRadius: 24 })}
{/* Barre du haut : fermer + consigne */}
router.back()} style={styles.round} hitSlop={8}>
✕
Scannez la carte du client
QR code affiché dans son Wallet
{invalid && (
Ce QR code n'est pas une carte MonEspacePro
)}
setTorch((t) => !t)}
style={[styles.round, { width: 64, height: 64, borderRadius: 32 }, torch && { backgroundColor: '#F2B544' }]}
>
🔦
{torch ? 'Lampe allumée' : 'Lampe'}
setManual(true)} hitSlop={10} style={{ paddingVertical: 8, paddingHorizontal: 16, borderRadius: 999, backgroundColor: 'rgba(255,255,255,0.2)' }}>
Saisir le numéro
{manual && manualPanel}
);
}
const styles = StyleSheet.create({
corner: { position: 'absolute', width: 44, height: 44, borderColor: '#FFFFFF' },
round: { width: 44, height: 44, borderRadius: 22, backgroundColor: 'rgba(255,255,255,0.2)', alignItems: 'center', justifyContent: 'center' },
});