Files
tolaria/src/hooks/useDiffMode.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

refactor: decompose Editor.tsx, NoteList.tsx, Sidebar.tsx — CodeScene hotspots (#126) * refactor: decompose Editor.tsx into focused subcomponents and hooks Extract from the monolithic Editor component (cc=35, 213 LoC, score 7.82): - useDiffMode hook: diff state + toggle/commit-diff handlers - useEditorFocus hook: new-note focus event listener - editorSchema: WikiLink spec + BlockNote schema (module-level) - SingleEditorView: BlockNote editor + suggestion menus - EditorContent: breadcrumb bar + diff/editor/loading views - EditorRightPanel: AI chat / Inspector panel switching - suggestionEnrichment util: shared enrichment logic (eliminates duplication) Editor.tsx is now 10.0 code health (was 7.82). All 25 existing tests pass. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: extract standalone functions from NoteList and Sidebar to reduce cc NoteList: extract createNoteStatusResolver and toggleSetMember from NoteListInner (cc 13→8, score 9.04→9.38). Sidebar: extract applyCustomization from Sidebar (cc 10→7, score 9.09→10.0). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * test: add tests for useDiffMode, useEditorFocus, and suggestionEnrichment Cover extracted hook/utility logic: diff toggle/reset, editor focus event listener with adaptive timing, and suggestion item enrichment pipeline. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: update architecture for editor decomposition and write .claude-done Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-02-27 15:27:25 +01:00
import { useState, useEffect, useCallback } from 'react'
interface UseDiffModeParams {
activeTabPath: string | null
onLoadDiff?: (path: string) => Promise<string>
onLoadDiffAtCommit?: (path: string, commitHash: string) => Promise<string>
}
export function useDiffMode({ activeTabPath, onLoadDiff, onLoadDiffAtCommit }: UseDiffModeParams) {
const [diffMode, setDiffMode] = useState(false)
const [diffContent, setDiffContent] = useState<string | null>(null)
const [diffLoading, setDiffLoading] = useState(false)
useEffect(() => {
setDiffMode(false)
setDiffContent(null)
}, [activeTabPath])
const handleToggleDiff = useCallback(async () => {
if (diffMode) {
setDiffMode(false)
setDiffContent(null)
return
}
if (!activeTabPath || !onLoadDiff) return
setDiffLoading(true)
try {
const diff = await onLoadDiff(activeTabPath)
setDiffContent(diff)
setDiffMode(true)
} catch (err) {
console.warn('Failed to load diff:', err)
} finally {
setDiffLoading(false)
}
}, [diffMode, activeTabPath, onLoadDiff])
const handleViewCommitDiff = useCallback(async (commitHash: string) => {
if (!activeTabPath || !onLoadDiffAtCommit) return
setDiffLoading(true)
try {
const diff = await onLoadDiffAtCommit(activeTabPath, commitHash)
setDiffContent(diff)
setDiffMode(true)
} catch (err) {
console.warn('Failed to load commit diff:', err)
} finally {
setDiffLoading(false)
}
}, [activeTabPath, onLoadDiffAtCommit])
return { diffMode, diffContent, diffLoading, handleToggleDiff, handleViewCommitDiff }
}