import { useEffect } from 'react'; import { useIntl } from '@edx/frontend-platform/i18n'; import { Breadcrumb, Container, } from '@openedx/paragon'; import { Helmet } from 'react-helmet'; import { Link } from 'react-router-dom'; import Loading from '../../generic/Loading'; import NotFoundAlert from '../../generic/NotFoundAlert'; import SubHeader from '../../generic/sub-header/SubHeader'; import ErrorAlert from '../../generic/alert-error'; import Header from '../../header'; import { useLibraryContext } from '../common/context/LibraryContext'; import { COLLECTION_INFO_TABS, COMPONENT_INFO_TABS, UNIT_INFO_TABS, useSidebarContext, } from '../common/context/SidebarContext'; import { useContainer, useContentLibrary } from '../data/apiHooks'; import { LibrarySidebar } from '../library-sidebar'; import { SubHeaderTitle } from '../LibraryAuthoringPage'; import { LibraryUnitBlocks } from './LibraryUnitBlocks'; import messages from './messages'; import { ContainerEditableTitle, FooterActions, HeaderActions } from '../containers'; import { ContainerType } from '../../generic/key-utils'; export const LibraryUnitPage = () => { const intl = useIntl(); const { libraryId, unitId, } = useLibraryContext(); // istanbul ignore if: this should never happen if (!unitId) { throw new Error('unitId is required'); } const { sidebarComponentInfo, setDefaultTab, setHiddenTabs, } = useSidebarContext(); useEffect(() => { setDefaultTab({ collection: COLLECTION_INFO_TABS.Details, component: COMPONENT_INFO_TABS.Manage, unit: UNIT_INFO_TABS.Manage, }); setHiddenTabs([COMPONENT_INFO_TABS.Preview, UNIT_INFO_TABS.Preview]); return () => { setDefaultTab({ component: COMPONENT_INFO_TABS.Preview, unit: UNIT_INFO_TABS.Preview, collection: COLLECTION_INFO_TABS.Manage, }); setHiddenTabs([]); }; }, [setDefaultTab, setHiddenTabs]); const { data: libraryData, isLoading: isLibLoading } = useContentLibrary(libraryId); const { data: unitData, isLoading, isError, error, } = useContainer(unitId); if (!unitId || !libraryId) { // istanbul ignore next - This shouldn't be possible; it's just here to satisfy the type checker. throw new Error('Rendered without unitId or libraryId URL parameter'); } // Only show loading if unit or library data is not fetched from index yet if (isLibLoading || isLoading) { return ; } if (!libraryData || !unitData) { return ; } // istanbul ignore if if (isError) { return ; } const breadcrumbs = ( ` spacer. { label: '', to: '', }, ]} linkAs={Link} /> ); return (
{libraryData.title} | {process.env.SITE_NAME}
} />} headerActions={( )} breadcrumbs={breadcrumbs} hideBorder />
{!!sidebarComponentInfo?.type && (
)}
); };