162 lines
5.0 KiB
TypeScript
162 lines
5.0 KiB
TypeScript
import {
|
|
useEffect,
|
|
useCallback,
|
|
useRef,
|
|
useState,
|
|
type ReactElement,
|
|
type ReactNode,
|
|
} from "react"
|
|
import {
|
|
type ScrollViewInstance,
|
|
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 { ScreenTitle, useScreenTitleScroll } from "@/components/screen-title"
|
|
import { useMediaGridLayout } from "@/hooks/use-media-grid-layout"
|
|
import { tabletLayoutTransition } from "@/lib/tablet-motion"
|
|
import { useThemeColors } from "@/lib/theme"
|
|
|
|
/** Height reserved under each tablet card's artwork for its title and subtitle. */
|
|
export const tabletCardTextHeight = 48
|
|
|
|
type GridProps = {
|
|
content: MediaItem[]
|
|
paddingBottom: number
|
|
onEndReached?: () => void
|
|
ListHeaderComponent?: ReactElement
|
|
contentInset?: boolean
|
|
/** iOS `ScreenTitle` shown above the grid; it replaces the top inset. */
|
|
title?: string
|
|
titleAccessory?: ReactNode
|
|
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,
|
|
title,
|
|
titleAccessory,
|
|
renderCard,
|
|
}: GridProps) {
|
|
const colors = useThemeColors()
|
|
const { ref, scrollY: titleScrollY } =
|
|
useScreenTitleScroll<ScrollViewInstance>()
|
|
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 + tabletCardTextHeight + 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)
|
|
}}
|
|
>
|
|
<Animated.ScrollView
|
|
ref={ref}
|
|
onScroll={onScroll}
|
|
// Every frame, so the title pins smoothly on pull-down.
|
|
scrollEventThrottle={16}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
contentContainerStyle={{
|
|
paddingTop: contentInset && !title ? horizontalPadding : 0,
|
|
paddingBottom: paddingBottom + insets.bottom,
|
|
}}
|
|
>
|
|
<View
|
|
onLayout={(event) => setHeaderHeight(event.nativeEvent.layout.height)}
|
|
>
|
|
{title ? (
|
|
<ScreenTitle
|
|
title={title}
|
|
scrollY={titleScrollY}
|
|
accessory={titleAccessory}
|
|
style={{ paddingHorizontal: horizontalPadding }}
|
|
/>
|
|
) : null}
|
|
{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>
|
|
</Animated.ScrollView>
|
|
</Animated.View>
|
|
)
|
|
}
|