Files
movie-advisor/hooks/useShowState.ts
T
nerandClaude Opus 5 01753d4eb6 refactor(analytics): method per event instead of track(name, props)
analytics.showViewed({...}) reads better than track('show_viewed', {...}) and
drops the track_ alias Captions needed, where vidstack already exports track.

The methods are derived from the event catalogue with a mapped type plus a
Proxy rather than written out, so the catalogue stays the only place an event
is declared and the two cannot drift. Names stay snake_case on the wire.
Emitters are cached so a method is referentially stable and safe to pass to a
hook or use as an effect dependency.

Events with no payload are typed void and take no arguments.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-14 12:39:54 +03:00

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, 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;