Files
movie-advisor/app/(site)/(root)/useRandomMovie.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

46 lines
996 B
TypeScript

'use client';
import { startTransition, useCallback } from 'react';
import { atom, getDefaultStore, useAtom } from 'jotai';
import randomQuery from '@/data/queries/random';
import useInfiniteList from '@/hooks/useInfiniteList';
import analytics from '@/lib/analytics';
const indexAtom = atom(0);
export const useSilentIndex = () => getDefaultStore().get(indexAtom);
interface Props {
page: number;
}
const useRandomMovie = ({ page }: Props) => {
const [index, setIndex] = useAtom(indexAtom);
const { shows: movies, fetchNextPage } = useInfiniteList({
...randomQuery({ page }),
mode: 'default',
list: 'random'
});
if (movies.length && !movies[index]) {
setIndex(0);
}
return {
movie: movies[index],
movies,
fetchNextPage,
onIndexChange: useCallback(
(index: number) => {
analytics.randomShuffled({ index });
startTransition(() => setIndex(index));
},
[setIndex]
)
};
};
export default useRandomMovie;