import { LoadingSpinner } from '@src/generic/Loading'; import { useCourseAuthoringContext } from '@src/CourseAuthoringContext'; import { useParams, Navigate } from 'react-router-dom'; import { useCourseItemData } from '../course-outline/data/apiHooks'; const SubsectionUnitRedirect = () => { const { courseId } = useCourseAuthoringContext(); let { subsectionId } = useParams(); // if the call is made via the click on breadcrumbs the re won't be courseId available // in such cases the page should redirect to the 1st unit of he subsection const { data: courseItemData, isLoading } = useCourseItemData(subsectionId); let firstUnitId = courseItemData?.childInfo?.children?.[0]?.id; if (isLoading) { return ; } if (firstUnitId) { firstUnitId = encodeURIComponent(firstUnitId); return ; } if (subsectionId) { // if no unit then navigate to the subsection outline subsectionId = encodeURIComponent(subsectionId); return ; } // navigate to the course page if no subsectionId and no unitId return ; }; export default SubsectionUnitRedirect;