feat: convex mobile pagination and optimizations
This commit is contained in:
1 parent
cba10aa02b
commit
c511b9e537
4 files changed
+359
-94
No files matched your search
@@ -1,9 +1,11 @@
|
||||
import { getAuthUserId } from "@convex-dev/auth/server"
|
||||
import { ConvexError, v } from "convex/values"
|
||||
import { paginationOptsValidator } from "convex/server"
|
||||
import type { Doc, Id } from "./_generated/dataModel"
|
||||
import type { MutationCtx, QueryCtx } from "./_generated/server"
|
||||
import { mutation, query } from "./_generated/server"
|
||||
import type { BrowsePlaylistDetail, BrowseSong } from "./library"
|
||||
import type { BrowsePlaylistDetail, BrowsePlaylistSummary, BrowseSong } from "./library"
|
||||
import { paginateSlice } from "./library"
|
||||
|
||||
function uniqueIds<T extends string>(ids: T[]): T[] {
|
||||
return [...new Set(ids)]
|
||||
@@ -189,16 +191,44 @@ export const getUserFavorites = query({
|
||||
},
|
||||
})
|
||||
|
||||
async function browseFavoritesSummaryHandler(
|
||||
ctx: QueryCtx
|
||||
): Promise<BrowsePlaylistSummary | null> {
|
||||
const userId = await getAuthUserId(ctx)
|
||||
if (userId === null) {
|
||||
return null
|
||||
}
|
||||
|
||||
const user = await ctx.db.get("users", userId)
|
||||
if (!user) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
id: null,
|
||||
isFavorites: true,
|
||||
title: "Favorites",
|
||||
owner: {
|
||||
id: user._id,
|
||||
name: user.name,
|
||||
},
|
||||
cover: null,
|
||||
isOwner: true,
|
||||
isSubscribed: false,
|
||||
songIds: user.favoriteSongs ?? [],
|
||||
}
|
||||
}
|
||||
|
||||
export const browseFavoritesSummary = query({
|
||||
args: {},
|
||||
handler: async (ctx) => browseFavoritesSummaryHandler(ctx),
|
||||
})
|
||||
|
||||
export const browseFavoritesPlaylistDetail = query({
|
||||
args: {},
|
||||
handler: async (ctx): Promise<BrowsePlaylistDetail | null> => {
|
||||
const userId = await getAuthUserId(ctx)
|
||||
if (userId === null) {
|
||||
return null
|
||||
}
|
||||
|
||||
const user = await ctx.db.get("users", userId)
|
||||
if (!user) {
|
||||
const summary = await browseFavoritesSummaryHandler(ctx)
|
||||
if (!summary) {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -206,7 +236,7 @@ export const browseFavoritesPlaylistDetail = query({
|
||||
const albumMap = await loadAlbumMap(ctx)
|
||||
|
||||
const songs: BrowseSong[] = []
|
||||
for (const songId of user.favoriteSongs ?? []) {
|
||||
for (const songId of summary.songIds) {
|
||||
const song = await toBrowseSong(ctx, songId, artistMap, albumMap)
|
||||
if (song) {
|
||||
songs.push(song)
|
||||
@@ -214,17 +244,41 @@ export const browseFavoritesPlaylistDetail = query({
|
||||
}
|
||||
|
||||
return {
|
||||
id: null,
|
||||
isFavorites: true,
|
||||
title: "Favorites",
|
||||
owner: {
|
||||
id: user._id,
|
||||
name: user.name,
|
||||
},
|
||||
cover: null,
|
||||
id: summary.id,
|
||||
isFavorites: summary.isFavorites,
|
||||
title: summary.title,
|
||||
owner: summary.owner,
|
||||
cover: summary.cover,
|
||||
songs,
|
||||
isOwner: true,
|
||||
isSubscribed: false,
|
||||
isOwner: summary.isOwner,
|
||||
isSubscribed: summary.isSubscribed,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const browseFavoritesSongsPaginated = query({
|
||||
args: { paginationOpts: paginationOptsValidator },
|
||||
handler: async (ctx, args) => {
|
||||
const summary = await browseFavoritesSummaryHandler(ctx)
|
||||
if (!summary) {
|
||||
return { page: [], isDone: true, continueCursor: "" }
|
||||
}
|
||||
|
||||
const slice = paginateSlice(summary.songIds, args.paginationOpts)
|
||||
const artistMap = await loadArtistMap(ctx)
|
||||
const albumMap = await loadAlbumMap(ctx)
|
||||
|
||||
const page: BrowseSong[] = []
|
||||
for (const songId of slice.page) {
|
||||
const song = await toBrowseSong(ctx, songId, artistMap, albumMap)
|
||||
if (song) {
|
||||
page.push(song)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...slice,
|
||||
page,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
@@ -8,11 +8,13 @@ import type { PaginationOptions, PaginationResult } from "convex/server"
|
||||
import type {
|
||||
BrowseAlbum,
|
||||
BrowseAlbumDetail,
|
||||
BrowseAlbumSummary,
|
||||
BrowseAlbumTrack,
|
||||
BrowseArtist,
|
||||
BrowseArtistDetail,
|
||||
BrowsePlaylistDetail,
|
||||
BrowsePlaylistOwner,
|
||||
BrowsePlaylistSummary,
|
||||
BrowseSong,
|
||||
LibrarySearchResults,
|
||||
OwnedPlaylistForAdd,
|
||||
@@ -24,11 +26,13 @@ import type {
|
||||
export type {
|
||||
BrowseAlbum,
|
||||
BrowseAlbumDetail,
|
||||
BrowseAlbumSummary,
|
||||
BrowseAlbumTrack,
|
||||
BrowseArtist,
|
||||
BrowseArtistDetail,
|
||||
BrowsePlaylistDetail,
|
||||
BrowsePlaylistOwner,
|
||||
BrowsePlaylistSummary,
|
||||
BrowseSong,
|
||||
LibrarySearchResults,
|
||||
OwnedPlaylistForAdd,
|
||||
@@ -196,6 +200,60 @@ export const browseArtists = query({
|
||||
},
|
||||
})
|
||||
|
||||
export function paginateSlice<T>(
|
||||
items: T[],
|
||||
paginationOpts: PaginationOptions
|
||||
): PaginationResult<T> {
|
||||
const cursor = paginationOpts.cursor
|
||||
const start =
|
||||
cursor && cursor.length > 0 ? Number.parseInt(cursor, 10) : 0
|
||||
const safeStart = Number.isFinite(start) ? start : 0
|
||||
const page = items.slice(safeStart, safeStart + paginationOpts.numItems)
|
||||
const next = safeStart + page.length
|
||||
|
||||
return {
|
||||
page,
|
||||
isDone: next >= items.length,
|
||||
continueCursor: next >= items.length ? "" : String(next),
|
||||
}
|
||||
}
|
||||
|
||||
export const browseArtistsPaginated = query({
|
||||
args: { paginationOpts: paginationOptsValidator },
|
||||
handler: async (ctx, args) => {
|
||||
const result = await ctx.db
|
||||
.query("artists")
|
||||
.withIndex("by_name")
|
||||
.order("asc")
|
||||
.paginate(args.paginationOpts)
|
||||
|
||||
return {
|
||||
...result,
|
||||
page: result.page.map(toBrowseArtist),
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const browseAlbumsPaginated = query({
|
||||
args: { paginationOpts: paginationOptsValidator },
|
||||
handler: async (ctx, args) => {
|
||||
const artistMap = await loadArtistMap(ctx)
|
||||
const result = await ctx.db
|
||||
.query("albums")
|
||||
.withIndex("by_name")
|
||||
.order("asc")
|
||||
.paginate(args.paginationOpts)
|
||||
|
||||
return {
|
||||
...result,
|
||||
page: result.page.flatMap((album) => {
|
||||
const artist = artistMap.get(album.artist)
|
||||
return artist ? [toBrowseAlbum(album, artist)] : []
|
||||
}),
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const browseSongs = query({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
@@ -424,13 +482,14 @@ export const browseSongsPaginated = query({
|
||||
export const browseAlbumDetail = query({
|
||||
args: { id: v.id("albums") },
|
||||
handler: async (ctx, args): Promise<BrowseAlbumDetail | null> => {
|
||||
const album = await ctx.db.get("albums", args.id)
|
||||
if (!album) {
|
||||
const summary = await browseAlbumSummaryHandler(ctx, args.id)
|
||||
if (!summary) {
|
||||
return null
|
||||
}
|
||||
|
||||
const artist = await ctx.db.get("artists", album.artist)
|
||||
if (!artist) {
|
||||
const album = await ctx.db.get("albums", args.id)
|
||||
const artist = album ? await ctx.db.get("artists", album.artist) : null
|
||||
if (!album || !artist) {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -441,18 +500,77 @@ export const browseAlbumDetail = query({
|
||||
|
||||
songs.sort(compareAlbumSongs)
|
||||
|
||||
const primarySong = songs[0]
|
||||
|
||||
return {
|
||||
...toBrowseAlbum(album, artist),
|
||||
year: primarySong?.year ?? null,
|
||||
genre: primarySong?.genres[0] ?? null,
|
||||
releaseDate: album.date,
|
||||
...summary,
|
||||
tracks: songs.map((song) => toBrowseAlbumTrack(song, artist.name)),
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
async function browseAlbumSummaryHandler(
|
||||
ctx: QueryCtx,
|
||||
albumId: Id<"albums">
|
||||
): Promise<BrowseAlbumSummary | null> {
|
||||
const album = await ctx.db.get("albums", albumId)
|
||||
if (!album) {
|
||||
return null
|
||||
}
|
||||
|
||||
const artist = await ctx.db.get("artists", album.artist)
|
||||
if (!artist) {
|
||||
return null
|
||||
}
|
||||
|
||||
const songs = await ctx.db
|
||||
.query("songs")
|
||||
.withIndex("by_album", (q) => q.eq("album", albumId))
|
||||
.collect()
|
||||
|
||||
songs.sort(compareAlbumSongs)
|
||||
const primarySong = songs[0]
|
||||
|
||||
return {
|
||||
...toBrowseAlbum(album, artist),
|
||||
year: primarySong?.year ?? null,
|
||||
genre: primarySong?.genres[0] ?? null,
|
||||
releaseDate: album.date,
|
||||
trackCount: songs.length,
|
||||
}
|
||||
}
|
||||
|
||||
export const browseAlbumSummary = query({
|
||||
args: { id: v.id("albums") },
|
||||
handler: async (ctx, args) => browseAlbumSummaryHandler(ctx, args.id),
|
||||
})
|
||||
|
||||
export const browseAlbumTracksPaginated = query({
|
||||
args: {
|
||||
albumId: v.id("albums"),
|
||||
paginationOpts: paginationOptsValidator,
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const album = await ctx.db.get("albums", args.albumId)
|
||||
if (!album) {
|
||||
return { page: [], isDone: true, continueCursor: "" }
|
||||
}
|
||||
|
||||
const artist = await ctx.db.get("artists", album.artist)
|
||||
if (!artist) {
|
||||
return { page: [], isDone: true, continueCursor: "" }
|
||||
}
|
||||
|
||||
const songs = await ctx.db
|
||||
.query("songs")
|
||||
.withIndex("by_album", (q) => q.eq("album", args.albumId))
|
||||
.collect()
|
||||
|
||||
songs.sort(compareAlbumSongs)
|
||||
|
||||
const tracks = songs.map((song) => toBrowseAlbumTrack(song, artist.name))
|
||||
return paginateSlice(tracks, args.paginationOpts)
|
||||
},
|
||||
})
|
||||
|
||||
export const browseArtistDetail = query({
|
||||
args: { id: v.id("artists") },
|
||||
handler: async (ctx, args) => {
|
||||
@@ -629,50 +747,78 @@ export const browseRecentlyAdded = query({
|
||||
},
|
||||
})
|
||||
|
||||
export const browseRecentlyAddedPaginated = query({
|
||||
args: { paginationOpts: paginationOptsValidator },
|
||||
handler: async (ctx, args) => {
|
||||
const artistMap = await loadArtistMap(ctx)
|
||||
const albums = await ctx.db.query("albums").collect()
|
||||
|
||||
const recentlyAdded = albums
|
||||
.flatMap((album) => {
|
||||
const artist = artistMap.get(album.artist)
|
||||
return artist ? [{ album, artist, addedAt: album._creationTime }] : []
|
||||
})
|
||||
.sort((a, b) => b.addedAt - a.addedAt)
|
||||
.map(({ album, artist }) => toBrowseAlbum(album, artist))
|
||||
|
||||
return paginateSlice(recentlyAdded, args.paginationOpts)
|
||||
},
|
||||
})
|
||||
|
||||
async function browseRecentlyPlayedItems(ctx: QueryCtx) {
|
||||
const userId = await getAuthUserId(ctx)
|
||||
if (userId === null) {
|
||||
return []
|
||||
}
|
||||
|
||||
const user = await ctx.db.get(userId)
|
||||
if (!user) {
|
||||
return []
|
||||
}
|
||||
|
||||
const recentlyPlayed = user.recentlyPlayed ?? []
|
||||
if (recentlyPlayed.length === 0) {
|
||||
return []
|
||||
}
|
||||
|
||||
const artistMap = await loadArtistMap(ctx)
|
||||
const items: (BrowseAlbum | PlaylistSummary)[] = []
|
||||
|
||||
for (const id of recentlyPlayed) {
|
||||
const album = await ctx.db.get("albums", id as Id<"albums">)
|
||||
if (album) {
|
||||
const artist = artistMap.get(album.artist)
|
||||
if (artist) {
|
||||
items.push(toBrowseAlbum(album, artist))
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
const playlist = await ctx.db.get("playlists", id as Id<"playlists">)
|
||||
if (playlist) {
|
||||
const owner = await ctx.db.get(playlist.owner)
|
||||
items.push({
|
||||
id: playlist._id,
|
||||
title: playlist.name,
|
||||
owner: owner?.name ?? "Unknown",
|
||||
cover: playlist.cover,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
export const browseRecentlyPlayed = query({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
const userId = await getAuthUserId(ctx)
|
||||
if (userId === null) {
|
||||
return []
|
||||
}
|
||||
handler: async (ctx) => browseRecentlyPlayedItems(ctx),
|
||||
})
|
||||
|
||||
const user = await ctx.db.get(userId)
|
||||
if (!user) {
|
||||
return []
|
||||
}
|
||||
|
||||
const recentlyPlayed = user.recentlyPlayed ?? []
|
||||
if (recentlyPlayed.length === 0) {
|
||||
return []
|
||||
}
|
||||
|
||||
const artistMap = await loadArtistMap(ctx)
|
||||
const items: (BrowseAlbum | PlaylistSummary)[] = []
|
||||
|
||||
for (const id of recentlyPlayed) {
|
||||
const album = await ctx.db.get("albums", id as Id<"albums">)
|
||||
if (album) {
|
||||
const artist = artistMap.get(album.artist)
|
||||
if (artist) {
|
||||
items.push(toBrowseAlbum(album, artist))
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
const playlist = await ctx.db.get("playlists", id as Id<"playlists">)
|
||||
if (playlist) {
|
||||
const owner = await ctx.db.get(playlist.owner)
|
||||
items.push({
|
||||
id: playlist._id,
|
||||
title: playlist.name,
|
||||
owner: owner?.name ?? "Unknown",
|
||||
cover: playlist.cover,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return items
|
||||
export const browseRecentlyPlayedPaginated = query({
|
||||
args: { paginationOpts: paginationOptsValidator },
|
||||
handler: async (ctx, args) => {
|
||||
const items = await browseRecentlyPlayedItems(ctx)
|
||||
return paginateSlice(items, args.paginationOpts)
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { paginationOptsValidator } from "convex/server"
|
||||
import { getAuthUserId } from "@convex-dev/auth/server"
|
||||
import { ConvexError, v } from "convex/values"
|
||||
import type { Doc, Id } from "./_generated/dataModel"
|
||||
@@ -6,12 +7,13 @@ import { mutation, query } from "./_generated/server"
|
||||
import type {
|
||||
BrowseArtist,
|
||||
BrowsePlaylistDetail,
|
||||
BrowsePlaylistSummary,
|
||||
BrowseSong,
|
||||
OwnedPlaylistForAdd,
|
||||
PlaylistSummary,
|
||||
SidebarPlaylists,
|
||||
} from "./library"
|
||||
import { compareAlbumSongs } from "./library"
|
||||
import { compareAlbumSongs, paginateSlice } from "./library"
|
||||
|
||||
function toBrowseArtist(artist: Doc<"artists">): BrowseArtist {
|
||||
return {
|
||||
@@ -96,30 +98,58 @@ async function requirePlaylistOwner(
|
||||
return { userId, playlist }
|
||||
}
|
||||
|
||||
async function browsePlaylistSummaryHandler(
|
||||
ctx: QueryCtx,
|
||||
playlistId: Id<"playlists">
|
||||
): Promise<BrowsePlaylistSummary | null> {
|
||||
const playlist = await ctx.db.get("playlists", playlistId)
|
||||
if (!playlist) {
|
||||
return null
|
||||
}
|
||||
|
||||
const owner = await ctx.db.get("users", playlist.owner)
|
||||
if (!owner) {
|
||||
return null
|
||||
}
|
||||
|
||||
const userId = await getAuthUserId(ctx)
|
||||
const currentUser = userId ? await ctx.db.get("users", userId) : null
|
||||
const isOwner = userId === playlist.owner
|
||||
const isSubscribed =
|
||||
currentUser?.subscribedPlaylists?.includes(playlistId) ?? false
|
||||
|
||||
return {
|
||||
id: playlist._id,
|
||||
title: playlist.name,
|
||||
owner: {
|
||||
id: owner._id,
|
||||
name: owner.name,
|
||||
},
|
||||
cover: playlist.cover,
|
||||
isOwner,
|
||||
isSubscribed,
|
||||
songIds: playlist.songs,
|
||||
}
|
||||
}
|
||||
|
||||
export const browsePlaylistSummary = query({
|
||||
args: { id: v.id("playlists") },
|
||||
handler: async (ctx, args) => browsePlaylistSummaryHandler(ctx, args.id),
|
||||
})
|
||||
|
||||
export const browsePlaylistDetail = query({
|
||||
args: { id: v.id("playlists") },
|
||||
handler: async (ctx, args): Promise<BrowsePlaylistDetail | null> => {
|
||||
const playlist = await ctx.db.get("playlists", args.id)
|
||||
if (!playlist) {
|
||||
const summary = await browsePlaylistSummaryHandler(ctx, args.id)
|
||||
if (!summary) {
|
||||
return null
|
||||
}
|
||||
|
||||
const owner = await ctx.db.get("users", playlist.owner)
|
||||
if (!owner) {
|
||||
return null
|
||||
}
|
||||
|
||||
const userId = await getAuthUserId(ctx)
|
||||
const currentUser = userId ? await ctx.db.get("users", userId) : null
|
||||
const isOwner = userId === playlist.owner
|
||||
const isSubscribed =
|
||||
currentUser?.subscribedPlaylists?.includes(args.id) ?? false
|
||||
|
||||
const artistMap = await loadArtistMap(ctx)
|
||||
const albumMap = await loadAlbumMap(ctx)
|
||||
|
||||
const songs: BrowseSong[] = []
|
||||
for (const songId of playlist.songs) {
|
||||
for (const songId of summary.songIds) {
|
||||
const song = await toBrowseSong(ctx, songId, artistMap, albumMap)
|
||||
if (song) {
|
||||
songs.push(song)
|
||||
@@ -127,16 +157,43 @@ export const browsePlaylistDetail = query({
|
||||
}
|
||||
|
||||
return {
|
||||
id: playlist._id,
|
||||
title: playlist.name,
|
||||
owner: {
|
||||
id: owner._id,
|
||||
name: owner.name,
|
||||
},
|
||||
cover: playlist.cover,
|
||||
id: summary.id,
|
||||
title: summary.title,
|
||||
owner: summary.owner,
|
||||
cover: summary.cover,
|
||||
songs,
|
||||
isOwner,
|
||||
isSubscribed,
|
||||
isOwner: summary.isOwner,
|
||||
isSubscribed: summary.isSubscribed,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const browsePlaylistSongsPaginated = query({
|
||||
args: {
|
||||
id: v.id("playlists"),
|
||||
paginationOpts: paginationOptsValidator,
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const playlist = await ctx.db.get("playlists", args.id)
|
||||
if (!playlist) {
|
||||
return { page: [], isDone: true, continueCursor: "" }
|
||||
}
|
||||
|
||||
const slice = paginateSlice(playlist.songs, args.paginationOpts)
|
||||
const artistMap = await loadArtistMap(ctx)
|
||||
const albumMap = await loadAlbumMap(ctx)
|
||||
|
||||
const page: BrowseSong[] = []
|
||||
for (const songId of slice.page) {
|
||||
const song = await toBrowseSong(ctx, songId, artistMap, albumMap)
|
||||
if (song) {
|
||||
page.push(song)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...slice,
|
||||
page,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
@@ -39,6 +39,10 @@ export type BrowseAlbumDetail = BrowseAlbum & {
|
||||
tracks: BrowseAlbumTrack[]
|
||||
}
|
||||
|
||||
export type BrowseAlbumSummary = Omit<BrowseAlbumDetail, "tracks"> & {
|
||||
trackCount: number
|
||||
}
|
||||
|
||||
export type BrowseArtistDetail = {
|
||||
artist: BrowseArtist
|
||||
albums: BrowseAlbum[]
|
||||
@@ -73,6 +77,10 @@ export type BrowsePlaylistDetail = {
|
||||
isSubscribed: boolean
|
||||
}
|
||||
|
||||
export type BrowsePlaylistSummary = Omit<BrowsePlaylistDetail, "songs"> & {
|
||||
songIds: Id<"songs">[]
|
||||
}
|
||||
|
||||
export type SidebarPlaylists = {
|
||||
owned: PlaylistSummary[]
|
||||
subscribed: PlaylistSummary[]
|
||||
|
||||
Reference in new issue
Block a user