import { useMemo } from 'react' import { useAppState } from '../state/AppState.js' import { hasVoiceAuth, isVoiceGrowthBookEnabled, } from '../voice/voiceModeEnabled.js' /** * Combines user intent (settings.voiceEnabled) with auth + GB kill-switch. * When using Doubao backend, auth check is skipped (Doubao has its own credentials). * Only the auth half is memoized on authVersion — it's the expensive one * (cold getClaudeAIOAuthTokens memoize → sync `security` spawn, ~60ms/call, * ~180ms total in profile v5 when token refresh cleared the cache mid-session). * GB is a cheap cached-map lookup and stays outside the memo so a mid-session * kill-switch flip still takes effect on the next render. */ export function useVoiceEnabled(): boolean { const userIntent = useAppState(s => s.settings.voiceEnabled === true) const provider = useAppState(s => s.settings.voiceProvider) // All hooks must be called unconditionally (Rules of Hooks) const authVersion = useAppState(s => s.authVersion) // eslint-disable-next-line react-hooks/exhaustive-deps const authed = useMemo(hasVoiceAuth, [authVersion]) if (provider === 'doubao') { return userIntent && isVoiceGrowthBookEnabled() } return userIntent && authed && isVoiceGrowthBookEnabled() }