97 lines
2.8 KiB
TypeScript
97 lines
2.8 KiB
TypeScript
import type { OptimisticLocalStore, OptimisticUpdate } from "convex/browser"
|
|
import type { FunctionArgs, FunctionReference } from "convex/server"
|
|
|
|
import { api } from "../convex/_generated/api"
|
|
import type { Id } from "../convex/_generated/dataModel"
|
|
|
|
type FavoriteIds = {
|
|
favoriteSongs: Id<"songs">[]
|
|
favoriteAlbums: Id<"albums">[]
|
|
favoriteArtists: Id<"artists">[]
|
|
}
|
|
|
|
type FavoriteList = keyof FavoriteIds
|
|
|
|
function patchFavorites<List extends FavoriteList>(
|
|
store: OptimisticLocalStore,
|
|
list: List,
|
|
update: (ids: FavoriteIds[List]) => FavoriteIds[List]
|
|
) {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (!current) return
|
|
const next = update(current[list])
|
|
if (next !== current[list]) {
|
|
store.setQuery(
|
|
api.favorites.getUserFavorites,
|
|
{},
|
|
{
|
|
...current,
|
|
[list]: next,
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
function addFavorites<List extends FavoriteList>(
|
|
store: OptimisticLocalStore,
|
|
list: List,
|
|
ids: FavoriteIds[List]
|
|
) {
|
|
patchFavorites(store, list, (current) => {
|
|
const added = ids.filter((id) => !(current as string[]).includes(id))
|
|
return added.length === 0
|
|
? current
|
|
: ([...new Set([...current, ...added])] as FavoriteIds[List])
|
|
})
|
|
}
|
|
|
|
function removeFavorites<List extends FavoriteList>(
|
|
store: OptimisticLocalStore,
|
|
list: List,
|
|
ids: FavoriteIds[List]
|
|
) {
|
|
const removed = new Set<string>(ids)
|
|
patchFavorites(
|
|
store,
|
|
list,
|
|
(current) =>
|
|
(current as string[]).filter(
|
|
(id) => !removed.has(id)
|
|
) as FavoriteIds[List]
|
|
)
|
|
}
|
|
|
|
type FavoritesApi = typeof api.favorites
|
|
type FavoriteMutation = {
|
|
[
|
|
Name in keyof FavoritesApi
|
|
]: FavoritesApi[Name] extends FunctionReference<"mutation"> ? Name : never
|
|
}[keyof FavoritesApi]
|
|
|
|
/**
|
|
* Optimistic updates for the favorites mutations, so toggles show instantly.
|
|
* Pass them to `useMutation(...).withOptimisticUpdate(...)`.
|
|
*/
|
|
export const favoriteOptimisticUpdates = {
|
|
favoriteSong: (store, { songId }) =>
|
|
addFavorites(store, "favoriteSongs", [songId]),
|
|
favoriteSongs: (store, { songIds }) =>
|
|
addFavorites(store, "favoriteSongs", songIds),
|
|
unfavoriteSong: (store, { songId }) =>
|
|
removeFavorites(store, "favoriteSongs", [songId]),
|
|
unfavoriteSongs: (store, { songIds }) =>
|
|
removeFavorites(store, "favoriteSongs", songIds),
|
|
favoriteAlbum: (store, { albumId }) =>
|
|
addFavorites(store, "favoriteAlbums", [albumId]),
|
|
unfavoriteAlbum: (store, { albumId }) =>
|
|
removeFavorites(store, "favoriteAlbums", [albumId]),
|
|
favoriteArtist: (store, { artistId }) =>
|
|
addFavorites(store, "favoriteArtists", [artistId]),
|
|
unfavoriteArtist: (store, { artistId }) =>
|
|
removeFavorites(store, "favoriteArtists", [artistId]),
|
|
} satisfies {
|
|
[Name in FavoriteMutation]?: OptimisticUpdate<
|
|
FunctionArgs<FavoritesApi[Name]>
|
|
>
|
|
}
|