Skip to content

Commit b0066e5

Browse files
authored
feat: create context for sidebarPages (#2827)
Creates the `OutlineSidebarPagesContext` so we can add new pages using plugins, as in openedx/frontend-plugin-aspects#115
1 parent 6558c2b commit b0066e5

4 files changed

Lines changed: 91 additions & 8 deletions

File tree

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"@openedx-plugins/course-app-wiki": "file:plugins/course-apps/wiki",
6161
"@openedx-plugins/course-app-xpert_unit_summary": "file:plugins/course-apps/xpert_unit_summary",
6262
"@openedx/frontend-build": "^14.6.2",
63-
"@openedx/frontend-plugin-framework": "^1.7.0",
63+
"@openedx/frontend-plugin-framework": "^1.8.0",
6464
"@openedx/paragon": "^23.5.0",
6565
"@redux-devtools/extension": "^3.3.0",
6666
"@reduxjs/toolkit": "2.11.2",

src/course-outline/outline-sidebar/OutlineSidebar.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import { useMediaQuery } from 'react-responsive';
33

44
import { Sidebar } from '@src/generic/sidebar';
55

6+
import { isOutlineNewDesignEnabled } from '../utils';
67
import OutlineHelpSidebar from './OutlineHelpSidebar';
78
import { useOutlineSidebarContext } from './OutlineSidebarContext';
8-
import { isOutlineNewDesignEnabled } from '../utils';
9-
import { getOutlineSidebarPages } from './sidebarPages';
9+
import { useOutlineSidebarPagesContext } from './OutlineSidebarPagesContext';
1010

1111
const OutlineSideBar = () => {
1212
const isMedium = useMediaQuery({ maxWidth: breakpoints.medium.maxWidth });
13-
const sidebarPages = getOutlineSidebarPages();
1413

1514
const {
1615
currentPageKey,
@@ -19,6 +18,8 @@ const OutlineSideBar = () => {
1918
toggle,
2019
} = useOutlineSidebarContext();
2120

21+
const sidebarPages = useOutlineSidebarPagesContext();
22+
2223
// Returns the previous help sidebar component if the waffle flag is disabled
2324
if (!isOutlineNewDesignEnabled()) {
2425
// On screens smaller than medium, the help sidebar is shown below the course outline
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { createContext, useContext } from 'react';
2+
import { getConfig } from '@edx/frontend-platform';
3+
import {
4+
HelpOutline, Info, Plus, Tag,
5+
} from '@openedx/paragon/icons';
6+
7+
import type { SidebarPage } from '@src/generic/sidebar';
8+
9+
import { AddSidebar } from './AddSidebar';
10+
import { OutlineAlignSidebar } from './OutlineAlignSidebar';
11+
import OutlineHelpSidebar from './OutlineHelpSidebar';
12+
import { OutlineInfoSidebar } from './OutlineInfoSidebar';
13+
import messages from './messages';
14+
15+
export type OutlineSidebarPages = {
16+
info: SidebarPage;
17+
help: SidebarPage;
18+
add: SidebarPage;
19+
align?: SidebarPage;
20+
};
21+
22+
const showAlignSidebar = getConfig().ENABLE_TAGGING_TAXONOMY_PAGES === 'true';
23+
24+
const OUTLINE_SIDEBAR_PAGES: OutlineSidebarPages = {
25+
info: {
26+
component: OutlineInfoSidebar,
27+
icon: Info,
28+
title: messages.sidebarButtonInfo,
29+
},
30+
...(showAlignSidebar && {
31+
align: {
32+
component: OutlineAlignSidebar,
33+
icon: Tag,
34+
title: messages.sidebarButtonAlign,
35+
},
36+
}),
37+
help: {
38+
component: OutlineHelpSidebar,
39+
icon: HelpOutline,
40+
title: messages.sidebarButtonHelp,
41+
},
42+
add: {
43+
component: AddSidebar,
44+
icon: Plus,
45+
title: messages.sidebarButtonAdd,
46+
},
47+
};
48+
49+
/**
50+
* Context for the Outline Sidebar Pages.
51+
*
52+
* This could be used in plugins to add new pages to the sidebar.
53+
*
54+
* @example
55+
*
56+
* ```tsx
57+
* export function CourseOutlineSidebarWrapper(
58+
* { component, pluginProps }: { component: React.ReactNode, pluginProps: CourseOutlineAspectsPageProps },
59+
* ) {
60+
* const sidebarPages = useOutlineSidebarPagesContext();
61+
*
62+
* const AnalyticsPage = React.useCallback(() => <CourseOutlineAspectsPage {...pluginProps} />, [pluginProps]);
63+
*
64+
* const overridedPages = useMemo(() => ({
65+
* ...sidebarPages,
66+
* analytics: {
67+
* component: AnalyticsPage,
68+
* icon: AutoGraph,
69+
* title: messages.analyticsLabel,
70+
* },
71+
* }), [sidebarPages, AnalyticsPage]);
72+
*
73+
* return (
74+
* <OutlineSidebarPagesContext.Provider value={overridedPages}>
75+
* {component}
76+
* </OutlineSidebarPagesContext.Provider>
77+
* );
78+
*}
79+
*/
80+
export const OutlineSidebarPagesContext = createContext<OutlineSidebarPages>(OUTLINE_SIDEBAR_PAGES);
81+
82+
export const useOutlineSidebarPagesContext = (): OutlineSidebarPages => useContext(OutlineSidebarPagesContext);

0 commit comments

Comments
 (0)