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(); useEffect(() => { if (usageKey) { openComponentInfoSidebar(usageKey); } }, [usageKey]); useLoadOnScroll( hasNextPage, isFetchingNextPage, fetchNextPage, true, ); if (isLoading) { return ; } if (totalHits === 0) { if (contentType === ContentType.collections) { return isFiltered ? : ( ); } return isFiltered ? : ; } return (
{hits.map((contentHit) => { const CardComponent = LibraryItemCard[contentHit.type] || ComponentCard; return ; })}
); }; export default LibraryContent;