-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathUnitSidebarPagesContext.tsx
More file actions
110 lines (98 loc) · 3.13 KB
/
UnitSidebarPagesContext.tsx
File metadata and controls
110 lines (98 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { createContext, useContext, useMemo } from 'react';
import { getConfig } from '@edx/frontend-platform';
import { Info, Plus, Tag } from '@openedx/paragon/icons';
import type { SidebarPage } from '@src/generic/sidebar';
import { InfoSidebar } from './unit-info/InfoSidebar';
import { AddSidebar } from './AddSidebar';
import { UnitAlignSidebar } from './UnitAlignSidebar';
import { useUnitSidebarContext } from './UnitSidebarContext';
import messages from './messages';
export type UnitSidebarPages = {
info: SidebarPage;
add?: SidebarPage;
align?: SidebarPage;
};
const getUnitSidebarPages = (readOnly: boolean, disableAdd: boolean) => {
const showAlignSidebar = getConfig().ENABLE_TAGGING_TAXONOMY_PAGES === 'true';
return {
info: {
component: InfoSidebar,
icon: Info,
title: messages.sidebarButtonInfo,
},
...(!readOnly && {
add: {
component: AddSidebar,
icon: Plus,
title: messages.sidebarButtonAdd,
disabled: disableAdd,
tooltip: disableAdd ? messages.sidebarDisabledAddTooltip : undefined,
},
}),
...(showAlignSidebar && {
align: {
component: UnitAlignSidebar,
icon: Tag,
title: messages.sidebarButtonAlign,
},
}),
};
};
/**
* Context for the Unit Sidebar Pages.
*
* This could be used in plugins to add new pages to the sidebar.
*
* @example
*
* ```tsx
* export function UnitOutlineSidebarWrapper(
* { component, pluginProps }: { component: React.ReactNode, pluginProps: UnitOutlineAspectsPageProps},
* ) {
* const sidebarPages = useUnitSidebarPagesContext();
* const AnalyticsPage = useCallback(() => <UnitOutlineAspectsPage {...pluginProps} />, [pluginProps]);
*
* const overridedPages = useMemo(() => ({
* ...sidebarPages,
* analytics: {
* component: AnalyticsPage,
* icon: AutoGraph,
* title: messages.analyticsLabel,
* },
* }), [sidebarPages, AnalyticsPage]);
*
* return (
* <UnitSidebarPagesContext.Provider value={overridedPages}>
* {component}
* </UnitSidebarPagesContext.Provider>
* );
* }
*/
export const UnitSidebarPagesContext = createContext<UnitSidebarPages | undefined>(undefined);
type UnitSidebarPagesProviderProps = {
children: React.ReactNode;
};
export const UnitSidebarPagesProvider = ({ children }: UnitSidebarPagesProviderProps) => {
const {
readOnly,
selectedComponentId,
currentItemCategory,
} = useUnitSidebarContext();
const hasComponentSelected = selectedComponentId !== undefined;
const isSplitTest = currentItemCategory === 'split_test';
const disableAdd = hasComponentSelected || isSplitTest;
const sidebarPages = useMemo(
() => getUnitSidebarPages(readOnly, disableAdd),
[readOnly, disableAdd],
);
return (
<UnitSidebarPagesContext.Provider value={sidebarPages}>
{children}
</UnitSidebarPagesContext.Provider>
);
};
export const useUnitSidebarPagesContext = (): UnitSidebarPages => {
const ctx = useContext(UnitSidebarPagesContext);
if (ctx === undefined) { throw new Error('useUnitSidebarPages must be used within an UnitSidebarPagesProvider'); }
return ctx;
};