import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { View, Image, Text, TouchableOpacity } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import * as Progress from 'react-native-progress'; import { getImageUri, photoStyles, smallPhotoHeight, smallPhotoWidth } from '../../utils'; import { NAVIGATION_PAGES } from 'src/types'; import { styles } from '../../Components/styles'; import { API_HOST } from 'src/constants'; import ImageView from 'better-react-native-image-viewing'; import { EventPhotos } from '@api/events'; import { ImageSource } from 'expo-image'; import { Colors } from 'src/theme'; type Photo = { id: number; filetype: string; data: 0 | 1; preview: 0 | 1; uid: number; name: string; avatar: string | null; uri: string; isSending?: boolean; }; export const PhotoItem = React.memo( ({ photos, eventId, photosLeft }: { photos: any; eventId: number; photosLeft: number }) => { const navigation = useNavigation(); const [currentImageIndex, setCurrentImageIndex] = useState(0); const [isViewerVisible, setIsViewerVisible] = useState(false); const [fullImages, setFullImages] = useState([]); const openImageViewer = (index: number) => { setCurrentImageIndex(index); setIsViewerVisible(true); }; useEffect(() => { setFullImages( photos .filter((p: EventPhotos) => p.data) .map((photo: Photo) => ({ ...photo, uri: API_HOST + '/webapi/events/get-photo/' + eventId + '/' + photo.id })) ); }, [photos]); const renderSpinner = (style: any) => ( ); const renderPhotos = () => { const photosToShow = photos.filter((p: EventPhotos) => p.preview).slice(0, 3); const morePhotosCount = photos.length - 3 + photosLeft; if (photosToShow.length === 1) { return ( openImageViewer(0)}> {photosToShow[0].isSending ? ( renderSpinner(photoStyles.onePhoto) ) : ( )} ); } else if (photosToShow.length === 2) { return photosToShow.map((photo: Photo, index: number) => ( openImageViewer(index)}> {photo?.isSending ? ( renderSpinner({ ...photoStyles.twoPhotos, marginRight: index === 0 ? 8 : 0 }) ) : ( )} )); } else { return ( openImageViewer(0)}> {photosToShow[0].isSending ? ( renderSpinner(photoStyles.bigPhoto) ) : ( )} openImageViewer(1)}> {photosToShow[1].isSending ? ( renderSpinner(photoStyles.smallPhoto) ) : ( )} openImageViewer(2)}> {morePhotosCount > 0 && ( handlePress()} style={[ styles.morePhotosOverlay, { width: smallPhotoWidth, height: smallPhotoHeight } ]} > +{morePhotosCount} )} ); } }; const photoViews = useMemo(() => renderPhotos(), [photos]); const handlePress = useCallback(() => { navigation.navigate( ...([ NAVIGATION_PAGES.ALL_EVENT_PHOTOS, { data: photos, eventId, lastPhotoId: photos[photos.length - 1].id, photosCountLeft: photosLeft } ] as never) ); }, [navigation, photos]); return ( {photoViews} index.toString()} imageIndex={currentImageIndex} visible={isViewerVisible} onRequestClose={() => setIsViewerVisible(false)} swipeToCloseEnabled={false} backgroundColor={Colors.DARK_BLUE} FooterComponent={({ imageIndex }) => ( { setIsViewerVisible(false); navigation.navigate( ...([ NAVIGATION_PAGES.PUBLIC_PROFILE_VIEW, { userId: fullImages[imageIndex].uid } ] as never) ); }} disabled={!fullImages[imageIndex].uid} style={styles.imageOwner} > {fullImages[imageIndex].avatar ? ( ) : null} {fullImages[imageIndex].name} )} /> ); } );