import {ActivityIndicator, Animated, FlatList, StyleSheet} from "react-native"; import React, {useCallback, useState} from "react"; import {Card, Text, View} from "react-native-ui-lib"; import HeaderTemplate from "@/components/shared/HeaderTemplate"; import {Notification, useGetNotifications} from "@/hooks/firebase/useGetNotifications"; import {formatDistanceToNow} from "date-fns"; import {useRouter} from "expo-router"; import {useSetAtom} from "jotai"; import {modeAtom, selectedDateAtom} from "@/components/pages/calendar/atoms"; import {Swipeable} from 'react-native-gesture-handler'; import {useDeleteNotification} from "@/hooks/firebase/useDeleteNotification"; interface NotificationItemProps { item: Notification; onDelete: (id: string) => void; onPress: () => void; isDeleting: boolean; } const NotificationItem: React.FC = React.memo(({ item, onDelete, onPress, isDeleting }) => { const renderRightActions = useCallback(( progress: Animated.AnimatedInterpolation, dragX: Animated.AnimatedInterpolation ) => { const trans = dragX.interpolate({ inputRange: [-100, 0], outputRange: [0, 100], extrapolate: 'clamp' }); return ( Delete ); }, []); return ( onDelete(item.id)} overshootRight={false} enabled={!isDeleting} > {isDeleting && ( )} {item.content} {formatDistanceToNow(new Date(item.timestamp), {addSuffix: true})} {item.timestamp.toLocaleDateString()} ); }); const NotificationsPage: React.FC = () => { const setSelectedDate = useSetAtom(selectedDateAtom); const setMode = useSetAtom(modeAtom); const {data: notifications} = useGetNotifications(); const deleteNotification = useDeleteNotification(); const {push} = useRouter(); const [deletingIds, setDeletingIds] = useState>(new Set()); const goToEventDay = useCallback((notification: Notification) => () => { if (notification?.date) { setSelectedDate(notification.date); setMode("day") } push({pathname: "/calendar"}); }, [push, setSelectedDate]); const handleDelete = useCallback((notificationId: string) => { setDeletingIds(prev => new Set(prev).add(notificationId)); deleteNotification.mutate(notificationId, { onSettled: () => { setDeletingIds(prev => { const newSet = new Set(prev); newSet.delete(notificationId); return newSet; }); } }); }, [deleteNotification]); const renderNotificationItem = useCallback(({item}: { item: Notification }) => ( ), [handleDelete, goToEventDay, deletingIds]); return ( See your notifications here. item.id} /> ); }; const styles = StyleSheet.create({ listContainer: { paddingBottom: 10, paddingHorizontal: 25, }, card: { width: '100%', backgroundColor: 'white', }, subtitle: { fontFamily: "Manrope_400Regular", fontSize: 14, }, deleteAction: { backgroundColor: '#FF3B30', justifyContent: 'center', alignItems: 'flex-end', paddingRight: 30, marginBottom: 10, width: 100, borderRadius: 10, }, deleteActionText: { color: 'white', fontWeight: '600', }, loadingOverlay: { ...StyleSheet.absoluteFillObject, backgroundColor: 'rgba(255, 255, 255, 0.8)', justifyContent: 'center', alignItems: 'center', zIndex: 1, }, }); export default NotificationsPage