refactor: extract hooks from App.tsx to improve code health (#62)

Extract vault management (useVaultSwitcher), git history loading
(useGitHistory), dialog state (useDialogs), and keyboard/command
setup (useAppCommands) into dedicated hooks. App.tsx code health
improves from 8.95 to 10.0 — all extracted hooks also score 10.0.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Luca Rossi
2026-02-25 13:31:19 +01:00
committed by GitHub
parent 131fe7dcd6
commit 558f50e610
5 changed files with 215 additions and 150 deletions

View File

@@ -0,0 +1,82 @@
import { useAppKeyboard } from './useAppKeyboard'
import { useCommandRegistry } from './useCommandRegistry'
import type { CommandAction } from './useCommandRegistry'
import { useKeyboardNavigation } from './useKeyboardNavigation'
import type { SidebarSelection, VaultEntry } from '../types'
import type { ViewMode } from './useViewMode'
interface Tab { entry: VaultEntry; content: string }
interface AppCommandsConfig {
activeTabPath: string | null
activeTabPathRef: React.MutableRefObject<string | null>
handleCloseTabRef: React.MutableRefObject<(path: string) => void>
tabs: Tab[]
entries: VaultEntry[]
allContent: Record<string, string>
modifiedCount: number
selection: SidebarSelection
onQuickOpen: () => void
onCommandPalette: () => void
onCreateNote: () => void
onSave: () => void
onOpenSettings: () => void
onTrashNote: (path: string) => void
onArchiveNote: (path: string) => void
onUnarchiveNote: (path: string) => void
onCommitPush: () => void
onSetViewMode: (mode: ViewMode) => void
onToggleInspector: () => void
onSelect: (sel: SidebarSelection) => void
onCloseTab: (path: string) => void
onSwitchTab: (path: string) => void
onReplaceActiveTab: (entry: VaultEntry) => void
onSelectNote: (entry: VaultEntry) => void
}
/** Sets up keyboard shortcuts, command registry, and keyboard navigation. */
export function useAppCommands(config: AppCommandsConfig): CommandAction[] {
useAppKeyboard({
onQuickOpen: config.onQuickOpen,
onCommandPalette: config.onCommandPalette,
onCreateNote: config.onCreateNote,
onSave: config.onSave,
onOpenSettings: config.onOpenSettings,
onTrashNote: config.onTrashNote,
onArchiveNote: config.onArchiveNote,
onSetViewMode: config.onSetViewMode,
activeTabPathRef: config.activeTabPathRef,
handleCloseTabRef: config.handleCloseTabRef,
})
const commands = useCommandRegistry({
activeTabPath: config.activeTabPath,
entries: config.entries,
modifiedCount: config.modifiedCount,
onQuickOpen: config.onQuickOpen,
onCreateNote: config.onCreateNote,
onSave: config.onSave,
onOpenSettings: config.onOpenSettings,
onTrashNote: config.onTrashNote,
onArchiveNote: config.onArchiveNote,
onUnarchiveNote: config.onUnarchiveNote,
onCommitPush: config.onCommitPush,
onSetViewMode: config.onSetViewMode,
onToggleInspector: config.onToggleInspector,
onSelect: config.onSelect,
onCloseTab: config.onCloseTab,
})
useKeyboardNavigation({
tabs: config.tabs,
activeTabPath: config.activeTabPath,
entries: config.entries,
selection: config.selection,
allContent: config.allContent,
onSwitchTab: config.onSwitchTab,
onReplaceActiveTab: config.onReplaceActiveTab,
onSelectNote: config.onSelectNote,
})
return commands
}