88 lines
2.7 KiB
TypeScript
88 lines
2.7 KiB
TypeScript
import type { OptimisticLocalStore, OptimisticUpdate } from "convex/browser"
|
|
import type {
|
|
FunctionArgs,
|
|
FunctionReference,
|
|
FunctionReturnType,
|
|
} from "convex/server"
|
|
|
|
import { api } from "../convex/_generated/api"
|
|
import { chunk } from "../convex/limits"
|
|
|
|
type UserFavorites = FunctionReturnType<typeof api.favorites.getUserFavorites>
|
|
type FavoriteList = keyof UserFavorites
|
|
|
|
/** Rewrites one list of the cached `getUserFavorites` result, if loaded. */
|
|
function patchFavorites(
|
|
store: OptimisticLocalStore,
|
|
list: FavoriteList,
|
|
update: (ids: string[]) => string[]
|
|
) {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (!current) return
|
|
const ids: string[] = current[list].flat()
|
|
const next = update(ids)
|
|
if (next !== ids) {
|
|
// `next` only holds ids of this list's kind, which TS cannot follow here.
|
|
store.setQuery(api.favorites.getUserFavorites, {}, {
|
|
...current,
|
|
[list]: chunk(next),
|
|
} as UserFavorites)
|
|
}
|
|
}
|
|
|
|
function addFavorites(
|
|
store: OptimisticLocalStore,
|
|
list: FavoriteList,
|
|
ids: string[]
|
|
) {
|
|
patchFavorites(store, list, (current) => {
|
|
const added = ids.filter((id) => !current.includes(id))
|
|
return added.length === 0 ? current : [...new Set([...current, ...added])]
|
|
})
|
|
}
|
|
|
|
function removeFavorites(
|
|
store: OptimisticLocalStore,
|
|
list: FavoriteList,
|
|
ids: string[]
|
|
) {
|
|
const removed = new Set(ids)
|
|
patchFavorites(store, list, (current) =>
|
|
current.filter((id) => !removed.has(id))
|
|
)
|
|
}
|
|
|
|
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]>
|
|
>
|
|
}
|