12 KiB
MetaBuilder Component Refactoring Strategy - Complete Guide
Status: ✅ Strategy Complete - Ready for Implementation
Generated: December 25, 2025
Estimated Effort: 103 hours over 4 weeks
📖 Documentation Index
This guide addresses React component size violations in MetaBuilder. The project enforces a <150 LOC limit per component to maintain modularity and follow the data-driven architecture philosophy.
📑 Documents in This Series
| Document | Purpose | Audience | Length |
|---|---|---|---|
| REFACTORING_SUMMARY.md | Executive overview and quick stats | Managers, Tech Leads | 300 lines |
| REFACTORING_STRATEGY.md | Detailed technical blueprint | Developers, Architects | 1,673 lines |
| REFACTORING_CHECKLIST.md | Implementation task list | Project Managers | 484 lines |
| REFACTORING_QUICK_REFERENCE.md | Patterns, templates, pitfalls | Developers | 857 lines |
🎯 Quick Start
For Managers
- Read REFACTORING_SUMMARY.md (5 min)
- Review timeline and effort estimates
- Allocate team resources for 4-week sprint
- Approve database schema changes
For Tech Leads
- Read REFACTORING_STRATEGY.md (30 min)
- Review architecture decisions
- Approve hook and component designs
- Plan code review process
For Developers
- Start with REFACTORING_QUICK_REFERENCE.md (15 min)
- Deep dive into REFACTORING_STRATEGY.md for your assigned violation
- Use REFACTORING_CHECKLIST.md as you implement
- Reference patterns for similar components
For Architects
- Review entire REFACTORING_STRATEGY.md
- Evaluate database schema design
- Validate hook API contracts
- Assess multi-tenant implications
📊 At a Glance
The Problem
- 8 components exceed 150 LOC architectural limit
- Largest: GitHubActionsFetcher (887 LOC)
- Average: 569 LOC per violation
- Impact: Hard to test, maintain, and reuse
The Solution
- Component decomposition into focused, small components
- Custom hooks for business logic and data management
- Database persistence for state that survives reloads
- Lua scripts for complex, reusable operations
- Configuration as data instead of hardcoded values
The Outcome
BEFORE AFTER
────────────────────────────────────────
8 monoliths → 40+ small components
569 LOC average → 73 LOC average
5 custom hooks → 20+ custom hooks
~10 Lua scripts → ~15 Lua scripts
Ephemeral state → Persistent database
Hardcoded config → JSON in database
🔍 Violations by Priority
Violation #1: GitHubActionsFetcher.tsx (887 LOC)
Target: 95 LOC | Effort: 28 hours | Timeline: Week 1
Responsibilities to split:
- GitHub API integration → useGitHubActions hook
- Log fetching → useWorkflowLogs hook
- Analysis → useWorkflowAnalysis hook
- UI rendering → 6 sub-components
Database models: 3 (WorkflowRun, Job, Config)
See: REFACTORING_STRATEGY.md - Violation #1
Violation #2: NerdModeIDE.tsx (733 LOC)
Target: 110 LOC | Effort: 30 hours | Timeline: Week 2
Responsibilities to split:
- File tree operations → useIDEFileTree hook
- Project management → useIDEProject hook
- File editing → useIDEEditor hook
- Git operations → useIDEGit hook
- Test execution → useIDETest hook
- UI rendering → 7 sub-components
Database models: 4 (Project, File, GitConfig, TestRun)
See: REFACTORING_STRATEGY.md - Violation #2
Violation #3: LuaEditor.tsx (686 LOC)
Target: 120 LOC | Effort: 25 hours | Timeline: Week 3
Responsibilities to split:
- Monaco config (60+ LOC) → LuaMonacoConfig component
- Script management → useLuaScripts hook
- Editor state → useLuaEditor hook
- Execution → useLuaExecution hook
- Security → useLuaSecurity hook
- UI rendering → 8 sub-components
Database models: 3 (Library, Parameter, TestRun)
See: REFACTORING_STRATEGY.md - Violation #3
🏗️ Architecture Pattern
Before (Monolithic)
Component.tsx (800+ LOC)
├── useState for everything (15+ hooks)
├── API calls inline
├── Business logic mixed with UI
├── Hardcoded configs
└── Lost on page reload
After (Modular, Data-Driven)
Component.tsx (100 LOC) - UI only
├── Sub1.tsx (60 LOC) - Sub-feature 1
├── Sub2.tsx (70 LOC) - Sub-feature 2
├── Sub3.tsx (50 LOC) - Sub-feature 3
│
Hooks (extracted)
├── useFeature1.ts - Data + logic
├── useFeature2.ts - Data + logic
└── useFeature3.ts - Data + logic
│
Database (persistent)
├── Model1 - Feature 1 state
├── Model2 - Feature 2 state
└── Model3 - Feature 3 state
│
Lua (reusable)
├── ComplexOperation1.lua
└── ComplexOperation2.lua
📈 Effort & Timeline
WEEK 1: GitHubActionsFetcher (28 hours)
├─ Mon: Database + API hook
├─ Tue-Wed: Log + Analysis hooks
├─ Thu: Components (toolbar, list, jobs)
└─ Fri: Panel + Main component
WEEK 2: NerdModeIDE (30 hours)
├─ Mon: Database + Project hook
├─ Tue-Wed: Tree + Editor hooks
├─ Thu: File Tree + Panels
└─ Fri: Dialogs + Main component
WEEK 3: LuaEditor (25 hours)
├─ Mon: Config extraction + Hooks
├─ Tue: Parameter + Execution
├─ Wed: Security + Snippets
└─ Thu-Fri: Main component + Tests
WEEK 4: Validation (20 hours)
├─ Mon-Tue: Unit & Integration tests
├─ Wed: Code review & Optimization
├─ Thu: Staging & Documentation
└─ Fri: Production deploy
────────────────────────────────
TOTAL: 103 hours over 4 weeks
💾 Database Design Summary
New Models: 15 total
GitHub Feature (3):
- GitHubWorkflowRun - Workflow runs with cached data
- GitHubWorkflowJob - Job details and steps
- GitHubConfig - API credentials and settings
IDE Feature (4):
- IDEProject - Project metadata
- IDEFile - File tree with content
- GitConfig - Git repository config (shared)
- IDETestRun - Test execution results
Lua Feature (3):
- LuaScriptLibrary - Reusable code snippets
- LuaScriptParameter - Parameter definitions
- LuaTestRun - Execution history
Updated (1):
- LuaScript - Add description field
System (2):
- Tenant - Multi-tenant support
- User - Owner tracking
🪝 Custom Hooks Design
Data Fetching Hooks (3)
useGitHubActions()
useWorkflowLogs()
useWorkflowAnalysis()
File Management Hooks (5)
useIDEProject()
useIDEFileTree()
useIDEEditor()
useIDEGit()
useIDETest()
Lua Management Hooks (4)
useLuaScripts()
useLuaEditor()
useLuaExecution()
useLuaSecurity()
🧩 New Components: 25 total
GitHubActions (7)
- GitHubActionsToolbar - Refresh, download controls
- GitHubActionsRunsList - Workflow runs display
- GitHubActionsRunRow - Single run item
- GitHubActionsJobsPanel - Job and log display
- GitHubActionsJobStep - Single step item
- GitHubActionsAnalysisPanel - AI analysis results
- GitHubActionsFetcher - Refactored orchestrator
NerdModeIDE (8)
- IDEFileTreePanel - File tree with nodes
- IDEFileTreeNode - Single file/folder item
- IDEEditorPanel - Monaco editor with toolbar
- IDEToolbar - File/project operations
- IDEConsolePanel - Output and test results
- IDEGitDialog - Git configuration
- IDENewFileDialog - Create file dialog
- NerdModeIDE - Refactored orchestrator
LuaEditor (10)
- LuaScriptSelector - Script list
- LuaCodeEditor - Monaco editor
- LuaMonacoConfig - Editor configuration (60+ LOC extracted)
- LuaParameterItem - Single parameter editor
- LuaParametersPanel - Parameter list
- LuaExecutionResult - Results display
- LuaExecutionPanel - Execute script UI
- LuaSecurityWarningDialog - Security warnings
- LuaSnippetLibraryPanel - Code snippets
- LuaEditor - Refactored orchestrator
🧪 Testing Strategy
Unit Tests
- Each custom hook with mocked database/API
- Isolated component testing with mocked props
- 80%+ code coverage
Component Tests
- Props validation
- User interactions (click, type)
- State changes
- Error handling
Integration Tests
- Hook + Component interaction
- Database persistence
- Multiple component composition
E2E Tests
- Full feature workflows
- Real API calls
- Complete user journeys
✅ Success Criteria
- All components < 150 LOC
- 80%+ test coverage
- Zero ESLint violations
- All database migrations completed
- All hooks fully typed
- All Lua scripts tested
- No performance regressions
- All E2E tests passing
- Complete documentation
🚀 Getting Started
Step 1: Preparation
- Review REFACTORING_STRATEGY.md with team
- Approve database schema
- Set up testing environment
- Create GitHub issues from checklist
Step 2: Implementation (Week 1-3)
- Follow the 3-week implementation plan
- Use REFACTORING_CHECKLIST.md for tracking
- Reference REFACTORING_QUICK_REFERENCE.md for patterns
- Complete weekly code reviews
Step 3: Validation (Week 4)
- Run full test suite
- Performance benchmarking
- Code review and optimization
- Documentation finalization
Step 4: Deployment
- Staging environment testing
- Team sign-off
- Production deployment
- Monitoring and support
📚 Related Resources
TODO: Paths below are wrong from docs/refactoring; update copilot/PRD and schema/src references to valid locations.
- copilot-instructions.md - MetaBuilder architecture guide
- PRD.md - Product requirements
- prisma/schema.prisma - Current database schema
- src/hooks/ - Existing hook examples
- src/components/ - Component examples
🤝 Contributing
When implementing this strategy:
- Follow the checklist - Don't skip steps
- Write tests first - TDD approach
- Document as you go - Don't wait until the end
- Ask questions - If unclear about design
- Communicate progress - Weekly status updates
- Code review thoroughly - Multiple eyes on changes
📞 Questions?
| Question | Document | Section |
|---|---|---|
| "What's the total effort?" | REFACTORING_SUMMARY.md | Effort Breakdown |
| "How do I structure hooks?" | REFACTORING_QUICK_REFERENCE.md | Hook API Patterns |
| "What are the pitfalls?" | REFACTORING_QUICK_REFERENCE.md | Common Pitfalls |
| "What's my task?" | REFACTORING_CHECKLIST.md | Your Violation |
| "Full technical details?" | REFACTORING_STRATEGY.md | Full Document |
🎓 Key Principles
The strategy follows MetaBuilder's core philosophy:
- Data-Driven - Configuration in database/JSON, not code
- Modular - Small, focused components with clear boundaries
- Reusable - Hooks for logic, Lua for runtime-modifiable operations
- Testable - Each piece independently testable
- Persistent - State survives page reloads via database
- Multi-tenant - Database schema supports multiple tenants
📋 Document Checklist
- Summary of violations identified
- Detailed refactoring plan for each violation
- Database schema design
- Hook API specifications
- Component decomposition plan
- Lua script definitions
- Testing strategy
- Implementation checklist
- Quick reference guide
- Timeline and effort estimates
- Risk mitigation strategies
- Success criteria
Version: 1.0
Status: Ready for Implementation ✅
Last Updated: December 25, 2025
Total Documentation: 3,600+ lines
Next Step: Discuss with team and begin Week 1 implementation! 🚀