Files
movie-advisor/app/(site)/(root)/useRandomMovie.ts
T
nerandClaude Opus 5 66776c582f refactor(analytics): send titles alongside ids, drop comments
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>
2026-08-14 12:44:38 +03:00

50 lines
1.1 KiB
TypeScript

'use client';
import { startTransition, useCallback, useRef } 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'
});
const moviesRef = useRef(movies);
moviesRef.current = movies;
if (movies.length && !movies[index]) {
setIndex(0);
}
return {
movie: movies[index],
movies,
fetchNextPage,
onIndexChange: useCallback(
(index: number) => {
analytics.randomShuffled({ index, showTitle: moviesRef.current[index]?.title ?? '' });
startTransition(() => setIndex(index));
},
[setIndex]
)
};
};
export default useRandomMovie;