4.7 KiB
4.7 KiB
Scheduling Component Refactoring Proposal
Current State Analysis
The ScheduleExperiment component is approximately 1,140 lines and handles multiple responsibilities:
-
Conductor Management (~150 lines)
- Fetching conductors
- Selection state
- Color mapping
- Availability fetching
-
Phase/Experiment/Repetition Management (~300 lines)
- Phase expansion/collapse
- Experiment loading
- Repetition creation/selection
- Soaking/airdrying data fetching
-
Calendar Event Management (~200 lines)
- Event generation
- Drag and drop handling
- Availability events
- Styling and theming
-
Scheduling Logic (~300 lines)
- Repetition spawning
- Staggering calculations
- Phase timing updates
- Lock/unlock functionality
- Database persistence
-
UI Rendering (~200 lines)
- Conductor panel
- Phase/Experiment/Repetition tree
- Calendar component
- Loading/error states
Proposed Structure
Component Hierarchy
ScheduleExperiment (Main Container - ~150 lines)
├── ConductorPanel (UI Component - ~100 lines)
├── ExperimentPhasePanel (UI Component - ~150 lines)
│ ├── ExperimentItem (UI Component - ~80 lines)
│ │ └── RepetitionItem (UI Component - ~120 lines)
└── SchedulingCalendar (UI Component - ~150 lines)
Custom Hooks
hooks/
├── useConductors.ts (~100 lines)
│ - Conductor fetching
│ - Selection state
│ - Color mapping
│ - Availability fetching
│
├── useExperimentPhases.ts (~150 lines)
│ - Phase/Experiment/Repetition data fetching
│ - Expansion state
│ - Selection state
│ - Soaking/airdrying data
│
├── useScheduling.ts (~200 lines)
│ - Repetition scheduling state
│ - Spawn/stagger logic
│ - Phase timing updates
│ - Lock/unlock functionality
│ - Database persistence
│
└── useCalendarEvents.ts (~150 lines)
- Calendar event generation
- Drag and drop handlers
- Event styling
- Scroll preservation
File Structure
scheduling-remote/src/components/
├── Scheduling.tsx (Main router - unchanged)
├── ScheduleExperiment/
│ ├── index.tsx (Main container - ~150 lines)
│ ├── ConductorPanel.tsx (~100 lines)
│ ├── ExperimentPhasePanel.tsx (~150 lines)
│ ├── ExperimentItem.tsx (~80 lines)
│ ├── RepetitionItem.tsx (~120 lines)
│ └── SchedulingCalendar.tsx (~150 lines)
└── hooks/
├── useConductors.ts (~100 lines)
├── useExperimentPhases.ts (~150 lines)
├── useScheduling.ts (~200 lines)
└── useCalendarEvents.ts (~150 lines)
Benefits
- Maintainability: Each component has a single, clear responsibility
- Testability: Smaller units are easier to test in isolation
- Reusability: Components and hooks can be reused elsewhere
- Readability: Easier to understand and navigate
- Performance: Better optimization opportunities with smaller components
- Collaboration: Multiple developers can work on different parts simultaneously
Refactoring Strategy
Phase 1: Extract Custom Hooks (Low Risk)
- Extract
useConductorshook - Extract
useExperimentPhaseshook - Extract
useSchedulinghook - Extract
useCalendarEventshook - Test each hook independently
Phase 2: Extract UI Components (Medium Risk)
- Extract
ConductorPanelcomponent - Extract
ExperimentPhasePanelcomponent - Extract
ExperimentItemcomponent - Extract
RepetitionItemcomponent - Extract
SchedulingCalendarcomponent - Test component integration
Phase 3: Refactor Main Component (Low Risk)
- Update
ScheduleExperimentto use extracted hooks and components - Remove duplicate code
- Final integration testing
Best Practices Applied
- Single Responsibility Principle: Each component/hook has one clear purpose
- Custom Hooks for Logic: Business logic separated from UI
- Props Drilling vs Context: Use props for direct parent-child, context only if needed
- Memoization: Preserve existing
useMemo/useCallbackoptimizations - Type Safety: Maintain all TypeScript types
- State Management: Keep related state together in hooks
Migration Path
- Backward Compatible: All functionality preserved
- Incremental: Can be done in phases
- Testable: Each phase can be tested independently
- Reversible: Changes can be rolled back if needed
Estimated Impact
- Main Component: 1,140 lines → ~150 lines (87% reduction)
- Total Lines: ~1,140 → ~1,200 (slight increase due to imports/exports, but much better organized)
- Complexity: Significantly reduced per file
- Maintainability: Dramatically improved