Files
adnify/scripts/refactor/split-sidebar-full.js
adnaan e06e07d5e7 refactor(sidebar): split monolithic Sidebar component into modular structure
- Extract Sidebar.tsx (1938 lines) into 10 focused components
- Create sidebar/panels/ directory with ExplorerView, SearchView, GitView, ProblemsView, OutlineView, and HistoryView
- Create sidebar/components/ directory with FileTreeItem and InlineCreateInput subcomponents
- Add sidebar/utils.ts for shared utility functions
- Create sidebar/index.ts for clean exports
- Implement new minimal Sidebar.tsx (30 lines) as composition root
- Add refactor scripts (split-sidebar.js, split-sidebar-full.js) for future component extraction
- Update App.tsx imports to use new sidebar module structure
- Update Logger.ts with improved logging capabilities
- Update REFACTORING_PLAN.md to mark Phase 4 complete and plan Phase 5
- Improves code maintainability, testability, and reduces cognitive complexity per file
2025-12-25 20:13:28 +08:00

108 lines
3.4 KiB
JavaScript

/**
* 完整拆分 Sidebar.tsx
*/
const fs = require('fs')
const path = require('path')
const SIDEBAR_PATH = path.join(__dirname, '../../src/renderer/components/Sidebar.tsx')
const OUTPUT_DIR = path.join(__dirname, '../../src/renderer/components/sidebar')
function ensureDir(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
}
const content = fs.readFileSync(SIDEBAR_PATH, 'utf-8')
// 提取组件代码块
function extractComponent(name, startPattern, endPattern) {
const startMatch = content.match(startPattern)
if (!startMatch) return null
const startIndex = startMatch.index
let endIndex
if (endPattern) {
const endMatch = content.slice(startIndex).match(endPattern)
endIndex = endMatch ? startIndex + endMatch.index : content.length
} else {
// 找到下一个 function 定义
const nextFunc = content.slice(startIndex + 10).match(/\n\nfunction \w+\(/)
endIndex = nextFunc ? startIndex + 10 + nextFunc.index : content.length
}
return content.slice(startIndex, endIndex).trim()
}
ensureDir(path.join(OUTPUT_DIR, 'panels'))
ensureDir(path.join(OUTPUT_DIR, 'components'))
// ============ 创建 HistoryView ============
const historyView = `/**
* 历史记录视图 - 包装 CheckpointPanel
*/
import CheckpointPanel from '../../CheckpointPanel'
export function HistoryView() {
return <CheckpointPanel />
}
`
fs.writeFileSync(path.join(OUTPUT_DIR, 'panels/HistoryView.tsx'), historyView)
console.log('Created: panels/HistoryView.tsx')
// ============ 创建新的主 Sidebar.tsx ============
const newSidebar = `/**
* Sidebar 主组件
* 根据 activeSidePanel 渲染对应的面板
*/
import { useStore } from '@store'
import { ExplorerView } from './sidebar/panels/ExplorerView'
import { SearchView } from './sidebar/panels/SearchView'
import { GitView } from './sidebar/panels/GitView'
import { ProblemsView } from './sidebar/panels/ProblemsView'
import { OutlineView } from './sidebar/panels/OutlineView'
import { HistoryView } from './sidebar/panels/HistoryView'
export default function Sidebar() {
const { activeSidePanel } = useStore()
if (!activeSidePanel) return null
return (
<div className="w-full bg-background/60 backdrop-blur-xl border-r border-white/5 flex flex-col h-full animate-slide-in relative z-10 shadow-2xl shadow-black/50">
{activeSidePanel === 'explorer' && <ExplorerView />}
{activeSidePanel === 'search' && <SearchView />}
{activeSidePanel === 'git' && <GitView />}
{activeSidePanel === 'problems' && <ProblemsView />}
{activeSidePanel === 'outline' && <OutlineView />}
{activeSidePanel === 'history' && <HistoryView />}
</div>
)
}
`
// 备份原文件
const backupPath = SIDEBAR_PATH + '.backup'
if (!fs.existsSync(backupPath)) {
fs.copyFileSync(SIDEBAR_PATH, backupPath)
console.log('Backed up original Sidebar.tsx')
}
console.log('\n拆分说明:')
console.log('='.repeat(50))
console.log('1. 已创建 panels/HistoryView.tsx')
console.log('2. 已创建目录结构')
console.log('3. 原文件已备份为 Sidebar.tsx.backup')
console.log('')
console.log('由于其他组件较复杂,建议手动拆分:')
console.log(' - ExplorerView 依赖 FileTreeItem, InlineCreateInput')
console.log(' - SearchView 有大量本地状态')
console.log(' - GitView 有复杂的 Git 操作逻辑')
console.log(' - ProblemsView 依赖 LSP 诊断')
console.log(' - OutlineView 依赖 LSP 符号')
console.log('='.repeat(50))