forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryContent.tsx
More file actions
99 lines (88 loc) · 3.07 KB
/
LibraryContent.tsx
File metadata and controls
99 lines (88 loc) · 3.07 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
import { useEffect } from 'react';
import { LoadingSpinner } from '../generic/Loading';
import { useSearchContext } from '../search-manager';
import { NoComponents, NoSearchResults } from './EmptyStates';
import { useLibraryContext } from './common/context/LibraryContext';
import { useSidebarContext } from './common/context/SidebarContext';
import CollectionCard from './components/CollectionCard';
import ComponentCard from './components/ComponentCard';
import ContainerCard from './components/ContainerCard';
import { ContentType } from './routes';
import { useLoadOnScroll } from '../hooks';
import messages from './collections/messages';
/**
* Library Content to show content grid
*
* Use content to:
* - 'collections': Suggest to create a collection on empty state.
* - Anything else to suggest to add content on empty state.
*/
type LibraryContentProps = {
contentType?: ContentType;
};
const LibraryItemCard = {
collection: CollectionCard,
library_block: ComponentCard,
library_container: ContainerCard,
};
const LibraryContent = ({ contentType = ContentType.home }: LibraryContentProps) => {
const {
hits,
totalHits,
isFetchingNextPage,
hasNextPage,
fetchNextPage,
isLoading,
isFiltered,
usageKey,
} = useSearchContext();
const { openCreateCollectionModal } = useLibraryContext();
const { openAddContentSidebar, openComponentInfoSidebar } = useSidebarContext();
/**
* Filter collections on the frontend to display only collection cards in the Collections tab.
* This approach is used instead of backend filtering to ensure that all components (including those
* within collections) remain available in the 'hits' array. This is necessary for the component
* selection workflow when adding components to xblocks by choosing the whole collection in Collections tab.
* Note: LibraryAuthoringPage.tsx has been modified to skip backend filtering for this purpose.
*/
const filteredHits = contentType === ContentType.collections
? hits.filter((hit) => hit.type === 'collection')
: hits;
useEffect(() => {
if (usageKey) {
openComponentInfoSidebar(usageKey);
}
}, [usageKey]);
useLoadOnScroll(
hasNextPage,
isFetchingNextPage,
fetchNextPage,
true,
);
if (isLoading) {
return <LoadingSpinner />;
}
if (totalHits === 0) {
if (contentType === ContentType.collections) {
return isFiltered
? <NoSearchResults infoText={messages.noSearchResultsCollections} />
: (
<NoComponents
infoText={messages.noCollections}
addBtnText={messages.addCollection}
handleBtnClick={openCreateCollectionModal}
/>
);
}
return isFiltered ? <NoSearchResults /> : <NoComponents handleBtnClick={openAddContentSidebar} />;
}
return (
<div className="library-cards-grid">
{filteredHits.map((contentHit) => {
const CardComponent = LibraryItemCard[contentHit.type] || ComponentCard;
return <CardComponent key={contentHit.id} hit={contentHit} />;
})}
</div>
);
};
export default LibraryContent;