All files / web/src/hooks useNextSkillToLearn.ts

100% Statements 51/51
100% Branches 9/9
100% Functions 4/4
100% Lines 51/51

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 5x 6x 2x 2x 2x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x  
/**
 * Hook for fetching the next skill a student should learn
 *
 * This hook is used by the StartPracticeModal to show a tutorial gate
 * when a new skill is ready to be unlocked.
 */
 
import { useQuery } from '@tanstack/react-query'
import type { SkillSuggestion } from '@/lib/curriculum/skill-unlock'
 
interface NextSkillResponse {
  suggestion: SkillSuggestion | null
}
 
export const nextSkillKeys = {
  all: ['nextSkill'] as const,
  forPlayer: (playerId: string) => [...nextSkillKeys.all, playerId] as const,
}
 
/**
 * Fetch the next skill the student should learn.
 * Returns null if no new skill is available (student is working on current skills).
 */
async function fetchNextSkillToLearn(playerId: string): Promise<SkillSuggestion | null> {
  const response = await fetch(`/api/curriculum/${playerId}/next-skill`)
 
  if (!response.ok) {
    const error = await response.json()
    throw new Error(error.message || 'Failed to fetch next skill')
  }
 
  const data: NextSkillResponse = await response.json()
  return data.suggestion
}
 
/**
 * Hook to get the next skill the student should learn.
 *
 * @param playerId - The player ID
 * @param enabled - Whether to enable the query (default: true)
 * @returns Query result with suggestion or null
 */
export function useNextSkillToLearn(playerId: string, enabled = true) {
  return useQuery({
    queryKey: nextSkillKeys.forPlayer(playerId),
    queryFn: () => fetchNextSkillToLearn(playerId),
    enabled: enabled && !!playerId,
    staleTime: 30_000, // 30 seconds - skill state doesn't change frequently
    refetchOnWindowFocus: false,
  })
}