'use client' import { ChevronLeft, ChevronRight } from 'lucide-react' import { getUriWithOrg } from '@services/config/config' import { useRouter } from 'next/navigation' import React, { useEffect, useState, useRef } from 'react' import ActivityIndicators from '@components/Pages/Courses/ActivityIndicators' import ActivityChapterDropdown from './ActivityChapterDropdown' import { getCourseThumbnailMediaDirectory } from '@services/media/media' import { useOrg } from '@components/Contexts/OrgContext' interface FixedActivitySecondaryBarProps { course: any currentActivityId: string orgslug: string activity: any } export default function FixedActivitySecondaryBar(props: FixedActivitySecondaryBarProps): React.ReactNode { const router = useRouter(); const [isScrolled, setIsScrolled] = useState(false); const [shouldShow, setShouldShow] = useState(false); const mainActivityInfoRef = useRef(null); const org = useOrg() as any; // Function to find the current activity's position in the course const findActivityPosition = () => { let allActivities: any[] = []; let currentIndex = -1; // Flatten all activities from all chapters props.course.chapters.forEach((chapter: any) => { chapter.activities.forEach((activity: any) => { const cleanActivityUuid = activity.activity_uuid?.replace('activity_', ''); allActivities.push({ ...activity, cleanUuid: cleanActivityUuid, chapterName: chapter.name }); // Check if this is the current activity if (cleanActivityUuid === props.currentActivityId.replace('activity_', '')) { currentIndex = allActivities.length - 1; } }); }); return { allActivities, currentIndex }; }; const { allActivities, currentIndex } = findActivityPosition(); // Get previous and next activities const prevActivity = currentIndex > 0 ? allActivities[currentIndex - 1] : null; const nextActivity = currentIndex < allActivities.length - 1 ? allActivities[currentIndex + 1] : null; // Navigate to an activity const navigateToActivity = (activity: any) => { if (!activity) return; const cleanCourseUuid = props.course.course_uuid?.replace('course_', ''); router.push(getUriWithOrg(props.orgslug, '') + `/course/${cleanCourseUuid}/activity/${activity.cleanUuid}`); }; // Handle scroll and intersection observer useEffect(() => { const handleScroll = () => { setIsScrolled(window.scrollY > 0); }; // Set up intersection observer for the main activity info const observer = new IntersectionObserver( ([entry]) => { // Show the fixed bar when the main info is not visible setShouldShow(!entry.isIntersecting); }, { threshold: [0, 0.1, 1], rootMargin: '-80px 0px 0px 0px' // Increased margin to account for the header } ); // Start observing the main activity info section with a slight delay to ensure DOM is ready setTimeout(() => { const mainActivityInfo = document.querySelector('.activity-info-section'); if (mainActivityInfo) { mainActivityInfoRef.current = mainActivityInfo as HTMLDivElement; observer.observe(mainActivityInfo); } }, 100); window.addEventListener('scroll', handleScroll); return () => { window.removeEventListener('scroll', handleScroll); if (mainActivityInfoRef.current) { observer.unobserve(mainActivityInfoRef.current); } }; }, []); return ( <> {shouldShow && (
{/* Left Section - Course Info and Navigation */}

Course

{props.course.name}

{/* Right Section - Navigation Controls */}
{currentIndex + 1} of {allActivities.length}
)} ); }