Skip to content

refactor(components): extract useWorkspaceArtifacts hook#59

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

refactor(components): extract useWorkspaceArtifacts hook#59
saagpatel merged 1 commit into
masterfrom
codex/refactor/wave5-7-draft-workspace-artifacts

Conversation

@saagpatel
Copy link
Copy Markdown
Owner

What

Extracts the workspace-artifact concerns out of `DraftTab.tsx` into a new `useWorkspaceArtifacts` hook:

  • similar-cases state (`similarCases`, `similarCasesLoading`, `compareCase`)
  • debounced auto-refresh effect
  • `handleRefreshSimilarCases`, `handleCompareLastResolution`, `handleCompareSimilarCase`
  • `handleSaveCurrentResolutionKit`, `handleApplyResolutionKit`
  • `handleToggleWorkspaceFavorite`

Shell reduction: 1738 → 1551 LOC (-187 on this PR).

Why

These callbacks form one cohesive concern (workspace artifacts — similar cases, resolution kits, favorites) and share the `similarCases`/`compareCase` state. Auto-refresh used to live in a useEffect in the shell that drove `handleRefreshSimilarCases`; it moves into the hook so the hook owns both state and the effect that populates it.

How

  1. Create `useWorkspaceArtifacts.ts` with the state + 6 self-contained handlers.
  2. Place the hook call between `useWorkspaceDerivedArtifacts` (provides `kbDraft`) and `useWorkspaceDraftState` (consumes `setCompareCase`). This ordering is load-bearing.
  3. Keep `loadSimilarCaseIntoWorkspace` and `handleOpenSimilarCase` in the shell — they depend on `handleLoadDraft` from `useWorkspaceDraftState`, which is called AFTER the artifacts hook. Moving those would require either deferred refs or a third hook.
  4. Replace `setSimilarCases([])/setSimilarCasesLoading(false)/setCompareCase(null)` in `handleClear` with a single `resetWorkspaceArtifacts()` call.
  5. Drop now-unused imports: `applyResolutionKit`, `buildResolutionKitFromWorkspace`, `buildSimilarCases`, `ResolutionKit` type, `WorkspaceFavorite` type.
  6. Add `useWorkspaceArtifacts.test.ts` (7 tests) covering: debounce auto-refresh fires, disabled-flag skip, empty-response compare guard, save kit + log + refresh, apply kit + focus-intake, favorite add, favorite remove.

Testing

  • `pnpm tsc --noEmit` — clean.
  • `pnpm test` — 227 pass (was 220; +7 new hook tests).
  • `pnpm ui:gate:static` — exit 0.

Risk / Notes

  • `DraftTabHandle` unchanged.
  • Debounce delay and query-building logic for similar-cases preserved exactly.
  • Resolution-kit payload shape unchanged.
  • Favorite toggle save/delete branching unchanged.

Wave 5 status after this PR

  • DraftTab: 2573 → 1551 LOC (-1022, ~40% total reduction)
  • Tests: 184 → 227 (+43 direct Draft concern tests)
  • 11 hooks + 1 panel extracted + 1 handle pin
  • ~550 LOC from the 1000-target. Two more concerns (response actions + lifecycle effects) remain in the roadmap.

Move similar-cases state (including the debounce autoRefresh effect),
resolution kits save+apply, workspace favorite toggle, and the compare
card state out of DraftTab.tsx into a dedicated hook. The two similar-
case loaders that bridge into useWorkspaceDraftState (loadSimilarCaseInto-
Workspace, handleOpenSimilarCase) stay in the shell because the hook
ordering is useWorkspaceArtifacts before useWorkspaceDraftState (so
setCompareCase can be passed down), and those two loaders need the
handleLoadDraft returned by useWorkspaceDraftState.
@saagpatel saagpatel merged commit b0869a1 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