Files
frontend-app-authoring/src/course-unit/SubsectionUnitRedirect.tsx
Ishan Masdekar 00ce3d7856 feat: navigates subsection breadcrumb to first unit page (#2329)
- navigates the breadcrumb to the first unit under the subsection
instead of the outline page.

Closes #1924
2025-07-31 14:26:18 -07:00

30 lines
1.2 KiB
TypeScript

import { LoadingSpinner } from '@src/generic/Loading';
import { useParams, Navigate } from 'react-router-dom';
import { useCourseItemData } from '../course-outline/data/apiHooks';
const SubsectionUnitRedirect = ({ courseId }: { courseId: string }) => {
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 <LoadingSpinner />;
}
if (firstUnitId) {
firstUnitId = encodeURIComponent(firstUnitId);
return <Navigate replace to={`/course/${courseId}/container/${firstUnitId}`} />;
}
if (subsectionId) {
// if no unit then navigate to the subsection outline
subsectionId = encodeURIComponent(subsectionId);
return <Navigate replace to={`/course/${courseId}?show=${subsectionId}`} />;
}
// navigate to the course page if no subsectionId and no unitId
return <Navigate replace to={`/course/${courseId}`} />;
};
export default SubsectionUnitRedirect;