fix: pagination for recently added on library (#6)

Co-authored-by: Rem Helper <remhelper@abunchofknowitalls.com>
Reviewed-on: https://git.abunchofknowitalls.com/joshuafhiggins/otunes/pulls/6
This commit is contained in:
joshuafhigginsandremhelper committed 2026-08-02 04:14:02 +00:00
1 parent 0644df4b8a
commit 103b6fda79
3 files changed
+69 -36

No files matched your search

@@ -5,7 +5,7 @@ import { LoadingView } from "@/components/ui/primitives"
import { contentBottomInset } from "@/components/player"
import { api } from "@otunes/convex/_generated/api"
const PAGE_SIZE = 50
const PAGE_SIZE = 16
export default function RecentlyAddedScreen() {
const { results, status, loadMore } = usePaginatedQuery(
+48 -32
View File
@@ -1,17 +1,18 @@
import { usePaginatedQuery } from "convex/react"
import { Pressable, ScrollView, StyleSheet, Text, View } from "react-native"
import { useMemo } from "react"
import { Pressable, StyleSheet, Text, View } from "react-native"
import { SymbolView } from "expo-symbols"
import { Platform } from "react-native"
import { Ionicons } from "@expo/vector-icons"
import { MediaGrid } from "@/components/media-cards"
import { MediaGridList } from "@/components/media-cards"
import { tabContentBottomInset } from "@/components/player"
import { LoadingView } from "@/components/ui/primitives"
import { useAppNavigation } from "@/lib/app-navigation"
import { api } from "@otunes/convex/_generated/api"
import { appleMusic, spacing, useThemeColors } from "@/lib/theme"
const RECENTLY_ADDED_PREVIEW_SIZE = 4
const PAGE_SIZE = 16
const links = [
{ title: "Playlists", href: "/browse/playlists", icon: "music.note.list" as const },
@@ -48,43 +49,59 @@ function LibraryIcon({ name }: { name: (typeof links)[number]["icon"] }) {
export default function LibraryScreen() {
const { push } = useAppNavigation()
const colors = useThemeColors()
const { results: recentlyAdded } = usePaginatedQuery(
const { results, status, loadMore } = usePaginatedQuery(
api.library.browseRecentlyAddedPaginated,
{},
{ initialNumItems: RECENTLY_ADDED_PREVIEW_SIZE }
{ initialNumItems: PAGE_SIZE }
)
return (
<ScrollView
style={{ flex: 1, backgroundColor: colors.background }}
contentContainerStyle={{ paddingBottom: tabContentBottomInset }}
contentInsetAdjustmentBehavior="automatic"
>
{links.map((link) => (
<Pressable
key={link.href}
style={[styles.row, { borderBottomColor: colors.border }]}
onPress={() => push(link.href)}
>
<LibraryIcon name={link.icon} />
<Text style={[styles.rowTitle, { color: colors.foreground }]}>{link.title}</Text>
<Ionicons name="chevron-forward" size={16} color={colors.mutedForeground} />
</Pressable>
))}
const listHeader = useMemo(
() => (
<View>
{links.map((link) => (
<Pressable
key={link.href}
style={[styles.row, { borderBottomColor: colors.border }]}
onPress={() => push(link.href)}
>
<LibraryIcon name={link.icon} />
<Text style={[styles.rowTitle, { color: colors.foreground }]}>{link.title}</Text>
<Ionicons name="chevron-forward" size={16} color={colors.mutedForeground} />
</Pressable>
))}
<View style={styles.section}>
<Pressable onPress={() => push("/recently-added")}>
<Pressable
style={[styles.section, { paddingHorizontal: spacing.lg }]}
onPress={() => push("/recently-added")}
>
<Text style={[styles.sectionTitle, { color: colors.foreground }]}>
Recently Added
</Text>
</Pressable>
{recentlyAdded === undefined ? (
<LoadingView />
) : (
<MediaGrid content={recentlyAdded} />
)}
</View>
</ScrollView>
),
[colors.border, colors.foreground, colors.mutedForeground, push]
)
if (results === undefined) {
return (
<View style={{ flex: 1, backgroundColor: colors.background }}>
{listHeader}
<LoadingView />
</View>
)
}
return (
<MediaGridList
content={results}
paddingBottom={tabContentBottomInset}
contentInset={false}
ListHeaderComponent={listHeader}
onEndReached={() => {
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
}}
/>
)
}
@@ -104,8 +121,7 @@ const styles = StyleSheet.create({
},
section: {
paddingTop: spacing.xl,
paddingHorizontal: spacing.lg,
gap: spacing.md,
paddingBottom: spacing.md,
},
sectionTitle: {
fontSize: 22,
+20 -3
View File
@@ -1,5 +1,5 @@
import { Ionicons } from "@expo/vector-icons"
import { memo, useCallback } from "react"
import { memo, useCallback, type ReactElement } from "react"
import { FlatList, Pressable, ScrollView, StyleSheet, Text, View } from "react-native"
import { CoverImage } from "@/components/cover-image"
@@ -179,10 +179,14 @@ export function MediaGridList({
content,
paddingBottom,
onEndReached,
ListHeaderComponent,
contentInset = true,
}: {
content: MediaItem[]
paddingBottom: number
onEndReached?: () => void
ListHeaderComponent?: ReactElement | null
contentInset?: boolean
}) {
const cardWidth = useTwoColumnCardWidth()
const colors = useThemeColors()
@@ -193,9 +197,16 @@ export function MediaGridList({
keyExtractor={(item) => item.id}
numColumns={2}
style={{ flex: 1, backgroundColor: colors.background }}
contentContainerStyle={[styles.gridListContent, { paddingBottom }]}
columnWrapperStyle={styles.gridListRow}
contentContainerStyle={[
contentInset ? styles.gridListContent : styles.gridListContentInset,
{ paddingBottom },
]}
columnWrapperStyle={[
styles.gridListRow,
!contentInset && styles.gridListRowInset,
]}
contentInsetAdjustmentBehavior="automatic"
ListHeaderComponent={ListHeaderComponent}
onEndReached={onEndReached}
onEndReachedThreshold={0.5}
renderItem={({ item }) => (
@@ -286,7 +297,13 @@ const styles = StyleSheet.create({
padding: spacing.lg,
gap: spacing.md,
},
gridListContentInset: {
gap: spacing.md,
},
gridListRow: {
gap: spacing.md,
},
gridListRowInset: {
paddingHorizontal: spacing.lg,
},
})