1- import React from 'react' ;
1+ import React , { useCallback , useMemo } from 'react' ;
22import { useLocation } from 'react-router-dom' ;
33import { useDispatch , useSelector } from 'react-redux' ;
44import { FormattedMessage , useIntl } from '@edx/frontend-platform/i18n' ;
99 Alert ,
1010 Button ,
1111 Form ,
12+ Chip ,
1213} from '@openedx/paragon' ;
1314import { Error } from '@openedx/paragon/icons' ;
1415
@@ -22,6 +23,7 @@ import ProcessingCourses from '@src/studio-home/processing-courses';
2223import { LoadingSpinner } from '@src/generic/Loading' ;
2324import AlertMessage from '@src/generic/alert-message' ;
2425import { RequestStatus } from '@src/data/constants' ;
26+ import { useMigrationInfo } from '@src/studio-home/data/apiHooks' ;
2527import messages from '../messages' ;
2628import CoursesFilters from './courses-filters' ;
2729import ContactAdministrator from './contact-administrator' ;
@@ -39,6 +41,7 @@ interface CardListProps {
3941 onClickNewCourse ?: ( ) => void ;
4042 inSelectMode ?: boolean ;
4143 selectedCourseId ?: string ;
44+ currentLibraryId ?: string ;
4245}
4346
4447const CardList = ( {
@@ -53,16 +56,37 @@ const CardList = ({
5356 onClickNewCourse = ( ) => { } ,
5457 inSelectMode = false ,
5558 selectedCourseId,
59+ currentLibraryId,
5660} : CardListProps ) => {
5761 const {
5862 courses,
5963 numPages,
6064 optimizationEnabled,
6165 } = useSelector ( getStudioHomeData ) ;
6266
67+ const {
68+ data : migrationInfoData ,
69+ } = useMigrationInfo ( courses ?. map ( item => item . courseKey ) || [ ] , true ) ;
70+
71+ const processedMigrationInfo = useMemo ( ( ) => {
72+ const result = { } ;
73+ if ( migrationInfoData ) {
74+ for ( const libraries of Object . values ( migrationInfoData ) ) {
75+ // The map key in `migrationInfoData` is in camelCase.
76+ // In the processed map, we use the key in its original form.
77+ result [ libraries [ 0 ] . sourceKey ] = libraries . map ( item => item . targetKey ) ;
78+ }
79+ }
80+ return result ;
81+ } , [ migrationInfoData ] ) ;
82+
6383 const isNotFilteringCourses = ! isFiltered && ! isLoading ;
6484 const hasCourses = courses ?. length > 0 ;
6585
86+ const isPreviouslyMigrated = useCallback ( ( courseKey : string ) => (
87+ courseKey in processedMigrationInfo && processedMigrationInfo [ courseKey ] . includes ( currentLibraryId )
88+ ) , [ processedMigrationInfo ] ) ;
89+
6690 return (
6791 < >
6892 { hasCourses ? (
@@ -79,7 +103,8 @@ const CardList = ({
79103 url,
80104 } ) => (
81105 < CardItem
82- key = { courseKey }
106+ // Add `-migrated` to force re-render of the Chip
107+ key = { `${ courseKey } ${ isPreviouslyMigrated ( courseKey ) ? '-migrated' : '' } ` }
83108 courseKey = { courseKey }
84109 onClick = { ( ) => onClickCard ?.( courseKey ) }
85110 itemId = { courseKey }
@@ -93,6 +118,16 @@ const CardList = ({
93118 selectMode = { inSelectMode ? 'single' : undefined }
94119 selectPosition = { inSelectMode ? 'card' : undefined }
95120 isSelected = { inSelectMode && selectedCourseId === courseKey }
121+ subtitleBeforeComponent = { isPreviouslyMigrated ( courseKey ) && (
122+ < div
123+ key = { `${ courseKey } -${ processedMigrationInfo [ courseKey ] . join ( '-' ) } ` }
124+ className = "previously-migrated-chip"
125+ >
126+ < Chip >
127+ < FormattedMessage { ...messages . previouslyImported } />
128+ </ Chip >
129+ </ div >
130+ ) }
96131 />
97132 ) ,
98133 ) }
@@ -139,6 +174,7 @@ interface Props {
139174 isShowProcessing ?: boolean ;
140175 selectedCourseId ?: string ;
141176 handleSelect ?: ( courseId : string ) => void ;
177+ currentLibraryId ?: string ;
142178}
143179
144180export const CoursesList : React . FC < Props > = ( {
@@ -147,6 +183,7 @@ export const CoursesList: React.FC<Props> = ({
147183 isShowProcessing = false ,
148184 selectedCourseId,
149185 handleSelect,
186+ currentLibraryId,
150187} ) => {
151188 const dispatch = useDispatch ( ) ;
152189 const intl = useIntl ( ) ;
@@ -243,6 +280,7 @@ export const CoursesList: React.FC<Props> = ({
243280 isFiltered = { isFiltered || false }
244281 inSelectMode
245282 selectedCourseId = { selectedCourseId }
283+ currentLibraryId = { currentLibraryId }
246284 />
247285 </ Form . RadioSet >
248286 ) : (
0 commit comments