import {useNavigation} from '@react-navigation/native'; import React, {useContext, useEffect, useMemo, useRef, useState} from 'react'; import { Image, KeyboardAvoidingView, Platform, StatusBar, StyleSheet, Text, TouchableOpacity, TouchableWithoutFeedback, View, } from 'react-native'; import Animated, {EasingNode} from 'react-native-reanimated'; import {useDispatch, useSelector, useStore} from 'react-redux'; import {headerBarOptions} from '../../routes'; import {MomentContext} from '../../screens/profile/IndividualMoment'; import {deleteMomentTag, loadMomentTags} from '../../services'; import {loadUserMoments} from '../../store/actions'; import {RootState} from '../../store/rootReducer'; import { MomentCommentPreviewType, MomentPostType, MomentTagType, ScreenType, UserType, } from '../../types'; import { getLoggedInUserAsProfilePreview, getTimePosted, HeaderHeight, navigateToProfile, normalize, SCREEN_HEIGHT, SCREEN_WIDTH, } from '../../utils'; import {mentionPartTypes, renderTextWithMentions} from '../../utils/comments'; import {AddComment} from '../comments'; import CommentsCount from '../comments/CommentsCount'; import {MomentTags} from '../common'; import {MomentMoreInfoDrawer, TaggAvatar} from '../profile'; interface MomentPostProps { moment: MomentPostType; userXId: string | undefined; screenType: ScreenType; index: number; } const MomentPost: React.FC = ({ moment, userXId, screenType, index, }) => { const {userId: loggedInUserId, username: loggedInUsername} = useSelector( (state: RootState) => state.user.user, ); const {user} = useSelector((state: RootState) => userXId ? state.userX[screenType][userXId] : state.user, ); const [tags, setTags] = useState([]); const [momentTagId, setMomentTagId] = useState(''); const isOwnProfile = user.username === loggedInUsername; /* * Load tags on initial render to pass tags data to moment header and content */ useEffect(() => { loadMomentTags(moment.moment_id).then((response) => { setTags(response ? response : []); }); }, []); /* * Check if loggedInUser has been tagged in the picture and set the id */ useEffect(() => { const getMomentTagId = () => { const ownTag: MomentTagType[] = tags.filter( (tag) => tag.user.id === loggedInUserId, ); if (ownTag?.length > 0) { setMomentTagId(ownTag[0].id); } else { setMomentTagId(''); } }; getMomentTagId(); }, [tags]); /* * Remove tag and update the current tags */ const removeTag = async () => { const success = await deleteMomentTag(momentTagId); if (success) { const filteredTags = tags.filter((tag) => tag.user.id !== loggedInUserId); setTags(filteredTags); } }; // const [tags, setTags] = useState(momentTags); const state: RootState = useStore().getState(); const navigation = useNavigation(); const dispatch = useDispatch(); const imageRef = useRef(null); const [visible, setVisible] = useState(false); const [fadeValue, setFadeValue] = useState>( new Animated.Value(0), ); const [commentCount, setCommentCount] = useState( moment.comments_count, ); const [commentPreview, setCommentPreview] = useState(moment.comment_preview); const {keyboardVisible} = useContext(MomentContext); const [hideText, setHideText] = useState(false); const [isFullScreen, setIsFullScreen] = useState(false); const [aspectRatio, setAspectRatio] = useState(1); const [drawerVisible, setDrawerVisible] = useState(false); useEffect( () => navigation.setOptions({ ...headerBarOptions('white', ''), headerTitle: () => ( 18 ? normalize(14) : normalize(16), }, ]}> {moment.moment_category} ), }), [moment.moment_id], ); useEffect(() => { Image.getSize( moment.moment_url, (w, h) => { const isFS = Math.abs(w / h - 9 / 16) < 0.01; setAspectRatio(w / h); setIsFullScreen(isFS); }, (err) => console.log(err), ); }, []); useEffect(() => { const fade = async () => { Animated.timing(fadeValue, { toValue: 1, duration: 250, easing: EasingNode.linear, }).start(); }; fade(); }, [fadeValue]); useEffect(() => { if (!keyboardVisible && hideText) { setHideText(false); } }, [keyboardVisible, hideText]); const tagsIcon = useMemo(() => { tags.length > 0 && ( ); }, [tags]); const MomentPosterPreview = () => ( navigateToProfile(state, dispatch, navigation, screenType, user) } style={styles.header}> {user.username} ); const TagsIcon = () => { return tags.length > 0 ? ( { setVisible(!visible); setFadeValue(new Animated.Value(0)); }}> ) : ( ); }; const [verticalOffset, setVerticalOffset] = useState(0); return ( <> { dispatch(loadUserMoments(loggedInUserId)); navigation.goBack(); }} screenType={screenType} moment={moment} tags={tags} /> {visible && ( null} imageRef={imageRef} /> )} {!hideText && ( <> {moment.caption !== '' && renderTextWithMentions({ value: moment.caption, styles: styles.captionText, partTypes: mentionPartTypes('white'), onPress: (user: UserType) => navigateToProfile( state, dispatch, navigation, screenType, user, ), })} )} { setCommentPreview({ commenter: getLoggedInUserAsProfilePreview(state), comment: message, }); setCommentCount(commentCount + 1); }} onFocus={() => { setHideText(true); setVerticalOffset(SCREEN_HEIGHT * 0.05); }} isKeyboardAvoiding={false} theme={'dark'} /> {getTimePosted(moment.date_created)} ); }; const styles = StyleSheet.create({ postHeader: {}, postContent: { paddingBottom: normalize(20), }, image: { width: SCREEN_WIDTH, marginBottom: '3%', zIndex: 0, position: 'absolute', }, text: { marginHorizontal: '5%', color: 'white', fontWeight: '500', textAlign: 'right', marginTop: 5, }, captionText: { position: 'relative', marginHorizontal: '5%', color: '#ffffff', fontWeight: '500', fontSize: normalize(13), lineHeight: normalize(15.51), letterSpacing: normalize(0.6), marginBottom: normalize(5), width: SCREEN_WIDTH * 0.79, }, tagIcon: { width: normalize(30), height: normalize(30), bottom: normalize(20), left: '5%', }, avatar: { width: 48, aspectRatio: 1, borderRadius: 100, marginLeft: '3%', }, headerText: { fontSize: 15, fontWeight: 'bold', color: 'white', paddingHorizontal: '3%', }, header: { alignItems: 'center', flexDirection: 'row', marginBottom: normalize(15), alignSelf: 'flex-start', }, momentPosterContainer: { flexDirection: 'column', justifyContent: 'center', alignItems: 'center', }, }); export default MomentPost;