# Code Size Limit Enforcement & Refactoring Guide ## ๐ฏ Overview This document outlines the code size limit enforcement strategy and refactoring approach for the MetaBuilder codebase. ## ๐ Size Limits Based on the project's architectural principles, the following limits are enforced: ### TypeScript/React Files (`.ts`, `.tsx`) | Metric | Limit | Severity | |--------|-------|----------| | Lines of Code (LOC) | 150 | โ Error | | Total Lines | 200 | โ ๏ธ Warning | | Max Nesting Depth | 3 | โ ๏ธ Warning | | Exports per File | 5 (components), 10 (utilities) | โ ๏ธ Warning | | Function Parameters | 5 | โ ๏ธ Warning | ### JavaScript Files (`.js`, `.jsx`) Same limits as TypeScript files. ## ๐ ๏ธ Enforcement Tool ### Location ```bash scripts/enforce-size-limits.ts ``` ### Usage ```bash # Run the size limit checker npx tsx scripts/enforce-size-limits.ts # Output # - Console report with violations # - JSON report (size-limits-report.json) for CI/CD ``` ### Features - Scans all TypeScript/JavaScript files (excludes node_modules, build, .next, etc.) - Counts actual lines of code (excludes comments and blank lines) - Detects nesting depth issues - Identifies function parameter count violations - Generates both human-readable and machine-readable reports ## โป๏ธ Refactoring Strategy ### Core Principles 1. **Separation of Concerns** - Each component should have ONE primary responsibility - Extract business logic into custom hooks - Move persistent state to database 2. **Hook-Based Architecture** - `useGitHubFetcher`: API integration and data fetching - `useAutoRefresh`: Polling and timer management - `useFileTree`: Tree data structure operations - `useCodeEditor`: Editor state and operations - Custom hooks for domain-specific logic 3. **Component Hierarchy** - Small, reusable UI components (<100 LOC) - Container components that use hooks (<150 LOC) - Avoid deeply nested JSX 4. **Data Flow** ``` Persistent Database โ Custom Hooks (logic) โ Container Components (state management) โ UI Components (rendering) ``` ## ๐ Refactoring Artifacts ### New Hooks Created ``` src/hooks/ โโโ useGitHubFetcher.ts # GitHub API integration โโโ useAutoRefresh.ts # Auto-refresh polling โโโ useCodeEditor.ts # Editor state management โโโ useFileTree.ts # File tree operations โโโ index.ts # Barrel export ``` ### New Components Created ``` src/components/ โโโ WorkflowRunCard.tsx # Extracted from GitHubActionsFetcher โโโ GitHubActionsFetcher.refactored.tsx # Refactored version โโโ ... (more to follow) ``` ## ๐ Refactoring Checklist ### Phase 1: Foundation (Week 1) - [x] Create size limit enforcement tool - [x] Create custom hooks for common patterns - [x] Create small, reusable UI components - [ ] Replace original GitHubActionsFetcher with refactored version - [ ] Add unit tests for hooks ### Phase 2: Large Components (Weeks 2-3) - [ ] Refactor NerdModeIDE.tsx (733 LOC) - [ ] Refactor LuaEditor.tsx (686 LOC) - [ ] Refactor PackageImportExport.tsx (638 LOC) ### Phase 3: Medium Components (Week 4) - [ ] Refactor PackageManager.tsx (558 LOC) - [ ] Refactor WorkflowEditor.tsx (498 LOC) - [ ] Refactor ComponentHierarchyEditor.tsx (477 LOC) ### Phase 4: Verification & Tooling (Ongoing) - [ ] Run size limit checker on all files - [ ] Set up pre-commit hook to prevent new violations - [ ] Add CI/CD check for size limits - [ ] Document refactored components ## ๐ Hook Usage Examples ### useGitHubFetcher ```tsx function MyComponent() { const { data, isLoading, error, fetch } = useGitHubFetcher() useEffect(() => { fetch() }, []) return