import React, { useState, useEffect, useCallback, useMemo } from "react"; import { View, Text, StyleSheet, FlatList, RefreshControl, Modal, TouchableOpacity, Animated, } from "react-native"; import { Ionicons } from "@expo/vector-icons"; import { spacing, fontSize, borderRadius } from "../../theme"; import { useTheme } from "../../context/ThemeContext"; import { getMyAlerts, triggerPoliceAlert, endAlert, } from "../../api/api_delivery"; import type { Alert as AlertType } from "../../api/types"; import LoadingSpinner from "../../components/ui/LoadingSpinner"; import Card from "../../components/ui/Card"; import Badge from "../../components/ui/Badge"; import Button from "../../components/ui/Button"; import AlertModal from "../../components/ui/AlertModal"; import { useAlert } from "../../hooks/useAlert"; export default function AlertsScreen() { const { colors } = useTheme(); const [alerts, setAlerts] = useState([]); const [loading, setLoading] = useState(true); const [refreshing, setRefreshing] = useState(false); const [triggering, setTriggering] = useState(false); const [showConfirmModal, setShowConfirmModal] = useState(false); const [showSuccessModal, setShowSuccessModal] = useState(false); const [successMessage, setSuccessMessage] = useState(""); const [pulseAnim] = useState(() => new Animated.Value(1)); const { alert: alertModal, showError, hideAlert } = useAlert(); const loadData = useCallback(async () => { try { const res = await getMyAlerts(); if (res.success && res.alerts) setAlerts(res.alerts); } catch { /* ignore */ } setLoading(false); }, []); useEffect(() => { loadData(); }, [loadData]); // Pulse animation when modal is open useEffect(() => { if (!showConfirmModal) return; const anim = Animated.loop( Animated.sequence([ Animated.timing(pulseAnim, { toValue: 1.15, duration: 800, useNativeDriver: true, }), Animated.timing(pulseAnim, { toValue: 1, duration: 800, useNativeDriver: true, }), ]), ); anim.start(); return () => anim.stop(); }, [showConfirmModal, pulseAnim]); const onRefresh = async () => { setRefreshing(true); await loadData(); setRefreshing(false); }; const handleConfirmTrigger = async () => { setShowConfirmModal(false); setTriggering(true); const res = await triggerPoliceAlert(); setTriggering(false); if (res.success) { setSuccessMessage(res.message || "Alerte déclenchée avec succès"); setShowSuccessModal(true); loadData(); } else { showError("Erreur", res.error || "Erreur"); } }; const handleEnd = async (alertId: number) => { const res = await endAlert(alertId); if (res.success) { loadData(); } else { showError("Erreur", res.error || "Erreur"); } }; const renderAlert = ({ item }: { item: AlertType }) => ( {new Date(item.created_at).toLocaleString("fr-FR")} {item.status === "true" && (