Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237import {useRef} from 'react'import {type StyleProp, View, type ViewStyle} from 'react-native'import {type AnimatedRef, useAnimatedRef} from 'react-native-reanimated'import {type AppBskyEmbedImages} from '@atproto/api'
import {atoms as a, useBreakpoints} from '#/alf'import {type Dimensions} from '#/components/Lightbox/types'import {PostEmbedViewContext} from '#/components/Post/Embed/types'import {GalleryItem} from './ImageLayoutGridItem'
interface ImageLayoutGridProps { images: AppBskyEmbedImages.ViewImage[] onPress?: ( index: number, containerRefs: AnimatedRef<any>[], fetchedDims: (Dimensions | null)[], ) => void onLongPress?: (index: number) => void onPressIn?: (index: number) => void style?: StyleProp<ViewStyle> viewContext?: PostEmbedViewContext isWithinQuote?: boolean}
export function ImageLayoutGrid({ style, isWithinQuote: isWithinQuoteProp, ...props}: ImageLayoutGridProps) { const {gtMobile} = useBreakpoints() const isWithinQuote = isWithinQuoteProp ?? props.viewContext === PostEmbedViewContext.FeedEmbedRecordWithMedia const gap = isWithinQuote ? (gtMobile ? a.gap_xs : a.gap_2xs) : a.gap_xs
return ( <View style={style}> <View style={[gap, a.rounded_md, a.overflow_hidden]}> <ImageLayoutGridInner {...props} gap={gap} isWithinQuote={isWithinQuote} /> </View> </View> )}
interface ImageLayoutGridInnerProps { images: AppBskyEmbedImages.ViewImage[] onPress?: ( index: number, containerRefs: AnimatedRef<any>[], fetchedDims: (Dimensions | null)[], ) => void onLongPress?: (index: number) => void onPressIn?: (index: number) => void viewContext?: PostEmbedViewContext isWithinQuote?: boolean gap: {gap: number}}
function ImageLayoutGridInner(props: ImageLayoutGridInnerProps) { const gap = props.gap const count = props.images.length
const containerRef1 = useAnimatedRef() const containerRef2 = useAnimatedRef() const containerRef3 = useAnimatedRef() const containerRef4 = useAnimatedRef() const thumbDimsRef = useRef<(Dimensions | null)[]>([])
switch (count) { case 2: { const containerRefs = [containerRef1, containerRef2] return ( <View style={[a.flex_1, a.flex_row, gap]}> <View style={[a.flex_1, a.aspect_square]}> <GalleryItem {...props} index={0} insetBorderStyle={noCorners(['topRight', 'bottomRight'])} containerRefs={containerRefs} thumbDimsRef={thumbDimsRef} /> </View> <View style={[a.flex_1, a.aspect_square]}> <GalleryItem {...props} index={1} insetBorderStyle={noCorners(['topLeft', 'bottomLeft'])} containerRefs={containerRefs} thumbDimsRef={thumbDimsRef} /> </View> </View> ) }
case 3: { const containerRefs = [containerRef1, containerRef2, containerRef3] return ( <View style={[a.flex_1, a.flex_row, gap]}> <View style={[a.flex_1, a.aspect_square]}> <GalleryItem {...props} index={0} insetBorderStyle={noCorners(['topRight', 'bottomRight'])} containerRefs={containerRefs} thumbDimsRef={thumbDimsRef} /> </View> <View style={[a.flex_1, a.aspect_square, gap]}> <View style={[a.flex_1]}> <GalleryItem {...props} index={1} insetBorderStyle={noCorners([ 'topLeft', 'bottomLeft', 'bottomRight', ])} containerRefs={containerRefs} thumbDimsRef={thumbDimsRef} /> </View> <View style={[a.flex_1]}> <GalleryItem {...props} index={2} insetBorderStyle={noCorners([ 'topLeft', 'bottomLeft', 'topRight', ])} containerRefs={containerRefs} thumbDimsRef={thumbDimsRef} /> </View> </View> </View> ) }
case 4: { const containerRefs = [ containerRef1, containerRef2, containerRef3, containerRef4, ] return ( <> <View style={[a.flex_row, gap]}> <View style={[a.flex_1, {aspectRatio: 1.5}]}> <GalleryItem {...props} index={0} insetBorderStyle={noCorners([ 'bottomLeft', 'topRight', 'bottomRight', ])} containerRefs={containerRefs} thumbDimsRef={thumbDimsRef} /> </View> <View style={[a.flex_1, {aspectRatio: 1.5}]}> <GalleryItem {...props} index={1} insetBorderStyle={noCorners([ 'topLeft', 'bottomLeft', 'bottomRight', ])} containerRefs={containerRefs} thumbDimsRef={thumbDimsRef} /> </View> </View> <View style={[a.flex_row, gap]}> <View style={[a.flex_1, {aspectRatio: 1.5}]}> <GalleryItem {...props} index={2} insetBorderStyle={noCorners([ 'topLeft', 'topRight', 'bottomRight', ])} containerRefs={containerRefs} thumbDimsRef={thumbDimsRef} /> </View> <View style={[a.flex_1, {aspectRatio: 1.5}]}> <GalleryItem {...props} index={3} insetBorderStyle={noCorners([ 'topLeft', 'bottomLeft', 'topRight', ])} containerRefs={containerRefs} thumbDimsRef={thumbDimsRef} /> </View> </View> </> ) }
default: return null }}
function noCorners( corners: ('topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight')[],) { const styles: StyleProp<ViewStyle>[] = [] if (corners.includes('topLeft')) { styles.push({borderTopLeftRadius: 0}) } if (corners.includes('topRight')) { styles.push({borderTopRightRadius: 0}) } if (corners.includes('bottomLeft')) { styles.push({borderBottomLeftRadius: 0}) } if (corners.includes('bottomRight')) { styles.push({borderBottomRightRadius: 0}) } return styles}