From 86c6d98b80adc1e89b5f71c0a63bb25a7afcfff0 Mon Sep 17 00:00:00 2001 From: Andres Date: Sat, 5 Jul 2025 23:57:21 +0200 Subject: [PATCH] store transcripts in redis/pg to alleviate the load on nq --- .../players/[id]/_components/games-table.tsx | 52 +++++++++++------- src/server/api/routers/history.ts | 10 ++++ src/server/db/schema.ts | 7 +++ src/server/services/neatqueue.service.ts | 53 +++++++++++++++++++ 4 files changed, 102 insertions(+), 20 deletions(-) diff --git a/src/app/(home)/players/[id]/_components/games-table.tsx b/src/app/(home)/players/[id]/_components/games-table.tsx index 5b68ff9..e2eb0a2 100644 --- a/src/app/(home)/players/[id]/_components/games-table.tsx +++ b/src/app/(home)/players/[id]/_components/games-table.tsx @@ -18,6 +18,7 @@ import { } from '@/components/ui/table' import { cn } from '@/lib/utils' import type { SelectGames } from '@/server/db/types' +import { api } from '@/trpc/react' import { type SortingState, createColumnHelper, @@ -42,11 +43,6 @@ const numberFormatter = new Intl.NumberFormat('en-US', { }) const columnHelper = createColumnHelper() -function getTranscript(gameNumber: number) { - return fetch( - `https://api.neatqueue.com/api/transcript/1226193436521267223/${gameNumber}` - ).then((res) => res.json()) -} // This function is now moved inside the GamesTable component const useColumns = (openTranscriptFn?: (gameNumber: number) => void) => { const format = useFormatter() @@ -197,22 +193,25 @@ const useColumns = (openTranscriptFn?: (gameNumber: number) => void) => { export function GamesTable({ games }: { games: SelectGames[] }) { const [sorting, setSorting] = useState([]) const [isDialogOpen, setIsDialogOpen] = useState(false) - const [transcriptContent, setTranscriptContent] = useState('') const [transcriptGameNumber, setTranscriptGameNumber] = useState< number | null >(null) + // Use the tRPC useQuery hook to fetch the transcript + const { data: transcriptContent, isLoading } = api.history.getTranscript.useQuery( + { gameNumber: transcriptGameNumber ?? 0 }, + { + // Only fetch when we have a game number and the dialog is open + enabled: transcriptGameNumber !== null && isDialogOpen, + // Don't refetch on window focus + refetchOnWindowFocus: false, + } + ) + // New openTranscript function that sets state instead of opening a new window const openTranscript = (gameNumber: number): void => { setTranscriptGameNumber(gameNumber) - getTranscript(gameNumber) - .then((html: string) => { - setTranscriptContent(html) - setIsDialogOpen(true) - }) - .catch((err) => { - console.error('Failed to load transcript:', err) - }) + setIsDialogOpen(true) } // Pass the openTranscript function to useColumns @@ -308,12 +307,25 @@ export function GamesTable({ games }: { games: SelectGames[] }) { {/* Use iframe to isolate the transcript content and prevent style leakage */}
-