# ComponentManager Refactoring Plan
## Current State
- File: `src/components/manager.rs` (1182 lines)
- Responsibilities:
1. Event handling/routing (~462 lines)
2. Component management (~100 lines)
3. Rendering functions (~417 lines)
4. Utility functions (~76 lines)
## Refactoring Strategy
### Goal
Split `manager.rs` into focused modules without changing any business logic.
### Proposed Structure
```
components/
├── manager/
│ ├── mod.rs # ComponentManager struct + coordinator logic (~200 lines)
│ ├── event_handlers.rs # All event handling logic (~462 lines)
│ ├── renderers.rs # All rendering functions (~417 lines)
│ └── utils.rs # Utility functions (~76 lines)
├── manager.rs # (deprecated, will be removed)
└── ... (other components)
```
### Module Breakdown
#### 1. `manager/mod.rs` (~200 lines)
**Responsibilities:**
- ComponentManager struct definition
- Component initialization (`new()`)
- Component lifecycle methods (`active_mut()`, `switch_to()`)
- Main coordinator methods:
- `handle_event()` - delegates to event_handlers
- `update()` - component update coordination
- `render()` - delegates to renderers
- Pane navigation helpers (`next_pane()`, `prev_pane()`, `target_pane()`)
- Main layout rendering (`render_main()`, `render_single()`)
**Public API:**
- `ComponentManager::new()`
- `ComponentManager::handle_event()`
- `ComponentManager::update()`
- `ComponentManager::render()`
- `ComponentManager::sync_status_selection()`
#### 2. `manager/event_handlers.rs` (~462 lines)
**Responsibilities:**
- All event handling logic extracted from `handle_event()`
- Mode-specific handlers:
- `handle_commit_mode()`
- `handle_branch_create_mode()`
- `handle_theme_picker()`
- `handle_command_palette()`
- `handle_rebase_todo()`
- `handle_conflicts_popup()`
- `handle_conflicts_guided()`
- `handle_pr_helper()`
- `handle_merge_log()`
- `handle_merge_base_picker()`
- `handle_diff_focus()`
- `handle_help()`
- `handle_global_keys()`
- `handle_active_component()`
**Public API:**
- `handle_event()` - main event router that calls appropriate handlers
#### 3. `manager/renderers.rs` (~417 lines)
**Responsibilities:**
- All rendering functions:
- `render_palette()`
- `render_theme_picker()`
- `render_op_log()`
- `render_merge_base_picker()`
- `render_conflicts_popup()`
- `render_conflicts_guided()`
- `render_pr_helper()`
- `render_rebase_todo()`
- `render_merge_log()`
- `render_commit_input()`
- `render_branch_create_input()`
- `render_feedback()`
**Public API:**
- Individual render functions (called from ComponentManager::render())
#### 4. `manager/utils.rs` (~76 lines)
**Responsibilities:**
- Utility functions:
- `center_rect()` - calculate centered rectangle
- `palette_filtered()` - filter palette commands
**Public API:**
- Utility functions (used by other manager modules)
### Implementation Steps
1. **Create directory structure**
- Create `components/manager/` directory
- Create `mod.rs`, `event_handlers.rs`, `renderers.rs`, `utils.rs`
2. **Extract utilities first** (lowest dependency)
- Move `center_rect()` and `palette_filtered()` to `utils.rs`
- Test compilation
3. **Extract renderers** (depends on utils)
- Move all `render_*()` functions to `renderers.rs`
- Move `render_feedback()` to `renderers.rs`
- Update imports in `mod.rs`
- Test compilation
4. **Extract event handlers** (depends on utils)
- Move all event handling logic to `event_handlers.rs`
- Create helper functions for each mode
- Update `handle_event()` in `mod.rs` to delegate
- Test compilation
5. **Refactor mod.rs** (coordinator)
- Keep ComponentManager struct
- Keep component management methods
- Update `handle_event()` to call event_handlers
- Update `render()` to call renderers
- Keep `sync_status_selection()` (needs git_service access)
6. **Update module exports**
- Update `components/mod.rs` to export from `manager/mod.rs`
- Remove old `manager.rs` file
7. **Verify**
- Run tests
- Check compilation
- Verify no behavior changes
### Key Principles
1. **No business logic changes** - Only structural refactoring
2. **Maintain existing public API** - ComponentManager interface unchanged
3. **Preserve all functionality** - Every handler and renderer must work identically
4. **Clear module boundaries** - Each module has a single, clear responsibility
5. **Minimal dependencies** - Modules only depend on what they need
### Benefits
1. **Reduced file size** - Main file goes from 1182 to ~200 lines
2. **Better organization** - Related code grouped together
3. **Easier maintenance** - Find and modify specific functionality
4. **Improved testability** - Individual handlers/renderers can be tested
5. **Clearer structure** - New contributors can understand codebase faster
### Risks & Mitigation
1. **Risk**: Breaking existing functionality
- **Mitigation**: Incremental extraction, test after each step
2. **Risk**: Circular dependencies
- **Mitigation**: Clear dependency hierarchy (utils → handlers/renderers → mod.rs)
3. **Risk**: Performance impact
- **Mitigation**: No runtime changes, only compile-time reorganization
### Testing Strategy
1. After each extraction step, verify:
- Code compiles
- No linter errors
- Application runs
- Key features work (commit, branch create, theme picker, etc.)
2. Final verification:
- All existing functionality works
- No regressions
- Code is cleaner and more maintainable