-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Overview
The current /workspaces/app-preview-generator/v002/app/page.tsx
file is a monolithic component with 1779 lines of code. This issue proposes refactoring the codebase to be more composable and modular without breaking any existing functionality or making assumptions about implementation details.
Current State Analysis
File Statistics
- Lines of Code: 1779 lines
- Single Component: Everything is in one
Home
component - Mixed Concerns: UI components, business logic, state management, event handlers, and utility functions are all combined
Identified Areas for Modularization
-
State Management (Lines 21-90)
- Screen state
- Drag states (device, text, asset, screenshot)
- UI state (currentScreen, deviceType, showGrid)
-
Event Handlers & Drag Logic (Lines 136-395)
- Mouse event handlers for dragging
- Separate drag systems for screenshots, text, assets
- Complex useEffect hooks for managing drag behavior
-
File Operations (Lines 399-464, 578-620)
- Image upload handling
- Asset upload handling
- Project save/load functionality
-
Utility Functions (Lines 515-670)
- Export functions
- Transform calculations
- Style generation
-
UI Components (Lines 673-1778)
- Preview canvas
- Device mockup
- Screen selector with thumbnails
- Control panels (4 tabs: Content, Style, Position, Export)
- Form controls and inputs
Proposed Modular Structure
Components to Extract
-
components/preview/DeviceMockup.tsx
- Device frame rendering
- Background styles
- Transform calculations
-
components/preview/ScreenshotLayer.tsx
- Screenshot rendering
- Multiple screenshot support
- Legacy single screenshot fallback
-
components/preview/TextOverlay.tsx
- Text overlay rendering
- Overlay styles
- Text positioning
-
components/preview/ImageAssets.tsx
- Additional image asset rendering
- Asset positioning and styling
-
components/screens/ScreenSelector.tsx
- Screen thumbnails
- Screen navigation
- Add/remove screen functionality
-
components/controls/ContentTab.tsx
- Screenshot management
- Image asset management
- Text content controls
-
components/controls/StyleTab.tsx
- Background style controls
- Color pickers
- Device position controls
- Layer order controls
-
components/controls/PositionTab.tsx
- Screenshot position controls
- Text position controls
- Quick positioning buttons
-
components/controls/ExportTab.tsx
- Device type selection
- Export controls
- Save/load project functionality
Hooks to Create
-
hooks/useDragAndDrop.ts
- Generic drag handling logic
- Mouse event management
- Cursor and selection management
-
hooks/useScreenManagement.ts
- Screen CRUD operations
- Current screen state
- Screen navigation
-
hooks/useFileOperations.ts
- Image upload handling
- Project save/load
- Export functionality
-
hooks/useScreenshotManagement.ts
- Screenshot CRUD operations
- Multiple screenshot handling
- Legacy compatibility
Utilities to Extract
-
utils/deviceTransforms.ts
- Device position transforms
- Device sizing calculations
-
utils/backgroundStyles.ts
- Background style generation
- Pattern generation
- Gradient calculations
-
utils/export.ts
- Canvas export logic
- Image sizing and formatting
- Batch export functionality
State Management Considerations
- Context or State Management Library
- Consider using React Context for shared state
- Or implement a state management solution (Zustand, Jotai)
- Keep local state where appropriate
Benefits of Modularization
- Maintainability: Easier to locate and fix bugs
- Reusability: Components can be reused in other parts of the application
- Testing: Smaller units are easier to test
- Performance: Potential for better React rendering optimization
- Collaboration: Multiple developers can work on different components
- Code Organization: Clear separation of concerns
- Type Safety: Better TypeScript inference with smaller components
Implementation Guidelines
Requirements
- ✅ No functionality should be broken
- ✅ All existing features must continue to work
- ✅ No assumptions about implementation details
- ✅ Preserve all existing state management logic
- ✅ Maintain backward compatibility with saved projects
- ✅ Keep all drag-and-drop functionality intact
Best Practices to Follow
- Start with extracting pure presentation components
- Move to extracting hooks for logic reuse
- Create utilities for pure functions
- Test each extraction thoroughly
- Maintain proper TypeScript types throughout
- Document component interfaces clearly
Testing Strategy
- Unit Tests: For individual components and utilities
- Integration Tests: For hook interactions
- E2E Tests: Ensure full functionality remains intact
- Visual Regression: Ensure UI remains pixel-perfect
Migration Path
Phase 1: Extract Utilities
- Move pure functions to utility files
- No component changes needed
Phase 2: Extract Simple Components
- Start with leaf components (no children)
- Move presentation-only components
Phase 3: Extract Complex Components
- Extract components with local state
- Maintain prop drilling initially
Phase 4: Implement Hooks
- Extract reusable logic into hooks
- Reduce code duplication
Phase 5: Optimize State Management
- Consider state management solution
- Reduce prop drilling if needed
Acceptance Criteria
- File is split into logical, reusable components
- Each component/hook/utility has a single responsibility
- No functionality is lost or broken
- Code is well-organized and easy to navigate
- TypeScript types are properly maintained
- Performance is not negatively impacted
- All existing projects can still be loaded
- Documentation is updated to reflect new structure
Additional Notes
The current implementation works well but the monolithic structure makes it challenging to:
- Add new features without increasing complexity
- Debug specific functionality
- Onboard new developers
- Optimize performance for specific parts
This refactoring should be done incrementally to minimize risk and ensure stability throughout the process.
🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com