All files / web/src/hooks useSongAlignment.ts

0% Statements 0/30
0% Branches 0/1
0% Functions 0/1
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31                                                             
'use client'

/**
 * Fetches the per-song word-alignment JSON written by the ElevenLabs music
 * task and served at `/api/audio/songs/{songId}/alignment`.
 *
 * Disabled when no path is provided (legacy songs without alignment). The
 * route 404s for songs generated before the timestamps feature shipped —
 * we surface that as a null query result so the player degrades cleanly.
 */

import { useQuery } from '@tanstack/react-query'
import { sessionSongKeys } from '@/lib/queryKeys'
import type { MusicAlignmentJson } from '@/lib/elevenlabs/music-client'

export function useSongAlignment(alignmentPath: string | null | undefined) {
  return useQuery<MusicAlignmentJson | null>({
    queryKey: sessionSongKeys.alignment(alignmentPath ?? ''),
    queryFn: async () => {
      if (!alignmentPath) return null
      const res = await fetch(alignmentPath)
      if (!res.ok) return null
      return (await res.json()) as MusicAlignmentJson
    },
    enabled: !!alignmentPath,
    // Alignment is immutable per song — cache forever.
    staleTime: Number.POSITIVE_INFINITY,
    gcTime: Number.POSITIVE_INFINITY,
  })
}