forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReviewImportDetails.tsx
More file actions
265 lines (240 loc) · 9.12 KB
/
ReviewImportDetails.tsx
File metadata and controls
265 lines (240 loc) · 9.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
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
import { getConfig } from '@edx/frontend-platform';
import { FormattedMessage } from '@edx/frontend-platform/i18n';
import { Alert, Stack } from '@openedx/paragon';
import { LoadingSpinner } from '@src/generic/Loading';
import { useCourseDetails } from '@src/course-outline/data/apiHooks';
import { useEffect, useMemo } from 'react';
import { CheckCircle, Warning } from '@openedx/paragon/icons';
import { useLibraryContext } from '@src/library-authoring/common/context/LibraryContext';
import { useLibraryBlockLimits, useMigrationInfo } from '@src/library-authoring/data/apiHooks';
import { useGetBlockTypes, useGetContentHits } from '@src/search-manager';
import { SummaryCard } from './SummaryCard';
import messages from '../messages';
interface Props {
courseId?: string;
markAnalysisComplete: (analysisCompleted: boolean) => void;
}
interface BannerProps {
courseId?: string;
isBlockDataPending?: boolean;
unsupportedBlockPercentage: number;
}
const Banner = ({ courseId, isBlockDataPending, unsupportedBlockPercentage }: BannerProps) => {
const { data, isPending } = useCourseDetails(courseId);
const { libraryId } = useLibraryContext();
const { data: migrationInfoData, isPending: migrationInfoIsPending } = useMigrationInfo(
[courseId!],
(courseId !== undefined && libraryId !== undefined),
);
const currentMigrationInfo = useMemo(() => {
if (!migrationInfoData || !courseId) {
return undefined;
}
return Object.values(migrationInfoData)[0]?.find(info => info.targetKey === libraryId);
}, [migrationInfoData]);
if (isPending) {
return (
<Alert>
<div className="text-center p-3">
<LoadingSpinner />
</div>
</Alert>
);
}
if (isBlockDataPending || migrationInfoIsPending) {
return (
<Alert>
<Alert.Heading><FormattedMessage {...messages.importCourseInProgressStatusTitle} /></Alert.Heading>
<p>
<FormattedMessage
{...messages.importCourseInProgressStatusBody}
values={{
courseName: data?.title || '',
}}
/>
</p>
</Alert>
);
}
if (currentMigrationInfo) {
return (
<>
<Alert variant="warning" icon={Warning}>
<Alert.Heading><FormattedMessage {...messages.importCourseAnalysisCompleteReimportTitle} /></Alert.Heading>
</Alert>
<p>
<FormattedMessage
{...messages.importCourseAnalysisCompleteReimportBody}
values={{
courseName: data?.title || '',
libraryName: currentMigrationInfo?.targetTitle || '',
}}
/>
</p>
</>
);
}
if (unsupportedBlockPercentage > 0) {
return (
<Alert variant="warning" icon={Warning}>
<Alert.Heading><FormattedMessage {...messages.importCourseAnalysisCompleteSomeContentTitle} /></Alert.Heading>
<p>
<FormattedMessage
{...messages.importCourseAnalysisCompleteSomeContentBody}
values={{
unsupportedBlockPercentage: unsupportedBlockPercentage.toFixed(2),
}}
/>
</p>
</Alert>
);
}
return (
<Alert variant="success" icon={CheckCircle}>
<Alert.Heading><FormattedMessage {...messages.importCourseAnalysisCompleteAllContentTitle} /></Alert.Heading>
<p>
<FormattedMessage
{...messages.importCourseAnalysisCompleteAllContentBody}
values={{
courseName: data?.title || '',
}}
/>
</p>
</Alert>
);
};
export const ReviewImportDetails = ({ courseId, markAnalysisComplete }: Props) => {
const { data: blockTypes, isPending: isBlockDataPending } = useGetBlockTypes([
`context_key = "${courseId}"`,
]);
const {
data: libraryBlockLimits,
isPending: isPendinglibraryBlockLimits,
} = useLibraryBlockLimits();
useEffect(() => {
// Mark complete to inform parent component of analysis completion.
markAnalysisComplete(!isBlockDataPending && !isPendinglibraryBlockLimits);
}, [isBlockDataPending, isPendinglibraryBlockLimits]);
/** Filter unsupported blocks by checking if the block type is in the library's list of unsupported blocks. */
const unsupportedBlockTypes = useMemo(() => {
if (!blockTypes) {
return undefined;
}
return Object.entries(blockTypes).filter(([blockType]) => (
getConfig().LIBRARY_UNSUPPORTED_BLOCKS.includes(blockType)
));
}, [blockTypes]);
/** Calculate the total number of unsupported blocks by summing up the count for each block type. */
const totalUnsupportedBlocks = useMemo(() => {
if (!unsupportedBlockTypes) {
return 0;
}
const unsupportedBlocks = unsupportedBlockTypes.reduce((total, [, count]) => total + count, 0);
return unsupportedBlocks;
}, [unsupportedBlockTypes]);
// Fetch unsupported blocks usage_key information from meilisearch index.
const { data: unsupportedBlocksData } = useGetContentHits(
[
`context_key = "${courseId}"`,
`block_type IN [${unsupportedBlockTypes?.flatMap(([value]) => `"${value}"`).join(',')}]`,
],
totalUnsupportedBlocks > 0,
['usage_key'],
totalUnsupportedBlocks,
'always',
);
// Fetch children blocks for each block in the unsupportedBlocks array.
const { data: unsupportedBlocksChildren } = useGetBlockTypes([
`context_key = "${courseId}"`,
`breadcrumbs.usage_key IN [${unsupportedBlocksData?.hits.map((value) => `"${value.usage_key}"`).join(',')}]`,
], (unsupportedBlocksData?.estimatedTotalHits || 0) > 0);
/** Calculate the total number of unsupported children blocks by summing up the count for each block. */
const totalUnsupportedBlockChildren = useMemo(() => {
if (!unsupportedBlocksChildren) {
return 0;
}
const unsupportedBlocks = Object.values(unsupportedBlocksChildren).reduce((total, count) => total + count, 0);
return unsupportedBlocks;
}, [unsupportedBlocksChildren]);
/** Finally calculate the final number of unsupported blocks by adding parent unsupported and children
unsupported blocks. */
let finalUnsupportedBlocks = useMemo(
() => totalUnsupportedBlocks + totalUnsupportedBlockChildren,
[totalUnsupportedBlocks, totalUnsupportedBlockChildren],
);
/** Calculate total components by excluding those that are chapters, sequential, or vertical. */
/** Also, calculate if the total components exceed the limits */
const { totalComponents, unsupportedByLimit } = useMemo(() => {
if (!blockTypes) {
return {
totalComponents: undefined,
unsupportedByLimit: 0,
};
}
let resultTotalComponents = Object.entries(blockTypes).reduce(
(total, [blockType, count]) => {
const isComponent = !['chapter', 'sequential', 'vertical'].includes(blockType);
if (isComponent) {
return total + count;
}
return total;
},
0,
) - finalUnsupportedBlocks;
let resultUnsupportedByLimit = 0;
if (libraryBlockLimits && resultTotalComponents > libraryBlockLimits.maxBlocksPerContentLibrary) {
resultUnsupportedByLimit = resultTotalComponents - libraryBlockLimits.maxBlocksPerContentLibrary;
resultTotalComponents -= resultUnsupportedByLimit;
}
return {
totalComponents: resultTotalComponents,
unsupportedByLimit: resultUnsupportedByLimit,
};
}, [blockTypes, finalUnsupportedBlocks, libraryBlockLimits]);
// Adds the components exceed the limit to the final unsupported count
if (unsupportedByLimit) {
finalUnsupportedBlocks += unsupportedByLimit;
}
/** Calculate total supported blocks by subtracting final unsupported blocks from the total number of blocks */
const totalBlocks = useMemo(() => {
if (!blockTypes) {
return undefined;
}
return Object.values(blockTypes).reduce((total, block) => total + block, 0) - finalUnsupportedBlocks;
}, [blockTypes, finalUnsupportedBlocks]);
/** Calculate the unsupported block percentage based on the final total blocks and unsupported blocks. */
const unsupportedBlockPercentage = useMemo(() => {
if (!blockTypes || !totalBlocks) {
return 0;
}
return (finalUnsupportedBlocks / (totalBlocks + finalUnsupportedBlocks)) * 100;
}, [blockTypes, finalUnsupportedBlocks]);
return (
<Stack gap={4}>
<Banner
courseId={courseId}
isBlockDataPending={isBlockDataPending}
unsupportedBlockPercentage={unsupportedBlockPercentage}
/>
<h4><FormattedMessage {...messages.importCourseAnalysisSummary} /></h4>
<SummaryCard
totalBlocks={totalBlocks}
totalComponents={totalComponents}
sections={blockTypes?.chapter}
subsections={blockTypes?.sequential}
units={blockTypes?.vertical}
unsupportedBlocks={finalUnsupportedBlocks}
isPending={isBlockDataPending}
/>
{!isBlockDataPending && finalUnsupportedBlocks > 0
&& (
<>
<h4><FormattedMessage {...messages.importCourseAnalysisDetails} /></h4>
<Stack className="align-items-center" gap={3}>
<FormattedMessage {...messages.importCourseAnalysisDetailsUnsupportedBlocksBody} />
</Stack>
</>
)}
</Stack>
);
};