- Virtualize mobile lists and defer queue loading - Split playback state from actions to reduce rerenders - Skip invalid or unnecessary Convex queries Reviewed-on: #18
250 lines
7.1 KiB
TypeScript
250 lines
7.1 KiB
TypeScript
import { useMutation, useQuery } from "convex/react"
|
|
import {
|
|
createContext,
|
|
useCallback,
|
|
useContext,
|
|
useMemo,
|
|
type ReactNode,
|
|
} from "react"
|
|
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import { showError, showSuccess } from "@/lib/toast"
|
|
|
|
type FavoritesContextValue = {
|
|
isSongFavorite: (songId: string) => boolean
|
|
isAlbumFavorite: (albumId: string) => boolean
|
|
isArtistFavorite: (artistId: string) => boolean
|
|
toggleSongFavorite: (songId: Id<"songs">) => Promise<void>
|
|
toggleAlbumFavorite: (albumId: Id<"albums">) => Promise<void>
|
|
toggleArtistFavorite: (artistId: Id<"artists">) => Promise<void>
|
|
favoriteSongs: (songIds: Id<"songs">[]) => Promise<void>
|
|
}
|
|
|
|
const FavoritesContext = createContext<FavoritesContextValue | null>(null)
|
|
|
|
export function useFavorites() {
|
|
const context = useContext(FavoritesContext)
|
|
if (!context) {
|
|
throw new Error("useFavorites must be used within FavoritesProvider")
|
|
}
|
|
return context
|
|
}
|
|
|
|
export function FavoritesProvider({ children }: { children: ReactNode }) {
|
|
const favorites = useQuery(api.favorites.getUserFavorites)
|
|
const favoriteSong = useMutation(
|
|
api.favorites.favoriteSong
|
|
).withOptimisticUpdate((store, { songId }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (!current || current.favoriteSongs.includes(songId)) return
|
|
store.setQuery(
|
|
api.favorites.getUserFavorites,
|
|
{},
|
|
{
|
|
...current,
|
|
favoriteSongs: [...current.favoriteSongs, songId],
|
|
}
|
|
)
|
|
})
|
|
const unfavoriteSong = useMutation(
|
|
api.favorites.unfavoriteSong
|
|
).withOptimisticUpdate((store, { songId }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (!current) return
|
|
store.setQuery(
|
|
api.favorites.getUserFavorites,
|
|
{},
|
|
{
|
|
...current,
|
|
favoriteSongs: current.favoriteSongs.filter((id) => id !== songId),
|
|
}
|
|
)
|
|
})
|
|
const favoriteAlbum = useMutation(
|
|
api.favorites.favoriteAlbum
|
|
).withOptimisticUpdate((store, { albumId }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (!current || current.favoriteAlbums.includes(albumId)) return
|
|
store.setQuery(
|
|
api.favorites.getUserFavorites,
|
|
{},
|
|
{
|
|
...current,
|
|
favoriteAlbums: [...current.favoriteAlbums, albumId],
|
|
}
|
|
)
|
|
})
|
|
const unfavoriteAlbum = useMutation(
|
|
api.favorites.unfavoriteAlbum
|
|
).withOptimisticUpdate((store, { albumId }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (!current) return
|
|
store.setQuery(
|
|
api.favorites.getUserFavorites,
|
|
{},
|
|
{
|
|
...current,
|
|
favoriteAlbums: current.favoriteAlbums.filter((id) => id !== albumId),
|
|
}
|
|
)
|
|
})
|
|
const favoriteArtist = useMutation(
|
|
api.favorites.favoriteArtist
|
|
).withOptimisticUpdate((store, { artistId }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (!current || current.favoriteArtists.includes(artistId)) return
|
|
store.setQuery(
|
|
api.favorites.getUserFavorites,
|
|
{},
|
|
{
|
|
...current,
|
|
favoriteArtists: [...current.favoriteArtists, artistId],
|
|
}
|
|
)
|
|
})
|
|
const unfavoriteArtist = useMutation(
|
|
api.favorites.unfavoriteArtist
|
|
).withOptimisticUpdate((store, { artistId }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (!current) return
|
|
store.setQuery(
|
|
api.favorites.getUserFavorites,
|
|
{},
|
|
{
|
|
...current,
|
|
favoriteArtists: current.favoriteArtists.filter(
|
|
(id) => id !== artistId
|
|
),
|
|
}
|
|
)
|
|
})
|
|
const favoriteSongsMutation = useMutation(
|
|
api.favorites.favoriteSongs
|
|
).withOptimisticUpdate((store, { songIds }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (!current) return
|
|
store.setQuery(api.favorites.getUserFavorites, {}, {
|
|
...current,
|
|
favoriteSongs: [...new Set([...current.favoriteSongs, ...songIds])],
|
|
})
|
|
})
|
|
|
|
const songIds = useMemo(
|
|
() => new Set(favorites?.favoriteSongs.map(String) ?? []),
|
|
[favorites?.favoriteSongs]
|
|
)
|
|
const albumIds = useMemo(
|
|
() => new Set(favorites?.favoriteAlbums.map(String) ?? []),
|
|
[favorites?.favoriteAlbums]
|
|
)
|
|
const artistIds = useMemo(
|
|
() => new Set(favorites?.favoriteArtists.map(String) ?? []),
|
|
[favorites?.favoriteArtists]
|
|
)
|
|
|
|
const toggleSongFavorite = useCallback(
|
|
async (songId: Id<"songs">) => {
|
|
try {
|
|
if (songIds.has(songId)) {
|
|
await unfavoriteSong({ songId })
|
|
showSuccess("Removed from favorites")
|
|
} else {
|
|
await favoriteSong({ songId })
|
|
showSuccess("Added to favorites")
|
|
}
|
|
} catch (error) {
|
|
showError(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[songIds, favoriteSong, unfavoriteSong]
|
|
)
|
|
|
|
const toggleAlbumFavorite = useCallback(
|
|
async (albumId: Id<"albums">) => {
|
|
try {
|
|
if (albumIds.has(albumId)) {
|
|
await unfavoriteAlbum({ albumId })
|
|
showSuccess("Removed from favorites")
|
|
} else {
|
|
await favoriteAlbum({ albumId })
|
|
showSuccess("Added to favorites")
|
|
}
|
|
} catch (error) {
|
|
showError(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[albumIds, favoriteAlbum, unfavoriteAlbum]
|
|
)
|
|
|
|
const toggleArtistFavorite = useCallback(
|
|
async (artistId: Id<"artists">) => {
|
|
try {
|
|
if (artistIds.has(artistId)) {
|
|
await unfavoriteArtist({ artistId })
|
|
showSuccess("Removed from favorites")
|
|
} else {
|
|
await favoriteArtist({ artistId })
|
|
showSuccess("Added to favorites")
|
|
}
|
|
} catch (error) {
|
|
showError(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[artistIds, favoriteArtist, unfavoriteArtist]
|
|
)
|
|
|
|
const favoriteSongs = useCallback(
|
|
async (ids: Id<"songs">[]) => {
|
|
const unique = [...new Set(ids)]
|
|
if (unique.length === 0) return
|
|
try {
|
|
await favoriteSongsMutation({ songIds: unique })
|
|
showSuccess(
|
|
unique.length > 1
|
|
? `Favorited ${unique.length} songs`
|
|
: "Added to favorites"
|
|
)
|
|
} catch (error) {
|
|
showError(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[favoriteSongsMutation]
|
|
)
|
|
|
|
const value = useMemo(
|
|
() => ({
|
|
isSongFavorite: (songId: string) => songIds.has(songId),
|
|
isAlbumFavorite: (albumId: string) => albumIds.has(albumId),
|
|
isArtistFavorite: (artistId: string) => artistIds.has(artistId),
|
|
toggleSongFavorite,
|
|
toggleAlbumFavorite,
|
|
toggleArtistFavorite,
|
|
favoriteSongs,
|
|
}),
|
|
[
|
|
songIds,
|
|
albumIds,
|
|
artistIds,
|
|
toggleSongFavorite,
|
|
toggleAlbumFavorite,
|
|
toggleArtistFavorite,
|
|
favoriteSongs,
|
|
]
|
|
)
|
|
|
|
return (
|
|
<FavoritesContext.Provider value={value}>
|
|
{children}
|
|
</FavoritesContext.Provider>
|
|
)
|
|
}
|