|
1 |
| -import React from 'react'; |
| 1 | +import React, { useMemo } from 'react'; |
2 | 2 | import { useIntl } from '@edx/frontend-platform/i18n';
|
| 3 | +import { orderBy } from 'lodash'; |
| 4 | +import { CardGrid } from '@openedx/paragon'; |
3 | 5 |
|
4 | 6 | import { SearchContextProvider, useSearchContext } from '../search-manager';
|
5 |
| -import { SearchSortOption } from '../search-manager/data/api'; |
6 |
| -import LibraryComponents from './components/LibraryComponents'; |
7 |
| -import LibrarySection from './components/LibrarySection'; |
| 7 | +import { type CollectionHit, type ContentHit, SearchSortOption } from '../search-manager/data/api'; |
| 8 | +import LibrarySection, { LIBRARY_SECTION_PREVIEW_LIMIT } from './components/LibrarySection'; |
8 | 9 | import messages from './messages';
|
| 10 | +import ComponentCard from './components/ComponentCard'; |
| 11 | +import { useLibraryBlockTypes } from './data/apiHooks'; |
| 12 | +import CollectionCard from './components/CollectionCard'; |
9 | 13 |
|
10 | 14 | const RecentlyModified: React.FC<{ libraryId: string }> = ({ libraryId }) => {
|
11 | 15 | const intl = useIntl();
|
12 |
| - const { totalHits: componentCount } = useSearchContext(); |
| 16 | + const { |
| 17 | + hits, |
| 18 | + collectionHits, |
| 19 | + totalHits, |
| 20 | + totalCollectionHits, |
| 21 | + } = useSearchContext(); |
| 22 | + |
| 23 | + const componentCount = totalHits + totalCollectionHits; |
| 24 | + // Since we only display a fixed number of items in preview, |
| 25 | + // only these number of items are use in sort step below |
| 26 | + const componentList = hits.slice(0, LIBRARY_SECTION_PREVIEW_LIMIT); |
| 27 | + const collectionList = collectionHits.slice(0, LIBRARY_SECTION_PREVIEW_LIMIT); |
| 28 | + // Sort them by `modified` field in reverse and display them |
| 29 | + const recentItems = orderBy([ |
| 30 | + ...componentList, |
| 31 | + ...collectionList, |
| 32 | + ], ['modified'], ['desc']).slice(0, LIBRARY_SECTION_PREVIEW_LIMIT); |
| 33 | + |
| 34 | + const { data: blockTypesData } = useLibraryBlockTypes(libraryId); |
| 35 | + const blockTypes = useMemo(() => { |
| 36 | + const result = {}; |
| 37 | + if (blockTypesData) { |
| 38 | + blockTypesData.forEach(blockType => { |
| 39 | + result[blockType.blockType] = blockType; |
| 40 | + }); |
| 41 | + } |
| 42 | + return result; |
| 43 | + }, [blockTypesData]); |
13 | 44 |
|
14 | 45 | return componentCount > 0
|
15 | 46 | ? (
|
16 | 47 | <LibrarySection
|
17 | 48 | title={intl.formatMessage(messages.recentlyModifiedTitle)}
|
18 | 49 | contentCount={componentCount}
|
19 | 50 | >
|
20 |
| - <LibraryComponents libraryId={libraryId} variant="preview" /> |
| 51 | + <CardGrid |
| 52 | + columnSizes={{ |
| 53 | + sm: 12, |
| 54 | + md: 6, |
| 55 | + lg: 4, |
| 56 | + xl: 3, |
| 57 | + }} |
| 58 | + hasEqualColumnHeights |
| 59 | + > |
| 60 | + {recentItems.map((contentHit) => ( |
| 61 | + contentHit.type === 'collection' ? ( |
| 62 | + <CollectionCard |
| 63 | + key={contentHit.id} |
| 64 | + collectionHit={contentHit as CollectionHit} |
| 65 | + /> |
| 66 | + ) : ( |
| 67 | + <ComponentCard |
| 68 | + key={contentHit.id} |
| 69 | + contentHit={contentHit as ContentHit} |
| 70 | + blockTypeDisplayName={blockTypes[(contentHit as ContentHit).blockType]?.displayName ?? ''} |
| 71 | + /> |
| 72 | + ) |
| 73 | + ))} |
| 74 | + </CardGrid> |
21 | 75 | </LibrarySection>
|
22 | 76 | )
|
23 | 77 | : null;
|
|
0 commit comments