@@ -8,7 +8,7 @@ import { useEffect, useMemo } from 'react';
88import { CheckCircle , Warning } from '@openedx/paragon/icons' ;
99import { useLibraryContext } from '@src/library-authoring/common/context/LibraryContext' ;
1010import { useMigrationInfo } from '@src/library-authoring/data/apiHooks' ;
11- import { useGetBlockTypes } from '@src/search-manager' ;
11+ import { useGetBlockTypes , useGetContentHits } from '@src/search-manager' ;
1212import { SummaryCard } from './SummaryCard' ;
1313import messages from '../messages' ;
1414
@@ -120,30 +120,72 @@ export const ReviewImportDetails = ({ courseId, markAnalysisComplete }: Props) =
120120 ] ) ;
121121
122122 useEffect ( ( ) => {
123+ // Mark complete to inform parent component of analysis completion.
123124 markAnalysisComplete ( ! isBlockDataPending ) ;
124125 } , [ isBlockDataPending ] ) ;
125126
126- const totalUnsupportedBlocks = useMemo ( ( ) => {
127+ /** Filter unsupported blocks by checking if the block type is in the library's list of unsupported blocks. */
128+ const unsupportedBlockTypes = useMemo ( ( ) => {
127129 if ( ! blockTypes ) {
130+ return undefined ;
131+ }
132+ return Object . entries ( blockTypes ) . filter ( ( [ blockType ] ) => (
133+ getConfig ( ) . LIBRARY_UNSUPPORTED_BLOCKS . includes ( blockType )
134+ ) ) ;
135+ } , [ blockTypes ] ) ;
136+
137+ /** Calculate the total number of unsupported blocks by summing up the count for each block type. */
138+ const totalUnsupportedBlocks = useMemo ( ( ) => {
139+ if ( ! unsupportedBlockTypes ) {
128140 return 0 ;
129141 }
130- const unsupportedBlocks = Object . entries ( blockTypes ) . reduce ( ( total , [ blockType , count ] ) => {
131- const isUnsupportedBlock = getConfig ( ) . LIBRARY_UNSUPPORTED_BLOCKS . includes ( blockType ) ;
132- if ( isUnsupportedBlock ) {
133- return total + count ;
134- }
135- return total ;
136- } , 0 ) ;
142+ const unsupportedBlocks = unsupportedBlockTypes . reduce ( ( total , [ , count ] ) => total + count , 0 ) ;
137143 return unsupportedBlocks ;
138- } , [ blockTypes ] ) ;
144+ } , [ unsupportedBlockTypes ] ) ;
145+
146+ // Fetch unsupported blocks usage_key information from meilisearch index.
147+ const { data : unsupportedBlocksData } = useGetContentHits (
148+ [
149+ `context_key = "${ courseId } "` ,
150+ `block_type IN [${ unsupportedBlockTypes ?. flatMap ( ( [ value ] ) => `"${ value } "` ) . join ( ',' ) } ]` ,
151+ ] ,
152+ totalUnsupportedBlocks > 0 ,
153+ [ 'usage_key' ] ,
154+ totalUnsupportedBlocks ,
155+ 'always' ,
156+ ) ;
157+
158+ // Fetch children blocks for each block in the unsupportedBlocks array.
159+ const { data : unsupportedBlocksChildren } = useGetBlockTypes ( [
160+ `context_key = "${ courseId } "` ,
161+ `breadcrumbs.usage_key IN [${ unsupportedBlocksData ?. hits . map ( ( value ) => `"${ value . usage_key } "` ) . join ( ',' ) } ]` ,
162+ ] , ( unsupportedBlocksData ?. estimatedTotalHits || 0 ) > 0 ) ;
139163
164+ /** Calculate the total number of unsupported children blocks by summing up the count for each block. */
165+ const totalUnsupportedBlockChildren = useMemo ( ( ) => {
166+ if ( ! unsupportedBlocksChildren ) {
167+ return 0 ;
168+ }
169+ const unsupportedBlocks = Object . values ( unsupportedBlocksChildren ) . reduce ( ( total , count ) => total + count , 0 ) ;
170+ return unsupportedBlocks ;
171+ } , [ unsupportedBlocksChildren ] ) ;
172+
173+ /** Finally calculate the final number of unsupported blocks by adding parent unsupported and children
174+ unsupported blocks. */
175+ const finalUnssupportedBlocks = useMemo (
176+ ( ) => totalUnsupportedBlocks + totalUnsupportedBlockChildren ,
177+ [ totalUnsupportedBlocks , totalUnsupportedBlockChildren ] ,
178+ ) ;
179+
180+ /** Calculate total supported blocks by subtracting final unsupported blocks from the total number of blocks */
140181 const totalBlocks = useMemo ( ( ) => {
141182 if ( ! blockTypes ) {
142183 return undefined ;
143184 }
144- return Object . values ( blockTypes ) . reduce ( ( total , block ) => total + block , 0 ) - totalUnsupportedBlocks ;
145- } , [ blockTypes ] ) ;
185+ return Object . values ( blockTypes ) . reduce ( ( total , block ) => total + block , 0 ) - finalUnssupportedBlocks ;
186+ } , [ blockTypes , finalUnssupportedBlocks ] ) ;
146187
188+ /** Calculate total components by excluding those that are chapters, sequential, or vertical. */
147189 const totalComponents = useMemo ( ( ) => {
148190 if ( ! blockTypes ) {
149191 return undefined ;
@@ -157,15 +199,16 @@ export const ReviewImportDetails = ({ courseId, markAnalysisComplete }: Props) =
157199 return total ;
158200 } ,
159201 0 ,
160- ) - totalUnsupportedBlocks ;
161- } , [ blockTypes ] ) ;
202+ ) - finalUnssupportedBlocks ;
203+ } , [ blockTypes , finalUnssupportedBlocks ] ) ;
162204
205+ /** Calculate the unsupported block percentage based on the final total blocks and unsupported blocks. */
163206 const unsupportedBlockPercentage = useMemo ( ( ) => {
164207 if ( ! blockTypes || ! totalBlocks ) {
165208 return 0 ;
166209 }
167- return ( totalUnsupportedBlocks / ( totalBlocks + totalUnsupportedBlocks ) ) * 100 ;
168- } , [ blockTypes ] ) ;
210+ return ( finalUnssupportedBlocks / ( totalBlocks + finalUnssupportedBlocks ) ) * 100 ;
211+ } , [ blockTypes , finalUnssupportedBlocks ] ) ;
169212
170213 return (
171214 < Stack gap = { 4 } >
@@ -181,10 +224,10 @@ export const ReviewImportDetails = ({ courseId, markAnalysisComplete }: Props) =
181224 sections = { blockTypes ?. chapter }
182225 subsections = { blockTypes ?. sequential }
183226 units = { blockTypes ?. vertical }
184- unsupportedBlocks = { totalUnsupportedBlocks }
227+ unsupportedBlocks = { finalUnssupportedBlocks }
185228 isPending = { isBlockDataPending }
186229 />
187- { ! isBlockDataPending && totalUnsupportedBlocks > 0
230+ { ! isBlockDataPending && finalUnssupportedBlocks > 0
188231 && (
189232 < >
190233 < h4 > < FormattedMessage { ...messages . importCourseAnalysisDetails } /> </ h4 >
0 commit comments