MonEspacePro/pro/app/scan.tsx
Tony AVERTY 39806791c4 App : page de scan retravaillée
Voile autour du cadre de visée, coins arrondis, ligne de balayage animée,
bouton fermer en haut, lampe en bouton rond, message d'erreur temporaire
avec vibration ; écran d'autorisation de la caméra plus clair.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 14:34:40 +02:00

150 lines
6.7 KiB
TypeScript

import { useEffect, useRef, useState } from 'react';
import { Animated, Easing, Pressable, StyleSheet, 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)';
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 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 (
<Screen noHeader style={{ justifyContent: 'center', alignItems: 'stretch' }}>
<View style={{ alignItems: 'center', gap: 14 }}>
<View style={{ width: 96, height: 96, borderRadius: 48, backgroundColor: c.primarySoft, alignItems: 'center', justifyContent: 'center' }}>
<T style={{ fontSize: 44 }}>📷</T>
</View>
<T v="h1" style={{ textAlign: 'center' }}>Accès à la caméra</T>
<T v="muted" style={{ textAlign: 'center' }}>La caméra sert uniquement à lire le QR code de la carte de fidélité du client.</T>
</View>
{permission.canAskAgain ? (
<Button large label="Autoriser la caméra" onPress={requestPermission} />
) : (
<T style={{ textAlign: 'center' }}>Autorisez la caméra pour MonEspacePro dans les réglages du téléphone.</T>
)}
<Button label="Retour" variant="secondary" onPress={() => router.back()} />
</Screen>
);
}
const corner = (pos: object) => <View style={[styles.corner, pos]} />;
return (
<View style={{ flex: 1, backgroundColor: '#000' }}>
<CameraView
style={StyleSheet.absoluteFill}
facing="back"
enableTorch={torch}
barcodeScannerSettings={{ barcodeTypes: ['qr'] }}
onBarcodeScanned={({ data }) => {
if (handled.current) return;
const serial = data.trim().toUpperCase();
if (!SERIAL.test(serial)) {
if (!invalid) Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning);
setInvalid(true);
return;
}
handled.current = true;
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
router.replace({ pathname: '/client/[serial]', params: { serial } });
}}
/>
{/* Voile sombre autour du cadre de visée */}
<View style={StyleSheet.absoluteFill} pointerEvents="none">
<View style={{ flex: 1, backgroundColor: DIM }} />
<View style={{ flexDirection: 'row', height: FRAME }}>
<View style={{ flex: 1, backgroundColor: DIM }} />
<View style={{ width: FRAME, height: FRAME }}>
{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 })}
<Animated.View
style={{
position: 'absolute',
left: 18,
right: 18,
height: 3,
borderRadius: 2,
backgroundColor: invalid ? '#F08A80' : '#F2B544',
transform: [{ translateY: line.interpolate({ inputRange: [0, 1], outputRange: [18, FRAME - 21] }) }],
}}
/>
</View>
<View style={{ flex: 1, backgroundColor: DIM }} />
</View>
<View style={{ flex: 1, backgroundColor: DIM }} />
</View>
<SafeAreaView style={{ flex: 1, justifyContent: 'space-between', padding: 20 }}>
{/* Barre du haut : fermer + consigne */}
<View style={{ gap: 18 }}>
<Pressable accessibilityRole="button" accessibilityLabel="Fermer" onPress={() => router.back()} style={styles.round} hitSlop={8}>
<T style={{ color: '#fff', fontSize: 22, lineHeight: 24 }}>✕</T>
</Pressable>
<View style={{ alignItems: 'center', gap: 4 }}>
<T style={{ color: '#fff', fontFamily: font.bold, fontSize: 22, textAlign: 'center' }}>Scannez la carte du client</T>
<T style={{ color: 'rgba(255,255,255,0.8)', fontSize: 15, textAlign: 'center' }}>QR code affiché dans son Wallet</T>
</View>
</View>
<View style={{ alignItems: 'center', gap: 16 }}>
{invalid && (
<View style={{ backgroundColor: 'rgba(184,50,42,0.92)', paddingVertical: 10, paddingHorizontal: 16, borderRadius: 999 }}>
<T style={{ color: '#fff', fontFamily: font.semibold, fontSize: 15 }}>Ce QR code n'est pas une carte MonEspacePro</T>
</View>
)}
<Pressable
accessibilityRole="button"
accessibilityLabel={torch ? 'Éteindre la lampe' : 'Allumer la lampe'}
accessibilityState={{ selected: torch }}
onPress={() => setTorch((t) => !t)}
style={[styles.round, { width: 64, height: 64, borderRadius: 32 }, torch && { backgroundColor: '#F2B544' }]}
>
<T style={{ fontSize: 26 }}>🔦</T>
</Pressable>
<T style={{ color: 'rgba(255,255,255,0.8)', fontSize: 13 }}>{torch ? 'Lampe allumée' : 'Lampe'}</T>
</View>
</SafeAreaView>
</View>
);
}
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' },
});