forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutlineSidebarContext.tsx
More file actions
111 lines (97 loc) · 3.12 KB
/
OutlineSidebarContext.tsx
File metadata and controls
111 lines (97 loc) · 3.12 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
111
import {
createContext,
useCallback,
useContext,
useMemo,
useState,
} from 'react';
import { useIntl } from '@edx/frontend-platform/i18n';
import { useToggle } from '@openedx/paragon';
import { HelpOutline, Info, Plus } from '@openedx/paragon/icons';
import type { SidebarPage } from '@src/generic/sidebar';
import OutlineHelpSidebar from './OutlineHelpSidebar';
import { OutlineInfoSidebar } from './OutlineInfoSidebar';
import messages from './messages';
import { AddSidebar } from './AddSidebar';
import { isOutlineNewDesignEnabled } from '../utils';
export type OutlineSidebarPageKeys = 'help' | 'info' | 'add';
export type OutlineSidebarPages = Record<OutlineSidebarPageKeys, SidebarPage>;
interface OutlineSidebarContextData {
currentPageKey: OutlineSidebarPageKeys;
setCurrentPageKey: (pageKey: OutlineSidebarPageKeys) => void;
isOpen: boolean;
open: () => void;
toggle: () => void;
sidebarPages: OutlineSidebarPages;
selectedContainerId?: string;
openContainerInfoSidebar: (containerId: string) => void;
}
const OutlineSidebarContext = createContext<OutlineSidebarContextData | undefined>(undefined);
export const OutlineSidebarProvider = ({ children }: { children?: React.ReactNode }) => {
const intl = useIntl();
const [currentPageKey, setCurrentPageKeyState] = useState<OutlineSidebarPageKeys>('info');
const [isOpen, open, , toggle] = useToggle(true);
const [selectedContainerId, setSelectedContainerId] = useState<string | undefined>();
const openContainerInfoSidebar = useCallback((containerId: string) => {
if (isOutlineNewDesignEnabled()) {
setSelectedContainerId(containerId);
}
}, [setSelectedContainerId]);
const setCurrentPageKey = useCallback((pageKey: OutlineSidebarPageKeys) => {
setCurrentPageKeyState(pageKey);
open();
}, [open]);
const sidebarPages = {
info: {
component: OutlineInfoSidebar,
icon: Info,
title: intl.formatMessage(messages.sidebarButtonInfo),
},
help: {
component: OutlineHelpSidebar,
icon: HelpOutline,
title: intl.formatMessage(messages.sidebarButtonHelp),
},
add: {
component: AddSidebar,
icon: Plus,
title: intl.formatMessage(messages.sidebarButtonAdd),
hideFromActionMenu: true,
},
} satisfies OutlineSidebarPages;
const context = useMemo<OutlineSidebarContextData>(
() => ({
currentPageKey,
setCurrentPageKey,
sidebarPages,
isOpen,
open,
toggle,
selectedContainerId,
openContainerInfoSidebar,
}),
[
currentPageKey,
setCurrentPageKey,
sidebarPages,
isOpen,
open,
toggle,
selectedContainerId,
openContainerInfoSidebar,
],
);
return (
<OutlineSidebarContext.Provider value={context}>
{children}
</OutlineSidebarContext.Provider>
);
};
export function useOutlineSidebarContext(): OutlineSidebarContextData {
const ctx = useContext(OutlineSidebarContext);
if (ctx === undefined) {
/* istanbul ignore next */
throw new Error('useOutlineSidebarContext() was used in a component without a <OutlineSidebarProvider> ancestor.');
}
return ctx;
}