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>
This commit is contained in:
nerandClaude Opus 5 committed 2026-08-14 12:39:54 +03:00
1 parent d67a0d4df6
commit 01753d4eb6
16 files changed
+84 -40

No files matched your search

+2 -2
View File
@@ -1,7 +1,7 @@
import { useCallback, useMemo } from 'react';
import { QueryKey, useInfiniteQuery, useSuspenseInfiniteQuery } from '@tanstack/react-query';
import { track } from '@/lib/analytics';
import analytics from '@/lib/analytics';
import type { ListName } from '@/lib/analytics/events';
interface Props {
@@ -51,7 +51,7 @@ const useInfiniteList = ({
const pages = data?.pages.length ?? 0;
const loadNextPage = useCallback(() => {
if (list) track('list_loaded_more', { list, page: pages + 1 });
if (list) analytics.listLoadedMore({ list, page: pages + 1 });
return fetchNextPage();
}, [list, pages, fetchNextPage]);
+2 -2
View File
@@ -3,7 +3,7 @@ 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 { track } from '@/lib/analytics';
import analytics from '@/lib/analytics';
import { useFavorites, useFavoritesStateToggle, useWatchList, useWatchListStateToggle } from './useLocalUsersLists';
import { useSession } from './useSession';
@@ -52,7 +52,7 @@ export const useMutateShowState = (show: Show) => {
list: mutation.variables?.list,
isPending: session ? mutation.isPending : false,
mutate: ({ list, value }: { list: 'favorite' | 'watchlist'; value: boolean }) => {
track('library_toggled', { showId: show.id, showType: show.type, list, value });
analytics.libraryToggled({ showId: show.id, showType: show.type, list, value });
if (session) return mutation.mutate({ showId: show.id, showType: show.type, list, value });