# 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 (