From e91955584369df277ab52405c12e20aad03e05fc Mon Sep 17 00:00:00 2001 From: Rem Helper Date: Thu, 9 Jul 2026 03:55:18 +0000 Subject: [PATCH] fix(mobile): paginate tracks in playlists --- .../app/(app)/(tabs)/home/playlist/[id].tsx | 137 ++++++++++-------- packages/convex/convex/playlists.ts | 18 ++- 2 files changed, 88 insertions(+), 67 deletions(-) diff --git a/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx b/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx index 6d9c0a7..dfec04d 100644 --- a/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx +++ b/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx @@ -1,6 +1,6 @@ -import { useMutation, useQuery } from "convex/react" +import { useMutation, usePaginatedQuery, useQuery } from "convex/react" import { Stack, useLocalSearchParams, useRouter } from "expo-router" -import { Pressable, ScrollView, StyleSheet, Text, View } from "react-native" +import { FlatList, Pressable, StyleSheet, Text, View } from "react-native" import { Ionicons } from "@expo/vector-icons" import { CoverImage } from "@/components/cover-image" @@ -18,11 +18,12 @@ import { useCoverBackgroundColor } from "@/hooks/use-cover-background-color" import { useMediaMenuActions } from "@/hooks/use-media-menu-actions" import { api } from "@otunes/convex/_generated/api" import type { Id } from "@otunes/convex/_generated/dataModel" -import type { BrowseSong } from "@otunes/convex/types" import { showError, showSuccess } from "@/lib/toast" import { playlistCoverUrl } from "@/lib/urls" import { radius, spacing, useThemeColors } from "@/lib/theme" +const PAGE_SIZE = 50 + export default function PlaylistScreen() { const { id } = useLocalSearchParams<{ id: string }>() const router = useRouter() @@ -30,6 +31,19 @@ export default function PlaylistScreen() { const { config } = useServerConfig() const playlist = useQuery(api.playlists.browsePlaylistDetail, { id: id as Id<"playlists">, + includeSongs: false, + }) + const { + results: songs, + status, + loadMore, + } = usePaginatedQuery( + api.playlists.browsePlaylistSongsPaginated, + { id: id as Id<"playlists"> }, + { initialNumItems: PAGE_SIZE } + ) + const allSongIds = useQuery(api.playlists.getPlaylistSongIds, { + playlistId: id as Id<"playlists">, }) const { playPlaylist, playPlaylistShuffled, playFromSong } = usePlayback() const { isSongFavorite } = useFavorites() @@ -41,7 +55,7 @@ export default function PlaylistScreen() { const coverUri = config && playlist ? playlistCoverUrl(config.serverUrl, playlist.id ?? "favorites") : "" const backgroundColor = useCoverBackgroundColor(coverUri, playlist?.cover != null, colors.background) - if (playlist === undefined || !config) { + if (playlist === undefined || songs === undefined || allSongIds === undefined || !config) { return ( <> song.id) + const songIds = allSongIds async function handleSubscribeToggle() { if (!playlist?.id) return @@ -146,68 +160,73 @@ export default function PlaylistScreen() { ), }} /> - song.id} style={{ flex: 1, backgroundColor }} contentContainerStyle={{ paddingBottom: contentBottomInset }} contentInsetAdjustmentBehavior="automatic" - > - - - {playlist.title} - {playlist.owner.name} - - {!playlist.isOwner && playlist.id ? ( + onEndReached={() => { + if (status === "CanLoadMore") loadMore(PAGE_SIZE) + }} + onEndReachedThreshold={0.5} + ListHeaderComponent={ + + + {playlist.title} + {playlist.owner.name} + + {!playlist.isOwner && playlist.id ? ( + void handleSubscribeToggle()} + style={[ + styles.iconAction, + { + backgroundColor: playlist.isSubscribed + ? colors.muted + : colors.primary, + }, + ]} + > + + + ) : null} +