Bare ids are unreadable in the dashboard, so every event that points at a show or a person now carries its title or name. Ids stay for disambiguation, since titles collide across remakes. - ShowRef gains showTitle, so show_viewed, library_toggled, torrents_searched, torrent_selected, playback_started and playback_failed all report it - person_viewed sends personName - random_shuffled sends the title of the movie scrolled to - torrent_selected sends the torrent title next to the show title - subtitles_selected sends the track label, such as English, rather than the internal track value Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
67 lines
2.4 KiB
TypeScript
67 lines
2.4 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
|
|
import statesMutation from '@/data/mutations/accountStates';
|
|
import statesQuery from '@/data/queries/accountStates';
|
|
import { KEY as USERS_SHOWS_KEY } from '@/data/queries/usersShows';
|
|
import analytics from '@/lib/analytics';
|
|
|
|
import { useFavorites, useFavoritesStateToggle, useWatchList, useWatchListStateToggle } from './useLocalUsersLists';
|
|
import { useSession } from './useSession';
|
|
|
|
const useShowState = ({ showId, showType = 'movie' }: { showId?: Show['id']; showType?: Show['type'] }) => {
|
|
const session = useSession();
|
|
|
|
const { map: favorites } = useFavorites();
|
|
const { map: watchList } = useWatchList();
|
|
|
|
const query = useQuery(statesQuery({ showId, showType, session }));
|
|
|
|
return session ? query.data : { id: showId, watchlist: watchList.has(showId!), favorite: favorites.has(showId!) };
|
|
};
|
|
|
|
export const useMutateShowState = (show: Show) => {
|
|
const session = useSession();
|
|
|
|
const { toggle: toggleFavorite } = useFavoritesStateToggle(show);
|
|
const { toggle: toggleWatchList } = useWatchListStateToggle(show);
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
const mutation = useMutation({
|
|
...statesMutation({ session }),
|
|
onMutate({ showId, showType, list, value }) {
|
|
const key = statesQuery({ showId, showType, session }).queryKey;
|
|
|
|
const snapshot = queryClient.getQueryData<ShowState>(key);
|
|
|
|
queryClient.setQueryData<ShowState>(key, (state) => ({ ...state, [list]: value }) as ShowState);
|
|
|
|
return snapshot;
|
|
},
|
|
onError(_, { showId, showType }, snapshot) {
|
|
const key = statesQuery({ showId, showType, session }).queryKey;
|
|
|
|
queryClient.setQueryData<ShowState>(key, snapshot);
|
|
},
|
|
onSuccess(_, { list, showType }) {
|
|
queryClient.invalidateQueries({ queryKey: USERS_SHOWS_KEY({ list, showType, session }), refetchType: 'all' });
|
|
}
|
|
});
|
|
|
|
return {
|
|
list: mutation.variables?.list,
|
|
isPending: session ? mutation.isPending : false,
|
|
mutate: ({ list, value }: { list: 'favorite' | 'watchlist'; value: boolean }) => {
|
|
analytics.libraryToggled({ showId: show.id, showType: show.type, showTitle: show.title, list, value });
|
|
|
|
if (session) return mutation.mutate({ showId: show.id, showType: show.type, list, value });
|
|
|
|
if (list === 'favorite') return toggleFavorite();
|
|
|
|
return toggleWatchList();
|
|
}
|
|
};
|
|
};
|
|
|
|
export default useShowState;
|