Skip to content

refactor(components): extract useDraftGeneration hook (Wave 5.7 Phase A complete)#55

Merged
saagpatel merged 1 commit into
masterfrom
codex/refactor/wave5-7-draft-generation
Apr 21, 2026
Merged

refactor(components): extract useDraftGeneration hook (Wave 5.7 Phase A complete)#55
saagpatel merged 1 commit into
masterfrom
codex/refactor/wave5-7-draft-generation

Conversation

@saagpatel
Copy link
Copy Markdown
Owner

What

Extracts the final Phase A hook, `useDraftGeneration`, from `src/components/Draft/DraftTab.tsx`. Completes the Wave 5.7 hook decomposition:

File size: 2283 → 2138 LOC (-145 on this PR; -435 total across both PRs from original 2573, ~17% reduction).

Why

`handleGenerate` + `handleGenerateAlternative` + `handleChooseAlternative` + `handleUseAlternative` form one cohesive concern: streaming LLM response generation and the alternatives lifecycle. The hook owns the two loading flags (`generating`, `generatingAlternative`) and the `firstDraftStartMsRef` time-to-draft tracker, accepting setters for the response-content state (`response`, `sources`, `metrics`, `confidence`, `grounding`, `originalResponse`, `isResponseEdited`) that stays in the shell because many non-generation handlers also write those fields.

How

  1. Create `useDraftGeneration.ts` with the 4 callbacks + 2 flags + the first-draft-timestamp ref.
  2. Add `useDraftGeneration.test.ts` (4 `renderHook` tests covering: no-model block, success path, alternative skip, alternative use).
  3. Update `DraftTab.tsx` to consume the hook, removing 4 inline `useCallback` handlers and the two `useState` + `useRef` declarations they owned.
  4. Replace the `setGeneratingAlternative(false)` + `firstDraftStartMsRef.current = null` lines in `handleClear` with a single `resetGeneration()` call.

Deferred to a follow-up PR

  • Phase B — sub-component panel extractions (`DraftIntakePanel`, `DraftResponsePanel`, `DraftChecklistPanel`).
  • Phase C — shell reduction to ≤ 700 LOC (depends on Phase B).
  • Phase D — DraftTab integration tests (depends on Phase C).

Testing

  • `pnpm tsc --noEmit` — clean.
  • `pnpm test` — 200 tests pass (was 196; +4 new hook tests).
  • `pnpm ui:gate:static` — clean (only pre-existing warnings).

Risk / Notes

  • `DraftTabHandle` interface unchanged.
  • `useWorkspaceDraftState` consumer contract preserved (shell still owns the response-content state and passes its setters in).
  • The conversation-submit handler in the shell still calls `setGenerating(true/false)` directly — exposed from the hook for that exact purpose.

Move the final Phase A concern — streaming response generation +
alternatives lifecycle — out of DraftTab.tsx into a dedicated hook.
Hook owns generating/generatingAlternative flags and the
firstDraftStartMsRef time-to-draft tracker; accepts setters for the
response/sources/metrics/confidence/grounding state that remains in
the shell, since those are read by many non-generation handlers.
@saagpatel saagpatel merged commit 3a350a8 into master Apr 21, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants