forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitInfoSidebar.tsx
More file actions
290 lines (271 loc) · 10 KB
/
UnitInfoSidebar.tsx
File metadata and controls
290 lines (271 loc) · 10 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { useEffect, useContext } from 'react';
import { isEmpty } from 'lodash';
import { useIntl } from '@edx/frontend-platform/i18n';
import {
Button,
Stack,
Tab,
Tabs,
} from '@openedx/paragon';
import {
OpenInFull,
} from '@openedx/paragon/icons';
import { getItemIcon } from '@src/generic/block-type-utils';
import { SidebarTitle } from '@src/generic/sidebar';
import { courseOutlineQueryKeys, useCourseItemData } from '@src/course-outline/data/apiHooks';
import Loading from '@src/generic/Loading';
import { useCourseAuthoringContext } from '@src/CourseAuthoringContext';
import { useCourseOutlineContext } from '@src/course-outline/CourseOutlineContext';
import XBlockContainerIframe from '@src/course-unit/xblock-container-iframe';
import { IframeProvider } from '@src/generic/hooks/context/iFrameContext';
import { Link, useNavigate } from 'react-router-dom';
import { getLibraryId } from '@src/generic/key-utils';
import { extractCourseUnitId } from '@src/course-unit/legacy-sidebar/utils';
import { possibleUnitMoves } from '@src/course-outline/drag-helper/utils';
import { GenericUnitInfoSettings } from '@src/course-unit/unit-sidebar/unit-info/GenericUnitInfoSettings';
import { useQueryClient } from '@tanstack/react-query';
import { useOutlineSidebarContext } from '../OutlineSidebarContext';
import { PublishButon } from './PublishButon';
import messages from '../messages';
import { InfoSection } from './InfoSection';
import { useClipboard } from '@src/generic/clipboard';
import { ToastContext } from '@src/generic/toast-context';
import { XBlock } from '@src/data/types';
interface Props {
unitId: string;
}
const UnitSettingsTab = ({ unitId }: Props) => {
const queryClient = useQueryClient();
const { data: unitData, isPending } = useCourseItemData(unitId);
const { selectedContainerState } = useOutlineSidebarContext();
if (isPending || !unitData) {
return <Loading />;
}
const onUpdate = () => {
queryClient.invalidateQueries({ queryKey: courseOutlineQueryKeys.courseItemId(unitId) });
};
return (
<GenericUnitInfoSettings
key={unitData.id}
id={unitData.id}
visibilityState={unitData.visibilityState}
discussionEnabled={unitData.discussionEnabled}
userPartitionInfo={unitData.userPartitionInfo}
updateCallback={onUpdate}
subsectionId={selectedContainerState?.subsectionId}
sectionId={selectedContainerState?.sectionId}
/>
);
};
export const UnitSidebar = () => {
const intl = useIntl();
const navigate = useNavigate();
const {
selectedContainerState,
clearSelection,
currentTabKey,
setCurrentTabKey,
setSelectedContainerState,
} = useOutlineSidebarContext();
const {
currentId: unitId = /* istanbul ignore next */ '',
index,
} = selectedContainerState ?? {};
const { data: unitData, isPending } = useCourseItemData(unitId);
const availableTabs = {
preview: 'preview',
info: 'info',
settings: 'settings',
};
useEffect(() => {
if (!currentTabKey || !Object.values(availableTabs).includes(currentTabKey)) {
// Set default Tab key
setCurrentTabKey('preview');
}
}, [currentTabKey, setCurrentTabKey]);
const { data: section } = useCourseItemData<XBlock>(selectedContainerState?.sectionId);
const { data: subsection } = useCourseItemData<XBlock>(selectedContainerState?.subsectionId);
const { getUnitUrl, courseId, openUnlinkModal } = useCourseAuthoringContext();
const {
openPublishModal,
handleDuplicateUnitSubmit,
sections,
updateUnitOrderByIndex,
openDeleteModal,
} = useCourseOutlineContext();
const sectionIndex = sections.findIndex((s) => s.id === selectedContainerState?.sectionId);
const subsectionIndex = section?.childInfo?.children?.findIndex(
(s) => s.id === selectedContainerState?.subsectionId,
) ?? -1;
const { copyToClipboard } = useClipboard();
const { showToast } = useContext(ToastContext);
const handlePublish = () => {
if (unitData?.hasChanges) {
openPublishModal({
value: unitData,
sectionId: selectedContainerState?.sectionId,
subsectionId: selectedContainerState?.subsectionId,
});
}
};
if (isPending || !unitData) {
return <Loading />;
}
// re-create actions object for customizations
const actions = { ...unitData.actions };
actions.deletable = actions.deletable && !subsection?.upstreamInfo?.upstreamRef;
actions.duplicable = actions.duplicable && !subsection?.upstreamInfo?.upstreamRef;
// Build move calculator only when all ancestor context is available
const getPossibleMoves = (section && subsection && subsectionIndex !== -1)
? possibleUnitMoves(
[...sections],
sectionIndex ?? -1,
subsectionIndex,
section,
subsection,
subsection.childInfo.children,
)
: undefined;
const canMoveUnit = (oldIndex: number, step: number) => {
if (getPossibleMoves) {
const moveDetails = getPossibleMoves(oldIndex, step);
return !isEmpty(moveDetails) && !subsection?.upstreamInfo?.upstreamRef;
}
/* istanbul ignore next */
return false;
};
const handleMove = (step: number) => {
if (section && subsection && getPossibleMoves && index !== undefined && sectionIndex !== undefined) {
const moveDetails = getPossibleMoves(index, step);
// section is the current parent section (used as prevSection in cross-section moves)
updateUnitOrderByIndex(section, moveDetails);
if (!isEmpty(moveDetails)) {
const newSectionId = moveDetails.sectionId;
const newSubsectionId = moveDetails.subsectionId;
// Cross-subsection move: unit goes to end of previous or start of next subsection
const isCrossSubsection = newSubsectionId !== subsection.id;
/* istanbul ignore next */
const newSectionIndex = newSectionId !== section.id
? sections.findIndex((s) => s.id === newSectionId)
: sectionIndex;
/* istanbul ignore next */
const newIndex = isCrossSubsection
? (step === -1
? sections[newSectionIndex].childInfo.children.find((s) => s.id === newSubsectionId)?.childInfo.children
.length ?? 0
: 0)
: index + step;
/* istanbul ignore next */
setSelectedContainerState(
selectedContainerState ?
{
...selectedContainerState,
sectionId: newSectionId,
subsectionId: newSubsectionId,
index: newIndex,
} :
undefined,
);
}
}
};
const handleCopyLocation = () => {
const locationId = extractCourseUnitId(unitId);
if (!locationId) {
/* istanbul ignore next */
return;
}
if (navigator.clipboard) {
// Modern approach: requires HTTPS (secure context)
void navigator.clipboard.writeText(locationId);
} /* istanbul ignore next */ else {
// Fallback for HTTP (non-secure) dev environments
// Note: execCommand is deprecated but still widely supported as fallback
const textarea = document.createElement('textarea');
textarea.value = locationId;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy'); // eslint-disable-line deprecation/deprecation
document.body.removeChild(textarea);
}
showToast(intl.formatMessage(messages.locationCopiedText));
};
return (
<>
<SidebarTitle
title={unitData?.displayName || ''}
icon={getItemIcon(unitData?.category || '')}
onBackBtnClick={clearSelection}
menuProps={{
itemId: unitId,
index: index ?? -1,
actions,
canMoveItem: canMoveUnit,
onClickDuplicate: unitData?.actions?.duplicable ? handleDuplicateUnitSubmit : undefined,
onClickMoveUp: () => handleMove(-1),
onClickMoveDown: () => handleMove(1),
onClickUnlink: () =>
openUnlinkModal({
value: unitData,
sectionId: selectedContainerState?.sectionId,
subsectionId: selectedContainerState?.subsectionId,
}),
onClickDelete: openDeleteModal,
onClickViewLibrary: () => {
const upstreamRef = unitData?.upstreamInfo?.upstreamRef;
if (upstreamRef) {
const libId = getLibraryId(upstreamRef);
navigate(`/library/${libId}/unit/${upstreamRef}`);
}
},
onClickCopy: /* istanbul ignore next */ () => copyToClipboard(unitId),
onClickCopyLocation: handleCopyLocation,
}}
/>
<Stack direction="horizontal" gap={1} className="mx-2">
<Button
variant="outline-primary"
as={Link}
to={getUnitUrl(unitId)}
iconBefore={OpenInFull}
block={!unitData?.hasChanges}
>
{intl.formatMessage(messages.openUnitPage)}
</Button>
{unitData?.hasChanges && <PublishButon onClick={handlePublish} />}
</Stack>
<Tabs
variant="tabs"
className="my-2 mx-n3.5"
id="unit-content-tabs"
activeKey={currentTabKey}
onSelect={setCurrentTabKey}
mountOnEnter
>
<Tab
eventKey={availableTabs.preview}
title={intl.formatMessage(messages.previewTabText)}
// To make sure that data is fresh
unmountOnExit
>
<IframeProvider>
<XBlockContainerIframe
courseId={courseId}
blockId={unitId}
isUnitVerticalType={false}
unitXBlockActions={{ handleDelete: () => {}, handleDuplicate: () => {}, handleUnlink: () => {} }}
courseVerticalChildren={[]}
readonly
/>
</IframeProvider>
</Tab>
<Tab eventKey={availableTabs.info} title={intl.formatMessage(messages.infoTabText)}>
<InfoSection itemId={unitId} />
</Tab>
<Tab eventKey={availableTabs.settings} title={intl.formatMessage(messages.settingsTabText)}>
<UnitSettingsTab unitId={unitId} />
</Tab>
</Tabs>
</>
);
};