import React, {useMemo, useState} from 'react' import { LayoutChangeEvent, StyleProp, StyleSheet, View, ViewStyle, } from 'react-native' import {ImageStyle} from 'expo-image' import {Dimensions} from 'lib/media/types' import {AppBskyEmbedImages} from '@atproto/api' import {GalleryItem} from './Gallery' interface ImageLayoutGridProps { images: AppBskyEmbedImages.ViewImage[] onPress?: (index: number) => void onLongPress?: (index: number) => void onPressIn?: (index: number) => void style?: StyleProp } export function ImageLayoutGrid({style, ...props}: ImageLayoutGridProps) { const [containerInfo, setContainerInfo] = useState() const onLayout = (evt: LayoutChangeEvent) => { const {width, height} = evt.nativeEvent.layout setContainerInfo({ width, height, }) } return ( {containerInfo ? ( ) : undefined} ) } interface ImageLayoutGridInnerProps { images: AppBskyEmbedImages.ViewImage[] onPress?: (index: number) => void onLongPress?: (index: number) => void onPressIn?: (index: number) => void containerInfo: Dimensions } function ImageLayoutGridInner({ containerInfo, ...props }: ImageLayoutGridInnerProps) { const count = props.images.length const size1 = useMemo(() => { if (count === 3) { const size = (containerInfo.width - 10) / 3 return {width: size, height: size, resizeMode: 'cover', borderRadius: 4} } else { const size = (containerInfo.width - 5) / 2 return {width: size, height: size, resizeMode: 'cover', borderRadius: 4} } }, [count, containerInfo]) const size2 = React.useMemo(() => { if (count === 3) { const size = ((containerInfo.width - 10) / 3) * 2 + 5 return {width: size, height: size, resizeMode: 'cover', borderRadius: 4} } else { const size = (containerInfo.width - 5) / 2 return {width: size, height: size, resizeMode: 'cover', borderRadius: 4} } }, [count, containerInfo]) switch (count) { case 2: return ( ) case 3: return ( ) case 4: return ( ) default: return null } } const styles = StyleSheet.create({ flexRow: {flexDirection: 'row', gap: 5}, flexColumn: {flexDirection: 'column', gap: 5}, })