import { useState, useEffect, Fragment } from 'react'; import { Link } from 'react-router-dom'; // Redux import { useDispatch, useSelector } from 'react-redux'; import { State } from '../../store/reducers'; import { bindActionCreators } from 'redux'; import { actionCreators } from '../../store'; // Typescript import { App, Category } from '../../interfaces'; // UI import { Icon, Container, SectionHeadline, Spinner } from '../UI'; // CSS import classes from './Home.module.css'; // Components import { AppGrid } from '../Apps/AppGrid/AppGrid'; import { BookmarkGrid } from '../Bookmarks/BookmarkGrid/BookmarkGrid'; import { SearchBar } from '../SearchBar/SearchBar'; import { Header } from './Header/Header'; export const Home = (): JSX.Element => { const { apps: { apps, loading: appsLoading }, bookmarks: { categories, loading: bookmarksLoading }, config: { config }, } = useSelector((state: State) => state); const dispatch = useDispatch(); const { getApps, getCategories } = bindActionCreators( actionCreators, dispatch ); // Local search query const [localSearch, setLocalSearch] = useState(null); const [appSearchResult, setAppSearchResult] = useState(null); const [bookmarkSearchResult, setBookmarkSearchResult] = useState< null | Category[] >(null); // Load applications useEffect(() => { if (!apps.length) { getApps(); } }, []); // Load bookmark categories useEffect(() => { if (!categories.length) { getCategories(); } }, []); useEffect(() => { if (localSearch) { // Search through apps setAppSearchResult([ ...apps.filter(({ name }) => new RegExp(localSearch, 'i').test(name)), ]); // Search through bookmarks const category = { ...categories[0] }; category.name = 'Search Results'; category.bookmarks = categories .map(({ bookmarks }) => bookmarks) .flat() .filter(({ name }) => new RegExp(localSearch, 'i').test(name)); setBookmarkSearchResult([category]); } else { setAppSearchResult(null); setBookmarkSearchResult(null); } }, [localSearch]); return ( {!config.hideSearch ? ( ) : (
)} {!config.hideHeader ?
:
} {!config.hideApps ? ( {appsLoading ? ( ) : ( isPinned) : appSearchResult } totalApps={apps.length} searching={!!localSearch} /> )}
) : (
)} {!config.hideCategories ? ( {bookmarksLoading ? ( ) : ( isPinned && bookmarks.length ) : bookmarkSearchResult } totalCategories={categories.length} searching={!!localSearch} /> )} ) : (
)} ); };