All files / web/src/lib/audio VoiceChainContext.tsx

76.19% Statements 32/42
100% Branches 1/1
50% Functions 1/2
76.19% Lines 32/42

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 431x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2751x 2751x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                      
'use client'
 
/**
 * Nestable context for stacking voice sources onto the global TTS voice chain.
 *
 * Each <VoiceChainProvider> prepends its voices on top of any parent's,
 * so the innermost provider has highest priority.
 */
 
import { createContext, useContext, useMemo } from 'react'
import type { VoiceSource } from './voiceSource'
 
const VoiceChainContext = createContext<VoiceSource[]>([])
 
/** Read the accumulated voice-chain overrides from context. */
export function useVoiceChainOverrides(): VoiceSource[] {
  return useContext(VoiceChainContext)
}
 
/**
 * Stack additional voice sources on top of any parent chain.
 *
 * Nesting multiple providers concatenates their voices (inner first):
 * ```tsx
 * <VoiceChainProvider voices={[ash]}>       // outer
 *   <VoiceChainProvider voices={[echo]}>    // inner
 *     {/* effective: [echo, ash] *\/}
 *   </VoiceChainProvider>
 * </VoiceChainProvider>
 * ```
 */
export function VoiceChainProvider({
  voices,
  children,
}: {
  voices: VoiceSource[]
  children: React.ReactNode
}) {
  const parentChain = useContext(VoiceChainContext)
  const combined = useMemo(() => [...voices, ...parentChain], [voices, parentChain])
  return <VoiceChainContext.Provider value={combined}>{children}</VoiceChainContext.Provider>
}