255 lines
7.4 KiB
TypeScript
255 lines
7.4 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import type { OptimisticUpdate } from "convex/browser"
|
|
import { useMutation, type ReactMutation } from "convex/react"
|
|
import type { FunctionArgs, FunctionReference } from "convex/server"
|
|
import { toast } from "sonner"
|
|
|
|
import { useCachedQuery } from "@/hooks/use-cached-query"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import { favoriteOptimisticUpdates } from "@otunes/convex/client/favorites"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
|
|
/**
|
|
* `useMutation(...).withOptimisticUpdate(...)` with a stable identity. Pass a
|
|
* module-level update so it does not change between renders.
|
|
*/
|
|
function useOptimisticMutation<Mutation extends FunctionReference<"mutation">>(
|
|
mutation: Mutation,
|
|
optimisticUpdate: OptimisticUpdate<FunctionArgs<Mutation>>
|
|
): ReactMutation<Mutation> {
|
|
const base = useMutation(mutation)
|
|
return React.useMemo(
|
|
() => base.withOptimisticUpdate(optimisticUpdate),
|
|
[base, optimisticUpdate]
|
|
)
|
|
}
|
|
|
|
type FavoritesContextValue = {
|
|
isSongFavorite: (songId: string) => boolean
|
|
isAlbumFavorite: (albumId: string) => boolean
|
|
isArtistFavorite: (artistId: string) => boolean
|
|
toggleSongFavorite: (songId: Id<"songs">) => Promise<void>
|
|
toggleSongsFavorite: (
|
|
songIds: Id<"songs">[],
|
|
favorited: boolean
|
|
) => Promise<boolean>
|
|
toggleAlbumFavorite: (albumId: Id<"albums">) => Promise<void>
|
|
toggleArtistFavorite: (artistId: Id<"artists">) => Promise<void>
|
|
favoriteSongs: (songIds: Id<"songs">[]) => Promise<void>
|
|
}
|
|
|
|
const FavoritesContext = React.createContext<FavoritesContextValue | null>(null)
|
|
|
|
export function useFavorites() {
|
|
const context = React.useContext(FavoritesContext)
|
|
if (!context) {
|
|
throw new Error("useFavorites must be used within FavoritesProvider")
|
|
}
|
|
return context
|
|
}
|
|
|
|
export function FavoritesProvider({ children }: { children: React.ReactNode }) {
|
|
const favorites = useCachedQuery(api.favorites.getUserFavorites)
|
|
const favoriteSong = useOptimisticMutation(
|
|
api.favorites.favoriteSong,
|
|
favoriteOptimisticUpdates.favoriteSong
|
|
)
|
|
const favoriteSongsMutation = useOptimisticMutation(
|
|
api.favorites.favoriteSongs,
|
|
favoriteOptimisticUpdates.favoriteSongs
|
|
)
|
|
const unfavoriteSong = useOptimisticMutation(
|
|
api.favorites.unfavoriteSong,
|
|
favoriteOptimisticUpdates.unfavoriteSong
|
|
)
|
|
const unfavoriteSongs = useOptimisticMutation(
|
|
api.favorites.unfavoriteSongs,
|
|
favoriteOptimisticUpdates.unfavoriteSongs
|
|
)
|
|
const favoriteAlbum = useOptimisticMutation(
|
|
api.favorites.favoriteAlbum,
|
|
favoriteOptimisticUpdates.favoriteAlbum
|
|
)
|
|
const unfavoriteAlbum = useOptimisticMutation(
|
|
api.favorites.unfavoriteAlbum,
|
|
favoriteOptimisticUpdates.unfavoriteAlbum
|
|
)
|
|
const favoriteArtist = useOptimisticMutation(
|
|
api.favorites.favoriteArtist,
|
|
favoriteOptimisticUpdates.favoriteArtist
|
|
)
|
|
const unfavoriteArtist = useOptimisticMutation(
|
|
api.favorites.unfavoriteArtist,
|
|
favoriteOptimisticUpdates.unfavoriteArtist
|
|
)
|
|
|
|
const songIds = React.useMemo(
|
|
() => new Set(favorites?.favoriteSongs.map(String) ?? []),
|
|
[favorites?.favoriteSongs]
|
|
)
|
|
const albumIds = React.useMemo(
|
|
() => new Set(favorites?.favoriteAlbums.map(String) ?? []),
|
|
[favorites?.favoriteAlbums]
|
|
)
|
|
const artistIds = React.useMemo(
|
|
() => new Set(favorites?.favoriteArtists.map(String) ?? []),
|
|
[favorites?.favoriteArtists]
|
|
)
|
|
|
|
const isSongFavorite = React.useCallback(
|
|
(songId: string) => songIds.has(songId),
|
|
[songIds]
|
|
)
|
|
const isAlbumFavorite = React.useCallback(
|
|
(albumId: string) => albumIds.has(albumId),
|
|
[albumIds]
|
|
)
|
|
const isArtistFavorite = React.useCallback(
|
|
(artistId: string) => artistIds.has(artistId),
|
|
[artistIds]
|
|
)
|
|
|
|
const toggleSongFavorite = React.useCallback(
|
|
async (songId: Id<"songs">) => {
|
|
try {
|
|
if (songIds.has(songId)) {
|
|
await unfavoriteSong({ songId })
|
|
toast.success("Removed from favorites")
|
|
} else {
|
|
await favoriteSong({ songId })
|
|
toast.success("Added to favorites")
|
|
}
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[songIds, favoriteSong, unfavoriteSong]
|
|
)
|
|
|
|
const favoriteSongs = React.useCallback(
|
|
async (ids: Id<"songs">[]) => {
|
|
const unique = [...new Set(ids)]
|
|
if (unique.length === 0) {
|
|
return
|
|
}
|
|
try {
|
|
await favoriteSongsMutation({ songIds: unique })
|
|
toast.success(
|
|
unique.length > 1
|
|
? `Favorited ${unique.length} songs`
|
|
: "Added to favorites"
|
|
)
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[favoriteSongsMutation]
|
|
)
|
|
|
|
const toggleSongsFavorite = React.useCallback(
|
|
async (ids: Id<"songs">[], favorited: boolean) => {
|
|
const unique = [...new Set(ids)]
|
|
if (unique.length === 0) {
|
|
return true
|
|
}
|
|
try {
|
|
if (favorited) {
|
|
await unfavoriteSongs({ songIds: unique })
|
|
toast.success(
|
|
unique.length > 1
|
|
? `Removed ${unique.length} songs from favorites`
|
|
: "Removed from favorites"
|
|
)
|
|
} else {
|
|
await favoriteSongsMutation({ songIds: unique })
|
|
toast.success(
|
|
unique.length > 1
|
|
? `Favorited ${unique.length} songs`
|
|
: "Added to favorites"
|
|
)
|
|
}
|
|
return true
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
return false
|
|
}
|
|
},
|
|
[favoriteSongsMutation, unfavoriteSongs]
|
|
)
|
|
|
|
const toggleAlbumFavorite = React.useCallback(
|
|
async (albumId: Id<"albums">) => {
|
|
try {
|
|
if (albumIds.has(albumId)) {
|
|
await unfavoriteAlbum({ albumId })
|
|
toast.success("Removed album from favorites")
|
|
} else {
|
|
await favoriteAlbum({ albumId })
|
|
toast.success("Added album to favorites")
|
|
}
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[albumIds, favoriteAlbum, unfavoriteAlbum]
|
|
)
|
|
|
|
const toggleArtistFavorite = React.useCallback(
|
|
async (artistId: Id<"artists">) => {
|
|
try {
|
|
if (artistIds.has(artistId)) {
|
|
await unfavoriteArtist({ artistId })
|
|
toast.success("Removed artist from favorites")
|
|
} else {
|
|
await favoriteArtist({ artistId })
|
|
toast.success("Added artist to favorites")
|
|
}
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[artistIds, favoriteArtist, unfavoriteArtist]
|
|
)
|
|
|
|
const value = React.useMemo(
|
|
() => ({
|
|
isSongFavorite,
|
|
isAlbumFavorite,
|
|
isArtistFavorite,
|
|
toggleSongFavorite,
|
|
toggleSongsFavorite,
|
|
toggleAlbumFavorite,
|
|
toggleArtistFavorite,
|
|
favoriteSongs,
|
|
}),
|
|
[
|
|
isSongFavorite,
|
|
isAlbumFavorite,
|
|
isArtistFavorite,
|
|
toggleSongFavorite,
|
|
toggleSongsFavorite,
|
|
toggleAlbumFavorite,
|
|
toggleArtistFavorite,
|
|
favoriteSongs,
|
|
]
|
|
)
|
|
|
|
return (
|
|
<FavoritesContext.Provider value={value}>
|
|
{children}
|
|
</FavoritesContext.Provider>
|
|
)
|
|
}
|