import { CameraView, useCameraPermissions } from "expo-camera"; import { useEffect, useRef, useState } from "react"; import { ActivityIndicator, Modal, Pressable, StyleSheet, Text, View, } from "react-native"; interface ScanQRCodeProps { visible: boolean; onClose: () => void; onScanned: (data: string) => void; } export default function ScanQRCode({ visible, onClose, onScanned, }: ScanQRCodeProps) { const [permission, requestPermission] = useCameraPermissions(); const [scanned, setScanned] = useState(false); const cameraRef = useRef(null); // Request camera permission when component mounts or when visible changes to true useEffect(() => { if (visible && !permission?.granted) { requestPermission(); } }, [visible, permission, requestPermission]); // Reset scanned state when modal opens useEffect(() => { if (visible) { setScanned(false); } }, [visible]); const handleBarCodeScanned = ({ type, data, }: { type: string; data: string; }) => { if (!scanned) { setScanned(true); onScanned(data); onClose(); } }; if (!permission) { return ( Requesting camera permission... ); } if (!permission.granted) { return ( Camera Permission Required This app needs camera access to scan QR codes. Please allow camera access in your settings. requestPermission()} > Request Permission Cancel ); } return ( {/* Align QR code within the frame */} Đặt mã QR vào khung hình ✕ Đóng ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: "rgba(0, 0, 0, 0.8)", justifyContent: "center", alignItems: "center", }, loadingContainer: { alignItems: "center", gap: 10, }, loadingText: { color: "#fff", fontSize: 16, }, permissionContainer: { backgroundColor: "#fff", marginHorizontal: 20, borderRadius: 12, padding: 20, alignItems: "center", gap: 15, }, permissionTitle: { fontSize: 18, fontWeight: "600", color: "#333", }, permissionText: { fontSize: 14, color: "#666", textAlign: "center", lineHeight: 20, }, button: { backgroundColor: "#007AFF", paddingVertical: 12, paddingHorizontal: 30, borderRadius: 8, width: "100%", alignItems: "center", }, cancelButton: { backgroundColor: "#666", }, buttonText: { color: "#fff", fontSize: 16, fontWeight: "600", }, camera: { flex: 1, }, overlay: { flex: 1, backgroundColor: "rgba(0, 0, 0, 0.5)", }, unfocusedContainer: { flex: 1, }, focusedRow: { height: "80%", width: "100%", justifyContent: "center", alignItems: "center", }, focusedContainer: { aspectRatio: 1, width: "70%", borderColor: "#00ff00", borderWidth: 3, borderRadius: 10, }, bottomContainer: { flex: 1, justifyContent: "flex-end", alignItems: "center", paddingBottom: 40, gap: 20, }, scanningText: { color: "#fff", fontSize: 16, fontWeight: "500", }, closeButton: { backgroundColor: "rgba(0, 0, 0, 0.6)", paddingVertical: 10, paddingHorizontal: 20, borderRadius: 8, }, closeButtonText: { color: "#fff", fontSize: 14, fontWeight: "600", }, });