import {useScrollToTop} from '@react-navigation/native'; import React, {useCallback, useEffect, useRef, useState} from 'react'; import {LayoutChangeEvent, RefreshControl, StyleSheet} from 'react-native'; import Animated, { useAnimatedScrollHandler, useSharedValue, } from 'react-native-reanimated'; import {useDispatch, useSelector, useStore} from 'react-redux'; import { blockUnblockUser, loadFriendsData, updateUserXFriends, } from '../../store/actions'; import { EMPTY_PROFILE_PREVIEW_LIST, NO_PROFILE, NO_USER, } from '../../store/initialStates'; import {RootState} from '../../store/rootreducer'; import {ScreenType} from '../../types'; import { canViewProfile, fetchUserX, getUserAsProfilePreviewType, userLogin, } from '../../utils'; import TaggsBar from '../taggs/TaggsBar'; import Cover from './Cover'; import PrivateProfile from './PrivateProfile'; import ProfileBody from './ProfileBody'; import ProfileCutout from './ProfileCutout'; import ProfileHeader from './ProfileHeader'; import PublicProfile from './PublicProfile'; interface ContentProps { userXId: string | undefined; screenType: ScreenType; } const Content: React.FC = ({userXId, screenType}) => { const dispatch = useDispatch(); const {user = NO_USER, profile = NO_PROFILE} = useSelector( (state: RootState) => userXId ? state.userX[screenType][userXId] : state.user, ); const {blockedUsers = EMPTY_PROFILE_PREVIEW_LIST} = useSelector( (state: RootState) => state.blocked, ); const {user: loggedInUser = NO_USER} = useSelector( (state: RootState) => state.user, ); const state: RootState = useStore().getState(); /* * Used to imperatively scroll to the top when presenting the moment tutorial. */ const scrollViewRef = useRef(null); /* * If scrolling is enabled. Set to false before scrolling up for the tutorial. */ const [scrollEnabled, setScrollEnabled] = useState(true); const y = useSharedValue(0); /** * States */ const [isBlocked, setIsBlocked] = useState(false); const [profileBodyHeight, setProfileBodyHeight] = useState(0); const [socialsBarHeight, setSocialsBarHeight] = useState(0); const [refreshing, setRefreshing] = useState(false); const onRefresh = useCallback(() => { const refrestState = async () => { setRefreshing(true); if (!userXId) { await userLogin(dispatch, loggedInUser); } else { await fetchUserX(dispatch, user, screenType); } }; refrestState().then(() => { setRefreshing(false); }); }, []); const onLayout = (e: LayoutChangeEvent) => { const {height} = e.nativeEvent.layout; setProfileBodyHeight(height); }; const onSocialsBarLayout = (e: LayoutChangeEvent) => { const {height} = e.nativeEvent.layout; setSocialsBarHeight(height); }; useEffect(() => { const isActuallyBlocked = blockedUsers.some( (cur_user) => user.username === cur_user.username, ); if (isBlocked !== isActuallyBlocked) { setIsBlocked(isActuallyBlocked); } }, [blockedUsers, user]); /** * Handles a click on the block / unblock button. * loadFriends updates friends list for the logged in user * updateUserXFriends updates friends list for the user. */ const handleBlockUnblock = async (callback?: () => void) => { dispatch( blockUnblockUser( loggedInUser, getUserAsProfilePreviewType(user, profile), isBlocked, ), ); dispatch(loadFriendsData(loggedInUser.userId)); dispatch(updateUserXFriends(user.userId, state)); if (callback) { callback(); } }; const scrollHandler = useAnimatedScrollHandler((event) => { y.value = event.contentOffset.y; }); useScrollToTop(scrollViewRef); return ( }> {canViewProfile(state, userXId, screenType) ? ( ) : ( )} ); }; const styles = StyleSheet.create({ container: { backgroundColor: '#fff', flex: 1, }, contentContainer: { flexGrow: 1, }, }); export default Content;