import { LinearGradient } from 'expo-linear-gradient'; import React, { useMemo, useState } from 'react'; import { StyleProp, StyleSheet, Text, TouchableOpacity, View, ViewStyle } from 'react-native'; import Animated, { FadeOut, Layout, LightSpeedInLeft } from 'react-native-reanimated'; import IoniconsIcon from '../../components/Icons/Ionicons'; import useTheme from '../hooks/useTheme'; import useTypograpghy from '../hooks/useTypography'; const messageTypes: any = { SUCCESS: { backgroundColor: '#09CE63', icon: 'checkmark' }, WARNING: { backgroundColor: '#FFD234', icon: 'alert' }, ERROR: { backgroundColor: '#FF3434', icon: 'close' }, INFO: { backgroundColor: '#67A7C1', icon: 'information' }, }; type NotificationContextType= { showNotification: (props: showNotificationProps) => void } type showNotificationProps = { header?:string, message?:string, type: keyof typeof messageTypes, } type NotificationProviderType= { children?:any, limit?:number, messageType?:any, containerStyle?:StyleProp linearMessageType?: (theme?:any, colorScheme?:'dark' | 'light') => {[key:string]: { backgroundColor:{ color:string [], location:number [], bottomColor:string [], bottomLocation:number [] } icon:React.ReactNode onPress:()=>void }} } const NotificationContext = React.createContext({} as NotificationContextType); const NotificationProvider = ({ children, limit = 3, messageType = messageTypes, linearMessageType, containerStyle }:NotificationProviderType) => { const [queue, setQueue] = useState([{ type: '', message: '', header: '' }]); const { theme, colorScheme } = useTheme(); const { typography } = useTypograpghy(); const showNotification = (item:showNotificationProps) => { pushQueue(item); }; const pushQueue = (item:any) => { setTimeout(() => { setQueue(prev => { limit <= prev.length ? popQueue(1) : popQueue(); if (prev.length > 0) popQueue(); return [{ ...item, keyID: Math.random() }, ...prev]; }); }, 100); }; const popQueue = (duration = 5000) => { setTimeout(() => { setQueue(prev => { const newQueue = [...prev]; if (newQueue.length) { newQueue.pop(); return newQueue; } return [...prev]; }); }, duration); }; const contextValue = useMemo(() => ({ showNotification }), []); function onPress(index:number) { const queueTemp = [...queue]; queueTemp.splice(index, 1); setQueue(queueTemp); } return ( // @ts-ignore {children} {queue.map((item:any, index) => ( onPress(index)}> {linearMessageType && item.type && ( <> )} { linearMessageType ? ( {linearMessageType(theme, colorScheme)[item?.type]?.icon} ) : ( )} {item?.header || item?.type } {item?.message} ))} ); }; export { NotificationProvider, NotificationContext }; const styles = StyleSheet.create({ itemContainer: { alignItems: 'center', width: '100%', paddingHorizontal: 24, zIndex: 100, height: 100, borderRadius: 10, }, linearConteiner: { alignItems: 'center', position: 'absolute', width: '100%', height: 100, borderRadius: 10, zIndex: 20, }, iconContainer: { zIndex: 100, marginTop: 21, marginLeft: 18, width: 28, height: 28, borderRadius: 100, alignItems: 'center', justifyContent: 'center', }, headerText: { fontWeight: '500', marginTop: 17, paddingHorizontal: 20, }, descText: { fontWeight: '400', marginTop: 6, paddingHorizontal: 20, }, });