140 lines
4.2 KiB
TypeScript
140 lines
4.2 KiB
TypeScript
import {
|
|
useEffect,
|
|
useCallback,
|
|
useRef,
|
|
useState,
|
|
type ReactElement,
|
|
} from "react"
|
|
import {
|
|
ScrollView,
|
|
View,
|
|
type NativeSyntheticEvent,
|
|
type NativeScrollEvent,
|
|
} from "react-native"
|
|
import Animated from "react-native-reanimated"
|
|
import { useSafeAreaInsets } from "react-native-safe-area-context"
|
|
|
|
import type { MediaItem } from "@/components/media-cards"
|
|
import { useMediaGridLayout } from "@/hooks/use-media-grid-layout"
|
|
import { tabletLayoutTransition } from "@/lib/tablet-motion"
|
|
import { useThemeColors } from "@/lib/theme"
|
|
|
|
type GridProps = {
|
|
content: MediaItem[]
|
|
paddingBottom: number
|
|
onEndReached?: () => void
|
|
ListHeaderComponent?: ReactElement
|
|
contentInset?: boolean
|
|
renderCard: (item: MediaItem, width: number) => ReactElement
|
|
}
|
|
|
|
/**
|
|
* Keep tiles keyed by media ID across column changes. Only the viewport plus
|
|
* two rows on either side is mounted, even when a large library is loaded.
|
|
*/
|
|
export function TabletMediaGrid({
|
|
content,
|
|
paddingBottom,
|
|
onEndReached,
|
|
ListHeaderComponent,
|
|
contentInset = true,
|
|
renderCard,
|
|
}: GridProps) {
|
|
const colors = useThemeColors()
|
|
const insets = useSafeAreaInsets()
|
|
const { columns, cardWidth, onLayout, horizontalPadding, gap, ready } =
|
|
useMediaGridLayout()
|
|
const [viewportHeight, setViewportHeight] = useState(900)
|
|
const [headerHeight, setHeaderHeight] = useState(0)
|
|
const [scrollY, setScrollY] = useState(0)
|
|
const lastRequestedLength = useRef(-1)
|
|
const rowHeight = cardWidth + 48 + gap
|
|
const firstRow = Math.max(
|
|
0,
|
|
Math.floor((scrollY - headerHeight) / rowHeight) - 2
|
|
)
|
|
const visibleRows = Math.ceil(viewportHeight / rowHeight) + 5
|
|
const start = firstRow * columns
|
|
const end = Math.min(content.length, (firstRow + visibleRows) * columns)
|
|
const rows = Math.ceil(content.length / columns)
|
|
const gridHeight = rows * rowHeight
|
|
|
|
const onScroll = useCallback(
|
|
(event: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
const { contentOffset, layoutMeasurement, contentSize } =
|
|
event.nativeEvent
|
|
const nextY = Math.max(0, contentOffset.y)
|
|
setScrollY((current) =>
|
|
Math.floor(current / rowHeight) === Math.floor(nextY / rowHeight)
|
|
? current
|
|
: nextY
|
|
)
|
|
if (
|
|
contentOffset.y + layoutMeasurement.height * 1.5 >=
|
|
contentSize.height &&
|
|
lastRequestedLength.current !== content.length
|
|
) {
|
|
lastRequestedLength.current = content.length
|
|
onEndReached?.()
|
|
}
|
|
},
|
|
[content.length, onEndReached, rowHeight]
|
|
)
|
|
useEffect(() => {
|
|
if (
|
|
content.length > 0 &&
|
|
gridHeight + headerHeight <= viewportHeight * 1.5 &&
|
|
lastRequestedLength.current !== content.length
|
|
) {
|
|
lastRequestedLength.current = content.length
|
|
onEndReached?.()
|
|
}
|
|
}, [content.length, gridHeight, headerHeight, viewportHeight, onEndReached])
|
|
|
|
return (
|
|
<Animated.View
|
|
style={{ flex: 1, backgroundColor: colors.background }}
|
|
onLayout={(event) => {
|
|
onLayout(event)
|
|
setViewportHeight(event.nativeEvent.layout.height)
|
|
}}
|
|
>
|
|
<ScrollView
|
|
onScroll={onScroll}
|
|
scrollEventThrottle={32}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
contentContainerStyle={{
|
|
paddingTop: contentInset ? horizontalPadding : 0,
|
|
paddingBottom: paddingBottom + insets.bottom,
|
|
}}
|
|
>
|
|
<View
|
|
onLayout={(event) => setHeaderHeight(event.nativeEvent.layout.height)}
|
|
>
|
|
{ListHeaderComponent}
|
|
</View>
|
|
<View style={{ height: gridHeight }}>
|
|
{(ready ? content.slice(start, end) : []).map((item, offset) => {
|
|
const index = start + offset
|
|
return (
|
|
<Animated.View
|
|
key={item.id}
|
|
layout={tabletLayoutTransition}
|
|
style={{
|
|
position: "absolute",
|
|
left:
|
|
horizontalPadding + (index % columns) * (cardWidth + gap),
|
|
top: Math.floor(index / columns) * rowHeight,
|
|
width: cardWidth,
|
|
}}
|
|
>
|
|
{renderCard(item, cardWidth)}
|
|
</Animated.View>
|
|
)
|
|
})}
|
|
</View>
|
|
</ScrollView>
|
|
</Animated.View>
|
|
)
|
|
}
|