|
| 1 | +import { Dimensions, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; |
| 2 | +import Animated, { Easing, SlideInDown, SlideOutDown } from 'react-native-reanimated'; |
| 3 | +import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context'; |
| 4 | +import { useInAppNotificationsState, useTheme } from 'stream-chat-react-native'; |
| 5 | +import type { Notification } from 'stream-chat'; |
| 6 | + |
| 7 | +const { width } = Dimensions.get('window'); |
| 8 | + |
| 9 | +const severityIconMap: Record<Notification['severity'], string> = { |
| 10 | + error: '❌', |
| 11 | + success: '✅', |
| 12 | + warning: '⚠️', |
| 13 | + info: 'ℹ️', |
| 14 | +}; |
| 15 | + |
| 16 | +export const Toast = () => { |
| 17 | + const { closeInAppNotification, notifications } = useInAppNotificationsState(); |
| 18 | + const { top } = useSafeAreaInsets(); |
| 19 | + const { |
| 20 | + theme: { |
| 21 | + colors: { overlay, white_smoke }, |
| 22 | + }, |
| 23 | + } = useTheme(); |
| 24 | + |
| 25 | + return ( |
| 26 | + <SafeAreaView style={[styles.container, { top }]} pointerEvents='box-none'> |
| 27 | + {notifications.map((notification) => ( |
| 28 | + <Animated.View |
| 29 | + key={notification.id} |
| 30 | + entering={SlideInDown.easing(Easing.bezierFn(0.25, 0.1, 0.25, 1.0))} |
| 31 | + exiting={SlideOutDown} |
| 32 | + style={[styles.toast, { backgroundColor: overlay }]} |
| 33 | + > |
| 34 | + <View style={[styles.icon, { backgroundColor: overlay }]}> |
| 35 | + <Text style={[styles.iconText, { color: white_smoke }]}> |
| 36 | + {severityIconMap[notification.severity]} |
| 37 | + </Text> |
| 38 | + </View> |
| 39 | + <View style={styles.content}> |
| 40 | + <Text style={[styles.message, { color: white_smoke }]}>{notification.message}</Text> |
| 41 | + </View> |
| 42 | + <TouchableOpacity onPress={() => closeInAppNotification(notification.id)}> |
| 43 | + <Text style={[styles.close, { color: white_smoke }]}>✕</Text> |
| 44 | + </TouchableOpacity> |
| 45 | + </Animated.View> |
| 46 | + ))} |
| 47 | + </SafeAreaView> |
| 48 | + ); |
| 49 | +}; |
| 50 | + |
| 51 | +const styles = StyleSheet.create({ |
| 52 | + container: { |
| 53 | + position: 'absolute', |
| 54 | + right: 16, |
| 55 | + left: 16, |
| 56 | + alignItems: 'flex-end', |
| 57 | + }, |
| 58 | + toast: { |
| 59 | + width: width * 0.9, |
| 60 | + borderRadius: 12, |
| 61 | + padding: 12, |
| 62 | + marginBottom: 8, |
| 63 | + flexDirection: 'row', |
| 64 | + justifyContent: 'space-between', |
| 65 | + alignItems: 'center', |
| 66 | + shadowColor: '#000', |
| 67 | + shadowOpacity: 0.2, |
| 68 | + shadowRadius: 4, |
| 69 | + elevation: 5, |
| 70 | + }, |
| 71 | + content: { |
| 72 | + flex: 1, |
| 73 | + marginHorizontal: 8, |
| 74 | + }, |
| 75 | + message: { |
| 76 | + fontSize: 14, |
| 77 | + fontWeight: '600', |
| 78 | + }, |
| 79 | + close: { |
| 80 | + fontSize: 16, |
| 81 | + }, |
| 82 | + icon: { |
| 83 | + width: 20, |
| 84 | + height: 20, |
| 85 | + borderRadius: 12, |
| 86 | + justifyContent: 'center', |
| 87 | + alignItems: 'center', |
| 88 | + }, |
| 89 | + iconText: { |
| 90 | + fontWeight: 'bold', |
| 91 | + includeFontPadding: false, |
| 92 | + }, |
| 93 | + warning: { |
| 94 | + backgroundColor: 'yellow', |
| 95 | + }, |
| 96 | +}); |
0 commit comments