Skip to content

[Cleanup] Split useVoiceInput god hook (351 lines, 5 responsibilities) #346

@samzong

Description

@samzong

Problem

useVoiceInput is a 351-line hook managing 5 distinct concerns:

  1. Voice session lifecycle — create, start, stop, destroy session
  2. Press-hold timing — 4 refs (holdTimerRef, pressStartedAtRef, pressActiveRef, startedFromCurrentPressRef) for detecting long-press
  3. Keyboard event handlinghandleKeyDown/handleKeyUp with complex space-bar interception
  4. Intro screen stateisIntroOpen, introSeenRef, confirmIntro, dismissIntro
  5. Transcript insertioninsertIntoTextarea, insertLiteralSpace

The press-hold timing logic alone uses 4 refs and could be a reusable hook.

Location

File: packages/desktop/src/renderer/hooks/useVoiceInput.ts (351 lines)

Fix Approach

Split into focused hooks:

// usePressHoldTiming.ts — reusable, 0 voice-specific logic
export function usePressHoldTiming(delayMs: number) {
  // holdTimerRef, pressStartedAtRef, pressActiveRef, startedFromCurrentPressRef
  return { pressActive, startPress, endPress, clearHoldTimer };
}

// useVoiceSession.ts — session lifecycle only
export function useVoiceSession(createSession, requestPermission) {
  return { isListening, isTranscribing, interimTranscript, errorCode, beginListening, stopListening };
}

// useVoiceInput.ts — thin orchestrator (original, now ~80 lines)
export function useVoiceInput(opts) {
  const timing = usePressHoldTiming(opts.pressHoldDelayMs);
  const session = useVoiceSession(opts.createSession, opts.requestPermission);
  // Wire keyboard events + intro screen + transcript insertion
}

Verification

  1. Run pnpm check — must pass
  2. Test voice input: press-and-hold space, short press space (inserts space), trigger via UI button

Context

  • WG: UI & Design System
  • Priority: Medium
  • Estimated effort: ~1.5 hours

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/uiUI & Design System WGkind/cleanupCategorizes issue or PR as related to code cleanup

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions