# 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](REFACTORING_SUMMARY.md)** | Executive overview and quick stats | Managers, Tech Leads | 300 lines | | **[REFACTORING_STRATEGY.md](REFACTORING_STRATEGY.md)** | Detailed technical blueprint | Developers, Architects | 1,673 lines | | **[REFACTORING_CHECKLIST.md](REFACTORING_CHECKLIST.md)** | Implementation task list | Project Managers | 484 lines | | **[REFACTORING_QUICK_REFERENCE.md](REFACTORING_QUICK_REFERENCE.md)** | Patterns, templates, pitfalls | Developers | 857 lines | --- ## ๐ŸŽฏ Quick Start ### For Managers 1. Read **REFACTORING_SUMMARY.md** (5 min) 2. Review timeline and effort estimates 3. Allocate team resources for 4-week sprint 4. Approve database schema changes ### For Tech Leads 1. Read **REFACTORING_STRATEGY.md** (30 min) 2. Review architecture decisions 3. Approve hook and component designs 4. Plan code review process ### For Developers 1. Start with **REFACTORING_QUICK_REFERENCE.md** (15 min) 2. Deep dive into **REFACTORING_STRATEGY.md** for your assigned violation 3. Use **REFACTORING_CHECKLIST.md** as you implement 4. Reference patterns for similar components ### For Architects 1. Review entire **REFACTORING_STRATEGY.md** 2. Evaluate database schema design 3. Validate hook API contracts 4. 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](REFACTORING_STRATEGY.md#violation-1-githubactionsfetchertsx-887-loc) --- ### 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](REFACTORING_STRATEGY.md#violation-2-nerdmodeidetsx-733-loc) --- ### 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](REFACTORING_STRATEGY.md#violation-3-luaeditortsx-686-loc) --- ## ๐Ÿ—๏ธ 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) ```typescript useGitHubActions() useWorkflowLogs() useWorkflowAnalysis() ``` ### File Management Hooks (5) ```typescript useIDEProject() useIDEFileTree() useIDEEditor() useIDEGit() useIDETest() ``` ### Lua Management Hooks (4) ```typescript useLuaScripts() useLuaEditor() useLuaExecution() useLuaSecurity() ``` --- ## ๐Ÿงฉ New Components: 25 total ### GitHubActions (7) 1. GitHubActionsToolbar - Refresh, download controls 2. GitHubActionsRunsList - Workflow runs display 3. GitHubActionsRunRow - Single run item 4. GitHubActionsJobsPanel - Job and log display 5. GitHubActionsJobStep - Single step item 6. GitHubActionsAnalysisPanel - AI analysis results 7. GitHubActionsFetcher - Refactored orchestrator ### NerdModeIDE (8) 1. IDEFileTreePanel - File tree with nodes 2. IDEFileTreeNode - Single file/folder item 3. IDEEditorPanel - Monaco editor with toolbar 4. IDEToolbar - File/project operations 5. IDEConsolePanel - Output and test results 6. IDEGitDialog - Git configuration 7. IDENewFileDialog - Create file dialog 8. NerdModeIDE - Refactored orchestrator ### LuaEditor (10) 1. LuaScriptSelector - Script list 2. LuaCodeEditor - Monaco editor 3. LuaMonacoConfig - Editor configuration (60+ LOC extracted) 4. LuaParameterItem - Single parameter editor 5. LuaParametersPanel - Parameter list 6. LuaExecutionResult - Results display 7. LuaExecutionPanel - Execute script UI 8. LuaSecurityWarningDialog - Security warnings 9. LuaSnippetLibraryPanel - Code snippets 10. 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 1. Review REFACTORING_STRATEGY.md with team 2. Approve database schema 3. Set up testing environment 4. Create GitHub issues from checklist ### Step 2: Implementation (Week 1-3) 1. Follow the 3-week implementation plan 2. Use REFACTORING_CHECKLIST.md for tracking 3. Reference REFACTORING_QUICK_REFERENCE.md for patterns 4. Complete weekly code reviews ### Step 3: Validation (Week 4) 1. Run full test suite 2. Performance benchmarking 3. Code review and optimization 4. Documentation finalization ### Step 4: Deployment 1. Staging environment testing 2. Team sign-off 3. Production deployment 4. 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](../.github/copilot-instructions.md)** - MetaBuilder architecture guide - **[PRD.md](./PRD.md)** - Product requirements - **[prisma/schema.prisma](../prisma/schema.prisma)** - Current database schema - **[src/hooks/](../src/hooks/)** - Existing hook examples - **[src/components/](../src/components/)** - Component examples --- ## ๐Ÿค Contributing When implementing this strategy: 1. **Follow the checklist** - Don't skip steps 2. **Write tests first** - TDD approach 3. **Document as you go** - Don't wait until the end 4. **Ask questions** - If unclear about design 5. **Communicate progress** - Weekly status updates 6. **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: 1. **Data-Driven** - Configuration in database/JSON, not code 2. **Modular** - Small, focused components with clear boundaries 3. **Reusable** - Hooks for logic, Lua for runtime-modifiable operations 4. **Testable** - Each piece independently testable 5. **Persistent** - State survives page reloads via database 6. **Multi-tenant** - Database schema supports multiple tenants --- ## ๐Ÿ“‹ Document Checklist - [x] Summary of violations identified - [x] Detailed refactoring plan for each violation - [x] Database schema design - [x] Hook API specifications - [x] Component decomposition plan - [x] Lua script definitions - [x] Testing strategy - [x] Implementation checklist - [x] Quick reference guide - [x] Timeline and effort estimates - [x] Risk mitigation strategies - [x] 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! ๐Ÿš€