import React, { createContext, useContext, useState, ReactNode } from 'react'; import { Modal, View, Text, TouchableOpacity, TouchableWithoutFeedback } from 'react-native'; import { CheckCircle, XCircle, Info, AlertTriangle } from 'lucide-react-native'; type AlertType = 'success' | 'error' | 'info' | 'warning'; type ConfirmButtonStyle = 'default' | 'destructive' | 'cancel'; interface ConfirmButton { text: string; onPress: () => void; style?: ConfirmButtonStyle; } interface AlertContextData { showAlert: (type: AlertType, title: string, message: string) => void; showConfirm: ( title: string, message: string, buttons: [ConfirmButton, ConfirmButton] ) => void; hideAlert: () => void; } const AlertContext = createContext({} as AlertContextData); // TODO: Move this config to a separate file const ALERT_CONFIG = { success: { icon: CheckCircle, color: 'text-green-600', bgColor: 'bg-green-100', btnColor: 'bg-green-600', }, error: { icon: XCircle, color: 'text-red-600', bgColor: 'bg-red-100', btnColor: 'bg-red-600', }, info: { icon: Info, color: 'text-sky-600', bgColor: 'bg-sky-100', btnColor: 'bg-sky-600', }, warning: { icon: AlertTriangle, color: 'text-orange-600', bgColor: 'bg-orange-100', btnColor: 'bg-orange-600', }, }; export const AlertProvider = ({ children }: { children: ReactNode }) => { const [visible, setVisible] = useState(false); const [title, setTitle] = useState(''); const [message, setMessage] = useState(''); const [type, setType] = useState('info'); const [isConfirmMode, setIsConfirmMode] = useState(false); const [confirmButtons, setConfirmButtons] = useState<[ConfirmButton, ConfirmButton]>([ { text: 'Annulla', onPress: () => {}, style: 'cancel' }, { text: 'Conferma', onPress: () => {}, style: 'default' } ]); const showAlert = (newType: AlertType, newTitle: string, newMessage: string) => { setType(newType); setTitle(newTitle); setMessage(newMessage); setIsConfirmMode(false); setVisible(true); }; const showConfirm = ( newTitle: string, newMessage: string, buttons: [ConfirmButton, ConfirmButton] ) => { setTitle(newTitle); setMessage(newMessage); setConfirmButtons(buttons); setIsConfirmMode(true); setVisible(true); }; const hideAlert = () => { setVisible(false); }; const { icon: Icon, color, bgColor, btnColor } = ALERT_CONFIG[type]; // TODO: Need to refactor component styles return ( {children} {/* Dark Backdrop */} {/* Alert Container */} {/* Icon Circle - Solo per alert normali */} {!isConfirmMode && ( )} {/* Texts */} {title} {message} {/* Buttons - Condizionale */} {isConfirmMode ? ( // Conferma: 2 bottoni orizzontali {confirmButtons.map((button, index) => { const isDestructive = button.style === 'destructive'; const isCancel = button.style === 'cancel'; return ( { hideAlert(); button.onPress(); }} className={`flex-1 py-3.5 rounded-3xl ${ isDestructive ? 'bg-red-600' : isCancel ? 'bg-gray-200' : 'bg-[#099499]' } active:opacity-90 shadow-sm`} > {button.text} ); })} ) : ( // Alert normale: singolo bottone OK Ok, ho capito )} ); }; export const useAlert = () => useContext(AlertContext);