|
| 1 | +import { useQuery } from '@tanstack/react-query'; |
| 2 | +import { useIntl } from '../../../runtime'; |
| 3 | +import classNames from 'classnames'; |
| 4 | +import { getCourseHomeCourseMetadata } from './data/service'; |
| 5 | +import { Tab, Tabs } from '@openedx/paragon'; |
| 6 | +import messages from './messages'; |
| 7 | +import { useNavigate, useLocation } from 'react-router-dom'; |
| 8 | +import './course-tabs-navigation.scss'; |
| 9 | + |
| 10 | +interface CourseMetaData { |
| 11 | + tabs: { |
| 12 | + title: string, |
| 13 | + slug: string, |
| 14 | + url: string, |
| 15 | + }[], |
| 16 | + isMasquerading: boolean, |
| 17 | +} |
| 18 | + |
| 19 | +const CourseTabsNavigation = () => { |
| 20 | + const location = useLocation(); |
| 21 | + const intl = useIntl(); |
| 22 | + const navigate = useNavigate(); |
| 23 | + |
| 24 | + const extractCourseId = (pathname: string): string => { |
| 25 | + const courseRegex = /\/courses?\/([^/]+)/; |
| 26 | + const courseMatch = courseRegex.exec(pathname); |
| 27 | + return courseMatch ? courseMatch[1] : ''; |
| 28 | + }; |
| 29 | + |
| 30 | + const courseId = extractCourseId(location.pathname); |
| 31 | + |
| 32 | + const { data } = useQuery({ |
| 33 | + queryKey: ['org.openedx.frontend.app.header.course-meta', courseId], |
| 34 | + queryFn: () => getCourseHomeCourseMetadata(courseId), |
| 35 | + retry: 2, |
| 36 | + }); |
| 37 | + |
| 38 | + if (!courseId) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + const { tabs = [] }: CourseMetaData = data ?? {}; |
| 43 | + |
| 44 | + const handleSelectedTab = (eventKey) => { |
| 45 | + const selectedUrl = tabs.find(tab => tab.slug === eventKey)?.url ?? '/'; |
| 46 | + |
| 47 | + try { |
| 48 | + if (selectedUrl.startsWith('http://') || selectedUrl.startsWith('https://')) { |
| 49 | + const url = new URL(selectedUrl); |
| 50 | + if (url.origin === window.location.origin) { |
| 51 | + navigate(url.pathname + url.search + url.hash); |
| 52 | + } else { |
| 53 | + window.location.href = selectedUrl; |
| 54 | + } |
| 55 | + } else { |
| 56 | + navigate(selectedUrl); |
| 57 | + } |
| 58 | + } catch (error) { |
| 59 | + navigate(selectedUrl); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + return ( |
| 64 | + <div id="courseTabsNavigation" className={classNames('course-tabs-navigation')}> |
| 65 | + <div className="container-xl"> |
| 66 | + <div className="nav-bar"> |
| 67 | + <div className="nav-menu"> |
| 68 | + <Tabs className="nav-underline-tabs" aria-label={intl.formatMessage(messages.courseMaterial)} onSelect={handleSelectedTab}> |
| 69 | + {tabs.map(({ title, slug }) => ( |
| 70 | + <Tab eventKey={slug} title={title} key={slug} /> |
| 71 | + ))} |
| 72 | + </Tabs> |
| 73 | + </div> |
| 74 | + {/* <div className="search-toggle"> |
| 75 | + <CoursewareSearchToggle /> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + {show && <CoursewareSearch />} */} |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + ); |
| 84 | +}; |
| 85 | + |
| 86 | +export default CourseTabsNavigation; |
0 commit comments