import {useNavigation} from '@react-navigation/native'; import React, {useEffect, useState} from 'react'; import {Image, StyleSheet, Text, View} from 'react-native'; import {TouchableWithoutFeedback} from 'react-native-gesture-handler'; import {useDispatch, useSelector, useStore} from 'react-redux'; import {loadAvatar, loadMomentThumbnail} from '../../services'; import {RootState} from '../../store/rootReducer'; import {NotificationType, ScreenType} from '../../types'; import { fetchUserX, SCREEN_HEIGHT, SCREEN_WIDTH, userXInStore, } from '../../utils'; interface NotificationProps { item: NotificationType; screenType: ScreenType; } const Notification: React.FC = (props) => { const { item: { actor: {id, username, first_name, last_name}, verbage, notification_type, notification_object, unread, }, screenType, } = props; const navigation = useNavigation(); const state: RootState = useStore().getState(); const dispatch = useDispatch(); const {moments: loggedInUserMoments} = notification_type === 'CMT' ? useSelector((state: RootState) => state.moments) : {moments: undefined}; const [avatarURI, setAvatarURI] = useState(undefined); const [momentURI, setMomentURI] = useState(undefined); const backgroundColor = unread ? '#DCF1F1' : 'rgba(0,0,0,0)'; const contentRightMargin = notification_type === 'CMT' ? '15%' : 0; useEffect(() => { let mounted = true; const loadAvatarImage = async (user_id: string) => { const response = await loadAvatar(user_id, true); if (mounted) { setAvatarURI(response); } }; loadAvatarImage(id); return () => { mounted = false; }; }, [id]); useEffect(() => { let mounted = true; const loadMomentImage = async (moment_id: string) => { const response = await loadMomentThumbnail(moment_id); if (mounted && response) { setMomentURI(response); } }; if (notification_type === 'CMT' && notification_object) { loadMomentImage(notification_object.moment_id); return () => { mounted = false; }; } }, [id, notification_object, notification_type]); const onNotificationTap = async () => { switch (notification_type) { case 'FRD': if (!userXInStore(state, screenType, id)) { await fetchUserX( dispatch, {userId: id, username: username}, screenType, ); } navigation.push('Profile', { userXId: id, screenType, }); break; case 'CMT': // find the moment we need to display const moment = loggedInUserMoments?.find( (m) => m.moment_id === notification_object?.moment_id, ); if (moment) { navigation.push('IndividualMoment', { moment, userXId: undefined, // we're only viewing our own moment here screenType, }); setTimeout(() => { navigation.push('MomentCommentsScreen', { moment_id: moment.moment_id, screenType, }); }, 500); } break; default: break; } }; return ( {first_name} {last_name} {verbage} {notification_type === 'CMT' && notification_object && ( )} ); }; const styles = StyleSheet.create({ container: { flexDirection: 'row', height: SCREEN_HEIGHT / 10, flex: 1, alignItems: 'center', }, avatarContainer: { marginLeft: '8%', flex: 1, justifyContent: 'center', }, avatar: { height: 42, width: 42, borderRadius: 20, }, contentContainer: { flex: 5, marginLeft: '5%', height: '80%', flexDirection: 'column', justifyContent: 'space-around', marginRight: SCREEN_WIDTH / 6, }, actorName: { fontSize: 15, fontWeight: '700', }, moment: { position: 'absolute', height: 42, width: 42, right: '5%', }, }); export default Notification;