# CourseWorx Component Architecture Documentation ## ๐Ÿ“‹ Document Information - **Version**: 1.0.0 - **Last Updated**: 2024-12-19 - **Author**: AI Assistant - **Status**: Draft - Ready for Review ## ๐ŸŽฏ Frontend Architecture Overview CourseWorx frontend is built with React 18 using a component-based architecture. The application follows a hierarchical structure with clear separation of concerns between pages, components, and services. ## ๐Ÿ—๏ธ Component Structure ### Directory Structure ``` frontend/src/ โ”œโ”€โ”€ components/ # Reusable UI components โ”œโ”€โ”€ pages/ # Page-level components โ”œโ”€โ”€ contexts/ # React Context providers โ”œโ”€โ”€ services/ # API and external services โ”œโ”€โ”€ utils/ # Utility functions โ””โ”€โ”€ App.js # Main application component ``` ## ๐Ÿ“ฑ Page Components Analysis ### 1. CourseContent.js โš ๏ธ **CRITICAL ISSUE** - **Size**: 1,736 lines - **Complexity**: Extremely high - **Issues**: - Mixed concerns (CRUD, UI, state management) - Multiple modals in single component - Complex state management - Difficult to maintain and test **Recommended Refactoring**: ``` CourseContent.js (1,736 lines) โ†’ Split into: โ”œโ”€โ”€ CourseContentManager.js (Main container) โ”œโ”€โ”€ ContentList.js (Content display) โ”œโ”€โ”€ AddContentModal.js (Content creation) โ”œโ”€โ”€ EditContentModal.js (Content editing) โ”œโ”€โ”€ QuizQuestionsModal.js (Quiz management) โ”œโ”€โ”€ SectionManager.js (Section CRUD) โ””โ”€โ”€ ContentDragDrop.js (Reordering) ``` ### 2. CourseContentViewer.js โš ๏ธ **NEEDS ATTENTION** - **Size**: 835 lines - **Issues**: - Large component with mixed concerns - Complex state management - Multiple responsibilities **Recommended Refactoring**: ``` CourseContentViewer.js (835 lines) โ†’ Split into: โ”œโ”€โ”€ ContentViewer.js (Main container) โ”œโ”€โ”€ ContentRenderer.js (Content display) โ”œโ”€โ”€ ProgressTracker.js (Progress management) โ”œโ”€โ”€ QuizRenderer.js (Quiz display) โ””โ”€โ”€ NavigationControls.js (Content navigation) ``` ### 3. Users.js โš ๏ธ **NEEDS ATTENTION** - **Size**: 496 lines - **Issues**: - User management mixed with UI - Complex filtering and pagination - Multiple modals **Recommended Refactoring**: ``` Users.js (496 lines) โ†’ Split into: โ”œโ”€โ”€ UserManager.js (Main container) โ”œโ”€โ”€ UserList.js (User display) โ”œโ”€โ”€ UserFilters.js (Filtering) โ”œโ”€โ”€ AddUserModal.js (User creation) โ””โ”€โ”€ UserActions.js (User operations) ``` ### 4. Well-Structured Pages โœ… - **Login.js** (419 lines): Well-structured authentication - **Dashboard.js** (396 lines): Good separation of concerns - **CourseDetail.js** (424 lines): Appropriate size and structure ## ๐Ÿงฉ Reusable Components Analysis ### 1. Layout.js โœ… **GOOD STRUCTURE** - **Size**: 163 lines - **Purpose**: Main application layout - **Structure**: Clean navigation and content area ### 2. CourseSidebar.js โœ… **GOOD STRUCTURE** - **Size**: 346 lines - **Purpose**: Course navigation sidebar - **Features**: Collapsible sections, progress tracking ### 3. LoadingSpinner.js โœ… **EXCELLENT** - **Size**: 18 lines - **Purpose**: Loading state indicator - **Structure**: Simple, focused, reusable ## ๐Ÿ”„ State Management Analysis ### Current State Management ``` Global State: โ”œโ”€โ”€ AuthContext (Authentication) โ”œโ”€โ”€ React Query (Server state) โ””โ”€โ”€ Local Component State Issues: โ”œโ”€โ”€ No centralized state management โ”œโ”€โ”€ Props drilling in large components โ””โ”€โ”€ Complex local state in large components ``` ### Recommended Improvements ``` Proposed State Management: โ”œโ”€โ”€ AuthContext (Keep as is) โ”œโ”€โ”€ React Query (Expand usage) โ”œโ”€โ”€ Context for UI state โ””โ”€โ”€ Reduced local state ``` ## ๐Ÿ”Œ Service Layer Analysis ### API Service Structure โœ… **GOOD DESIGN** ``` frontend/src/services/api.js: โ”œโ”€โ”€ authAPI โ”œโ”€โ”€ usersAPI โ”œโ”€โ”€ coursesAPI โ”œโ”€โ”€ courseContentAPI โ”œโ”€โ”€ courseSectionAPI โ”œโ”€โ”€ enrollmentsAPI โ”œโ”€โ”€ attendanceAPI โ”œโ”€โ”€ assignmentsAPI โ”œโ”€โ”€ lessonCompletionAPI โ”œโ”€โ”€ courseStatsAPI โ””โ”€โ”€ userNotesAPI ``` **Strengths**: - Clear separation by domain - Consistent naming convention - Proper error handling - Request/response interceptors ## ๐ŸŽจ Styling Architecture ### Current Approach โœ… **GOOD** - **Tailwind CSS**: Utility-first approach - **Consistent Design**: Good use of design tokens - **Responsive**: Mobile-first design ### Component Styling Patterns ``` Consistent Patterns: โ”œโ”€โ”€ Button styles: bg-blue-600 hover:bg-blue-700 โ”œโ”€โ”€ Form styles: Consistent input styling โ”œโ”€โ”€ Card styles: bg-white shadow rounded-lg โ””โ”€โ”€ Text styles: Consistent typography scale ``` ## ๐Ÿงช Component Testing Analysis ### Current Testing Status โŒ **CRITICAL GAP** - **Unit Tests**: None found - **Integration Tests**: None found - **Component Tests**: None found - **E2E Tests**: None found ### Recommended Testing Structure ``` Proposed Testing: โ”œโ”€โ”€ components/__tests__/ โ”‚ โ”œโ”€โ”€ LoadingSpinner.test.js โ”‚ โ”œโ”€โ”€ Layout.test.js โ”‚ โ””โ”€โ”€ CourseSidebar.test.js โ”œโ”€โ”€ pages/__tests__/ โ”‚ โ”œโ”€โ”€ Login.test.js โ”‚ โ”œโ”€โ”€ Dashboard.test.js โ”‚ โ””โ”€โ”€ CourseDetail.test.js โ””โ”€โ”€ services/__tests__/ โ””โ”€โ”€ api.test.js ``` ## ๐Ÿ”ง Component Performance Analysis ### Performance Issues Identified 1. **Large Bundle Size**: CourseContent.js affects initial load 2. **No Code Splitting**: All components loaded upfront 3. **No Memoization**: Potential re-render issues 4. **Large Component Re-renders**: Expensive updates ### Performance Recommendations ``` Performance Improvements: โ”œโ”€โ”€ Code Splitting: React.lazy() for large components โ”œโ”€โ”€ Memoization: React.memo for expensive components โ”œโ”€โ”€ Bundle Analysis: webpack-bundle-analyzer โ””โ”€โ”€ Virtual Scrolling: For large lists ``` ## ๐Ÿ“Š Component Complexity Metrics ### Component Size Analysis | Component | Lines | Complexity | Status | |-----------|-------|------------|---------| | CourseContent.js | 1,736 | Very High | โŒ Refactor Required | | CourseContentViewer.js | 835 | High | โš ๏ธ Needs Attention | | Users.js | 496 | Medium-High | โš ๏ธ Needs Attention | | CourseEdit.js | 514 | Medium-High | โš ๏ธ Consider Refactoring | | CourseCreate.js | 482 | Medium | โœ… Acceptable | | Login.js | 419 | Medium | โœ… Good Structure | ### Complexity Thresholds - **< 200 lines**: โœ… Good - **200-400 lines**: โš ๏ธ Monitor - **400-600 lines**: โš ๏ธ Consider refactoring - **> 600 lines**: โŒ Refactor required ## ๐Ÿ”ฎ Recommended Component Architecture ### 1. Atomic Design Principles ``` Proposed Structure: โ”œโ”€โ”€ atoms/ # Basic UI elements โ”‚ โ”œโ”€โ”€ Button.js โ”‚ โ”œโ”€โ”€ Input.js โ”‚ โ””โ”€โ”€ Icon.js โ”œโ”€โ”€ molecules/ # Simple component groups โ”‚ โ”œโ”€โ”€ SearchBox.js โ”‚ โ”œโ”€โ”€ UserCard.js โ”‚ โ””โ”€โ”€ CourseCard.js โ”œโ”€โ”€ organisms/ # Complex component groups โ”‚ โ”œโ”€โ”€ Header.js โ”‚ โ”œโ”€โ”€ Sidebar.js โ”‚ โ””โ”€โ”€ ContentList.js โ”œโ”€โ”€ templates/ # Page layouts โ”‚ โ”œโ”€โ”€ DashboardLayout.js โ”‚ โ””โ”€โ”€ CourseLayout.js โ””โ”€โ”€ pages/ # Complete pages โ”œโ”€โ”€ Dashboard.js โ””โ”€โ”€ CourseDetail.js ``` ### 2. Container/Presentation Pattern ``` Recommended Pattern: โ”œโ”€โ”€ containers/ # Logic and state โ”‚ โ”œโ”€โ”€ CourseContainer.js โ”‚ โ””โ”€โ”€ UserContainer.js โ””โ”€โ”€ components/ # UI only โ”œโ”€โ”€ CourseList.js โ””โ”€โ”€ UserList.js ``` ## ๐Ÿšจ Critical Refactoring Priorities ### Priority 1: CourseContent.js - **Impact**: High - Core functionality - **Effort**: High - Complex refactoring - **Timeline**: Immediate - 2 weeks ### Priority 2: CourseContentViewer.js - **Impact**: Medium-High - User experience - **Effort**: Medium - Moderate refactoring - **Timeline**: After Priority 1 - 1 week ### Priority 3: Users.js - **Impact**: Medium - Admin functionality - **Effort**: Medium - Moderate refactoring - **Timeline**: After Priority 2 - 1 week ## ๐Ÿ“‹ Component Development Guidelines ### 1. Component Size Guidelines - **Maximum Lines**: 300 lines per component - **Single Responsibility**: One purpose per component - **Props Limit**: Maximum 10 props per component ### 2. Naming Conventions - **Components**: PascalCase (UserList.js) - **Files**: Match component name - **Props**: camelCase - **Event Handlers**: on + Action (onUserClick) ### 3. Component Structure Template ```javascript // 1. Imports import React, { useState, useEffect } from 'react'; // 2. Component definition const ComponentName = ({ prop1, prop2, onAction }) => { // 3. State declarations const [state, setState] = useState(initialValue); // 4. Effects useEffect(() => { // Effect logic }, [dependencies]); // 5. Event handlers const handleAction = () => { // Handler logic }; // 6. Render return (
{/* JSX */}
); }; // 7. Export export default ComponentName; ``` --- **Next Steps**: 1. Prioritize CourseContent.js refactoring 2. Implement component testing strategy 3. Establish component development guidelines 4. Create component library documentation