2021-06-24 10:54:48 +02:00
|
|
|
import { useState, useEffect, Fragment } from 'react';
|
2021-05-08 18:49:08 +02:00
|
|
|
import { Link } from 'react-router-dom';
|
2021-05-23 18:55:27 +02:00
|
|
|
|
|
|
|
// Redux
|
2021-11-09 14:33:51 +01:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { State } from '../../store/reducers';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { actionCreators } from '../../store';
|
2021-05-13 18:23:12 +02:00
|
|
|
|
2021-05-23 18:55:27 +02:00
|
|
|
// Typescript
|
2021-11-09 14:33:51 +01:00
|
|
|
import { App, Category } from '../../interfaces';
|
2021-05-08 18:49:08 +02:00
|
|
|
|
2021-05-23 18:55:27 +02:00
|
|
|
// UI
|
2021-11-22 12:29:47 +01:00
|
|
|
import { Icon, Container, SectionHeadline, Spinner, Message } from '../UI';
|
2021-05-23 18:55:27 +02:00
|
|
|
|
|
|
|
// CSS
|
|
|
|
import classes from './Home.module.css';
|
|
|
|
|
|
|
|
// Components
|
2021-11-09 14:33:51 +01:00
|
|
|
import { AppGrid } from '../Apps/AppGrid/AppGrid';
|
|
|
|
import { BookmarkGrid } from '../Bookmarks/BookmarkGrid/BookmarkGrid';
|
|
|
|
import { SearchBar } from '../SearchBar/SearchBar';
|
|
|
|
import { Header } from './Header/Header';
|
|
|
|
|
2021-11-12 14:02:19 +01:00
|
|
|
// Utils
|
|
|
|
import { escapeRegex } from '../../utility';
|
|
|
|
|
2021-11-09 14:33:51 +01:00
|
|
|
export const Home = (): JSX.Element => {
|
2021-06-13 01:06:42 +02:00
|
|
|
const {
|
2021-11-09 14:33:51 +01:00
|
|
|
apps: { apps, loading: appsLoading },
|
|
|
|
bookmarks: { categories, loading: bookmarksLoading },
|
|
|
|
config: { config },
|
2021-11-22 12:29:47 +01:00
|
|
|
auth: { isAuthenticated },
|
2021-11-09 14:33:51 +01:00
|
|
|
} = useSelector((state: State) => state);
|
|
|
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const { getApps, getCategories } = bindActionCreators(
|
|
|
|
actionCreators,
|
|
|
|
dispatch
|
|
|
|
);
|
2021-06-13 01:06:42 +02:00
|
|
|
|
2021-09-06 12:24:01 +02:00
|
|
|
// Local search query
|
|
|
|
const [localSearch, setLocalSearch] = useState<null | string>(null);
|
2021-10-27 11:52:57 +02:00
|
|
|
const [appSearchResult, setAppSearchResult] = useState<null | App[]>(null);
|
|
|
|
const [bookmarkSearchResult, setBookmarkSearchResult] = useState<
|
|
|
|
null | Category[]
|
|
|
|
>(null);
|
2021-06-13 01:06:42 +02:00
|
|
|
|
|
|
|
// Load applications
|
2021-05-13 18:23:12 +02:00
|
|
|
useEffect(() => {
|
2021-10-27 11:52:57 +02:00
|
|
|
if (!apps.length) {
|
2021-06-13 01:06:42 +02:00
|
|
|
getApps();
|
2021-05-23 18:38:39 +02:00
|
|
|
}
|
2021-11-09 15:51:50 +01:00
|
|
|
}, []);
|
2021-05-08 18:49:08 +02:00
|
|
|
|
2021-06-13 01:06:42 +02:00
|
|
|
// Load bookmark categories
|
2021-05-23 18:38:39 +02:00
|
|
|
useEffect(() => {
|
2021-10-27 11:52:57 +02:00
|
|
|
if (!categories.length) {
|
2021-06-13 01:06:42 +02:00
|
|
|
getCategories();
|
2021-05-23 18:38:39 +02:00
|
|
|
}
|
2021-11-09 15:51:50 +01:00
|
|
|
}, []);
|
2021-05-08 18:49:08 +02:00
|
|
|
|
2021-10-27 11:52:57 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (localSearch) {
|
|
|
|
// Search through apps
|
|
|
|
setAppSearchResult([
|
2021-11-12 14:02:19 +01:00
|
|
|
...apps.filter(({ name }) =>
|
|
|
|
new RegExp(escapeRegex(localSearch), 'i').test(name)
|
|
|
|
),
|
2021-10-27 11:52:57 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Search through bookmarks
|
|
|
|
const category = { ...categories[0] };
|
|
|
|
|
|
|
|
category.name = 'Search Results';
|
|
|
|
category.bookmarks = categories
|
|
|
|
.map(({ bookmarks }) => bookmarks)
|
|
|
|
.flat()
|
2021-11-12 14:02:19 +01:00
|
|
|
.filter(({ name }) =>
|
|
|
|
new RegExp(escapeRegex(localSearch), 'i').test(name)
|
|
|
|
);
|
2021-10-27 11:52:57 +02:00
|
|
|
|
|
|
|
setBookmarkSearchResult([category]);
|
|
|
|
} else {
|
|
|
|
setAppSearchResult(null);
|
|
|
|
setBookmarkSearchResult(null);
|
|
|
|
}
|
|
|
|
}, [localSearch]);
|
2021-09-06 13:22:47 +02:00
|
|
|
|
2021-05-08 18:49:08 +02:00
|
|
|
return (
|
|
|
|
<Container>
|
2021-11-09 14:33:51 +01:00
|
|
|
{!config.hideSearch ? (
|
2021-10-27 11:52:57 +02:00
|
|
|
<SearchBar
|
|
|
|
setLocalSearch={setLocalSearch}
|
|
|
|
appSearchResult={appSearchResult}
|
|
|
|
bookmarkSearchResult={bookmarkSearchResult}
|
|
|
|
/>
|
2021-09-06 12:24:01 +02:00
|
|
|
) : (
|
|
|
|
<div></div>
|
|
|
|
)}
|
|
|
|
|
2021-11-18 16:03:44 +01:00
|
|
|
<Header />
|
2021-09-06 12:24:01 +02:00
|
|
|
|
2021-11-22 12:29:47 +01:00
|
|
|
{!isAuthenticated &&
|
|
|
|
!apps.some((a) => a.isPinned) &&
|
|
|
|
!categories.some((c) => c.isPinned) ? (
|
|
|
|
<Message>
|
|
|
|
Welcome to Flame! Go to <Link to="/settings/app">/settings</Link>,
|
|
|
|
login and start customizing your new homepage
|
|
|
|
</Message>
|
|
|
|
) : (
|
|
|
|
<></>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{!config.hideApps && (isAuthenticated || apps.some((a) => a.isPinned)) ? (
|
2021-09-06 12:24:01 +02:00
|
|
|
<Fragment>
|
|
|
|
<SectionHeadline title="Applications" link="/applications" />
|
|
|
|
{appsLoading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
<AppGrid
|
|
|
|
apps={
|
2021-10-27 11:52:57 +02:00
|
|
|
!appSearchResult
|
2021-09-06 12:24:01 +02:00
|
|
|
? apps.filter(({ isPinned }) => isPinned)
|
2021-10-27 11:52:57 +02:00
|
|
|
: appSearchResult
|
2021-09-06 12:24:01 +02:00
|
|
|
}
|
|
|
|
totalApps={apps.length}
|
|
|
|
searching={!!localSearch}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<div className={classes.HomeSpace}></div>
|
|
|
|
</Fragment>
|
|
|
|
) : (
|
2021-11-22 12:29:47 +01:00
|
|
|
<></>
|
2021-09-06 12:24:01 +02:00
|
|
|
)}
|
|
|
|
|
2021-11-22 12:29:47 +01:00
|
|
|
{!config.hideCategories &&
|
|
|
|
(isAuthenticated || categories.some((c) => c.isPinned)) ? (
|
2021-09-06 12:24:01 +02:00
|
|
|
<Fragment>
|
|
|
|
<SectionHeadline title="Bookmarks" link="/bookmarks" />
|
2021-11-09 14:33:51 +01:00
|
|
|
{bookmarksLoading ? (
|
2021-09-06 12:24:01 +02:00
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
<BookmarkGrid
|
2021-09-06 13:22:47 +02:00
|
|
|
categories={
|
2021-10-27 11:52:57 +02:00
|
|
|
!bookmarkSearchResult
|
2021-11-12 12:38:01 +01:00
|
|
|
? categories.filter(
|
|
|
|
({ isPinned, bookmarks }) => isPinned && bookmarks.length
|
|
|
|
)
|
2021-10-27 11:52:57 +02:00
|
|
|
: bookmarkSearchResult
|
2021-09-06 13:22:47 +02:00
|
|
|
}
|
2021-09-06 12:24:01 +02:00
|
|
|
totalCategories={categories.length}
|
2021-09-06 13:22:47 +02:00
|
|
|
searching={!!localSearch}
|
2021-11-25 16:54:27 +01:00
|
|
|
fromHomepage={true}
|
2021-09-06 12:24:01 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Fragment>
|
|
|
|
) : (
|
2021-11-22 12:29:47 +01:00
|
|
|
<></>
|
2021-09-06 12:24:01 +02:00
|
|
|
)}
|
|
|
|
|
|
|
|
<Link to="/settings" className={classes.SettingsButton}>
|
|
|
|
<Icon icon="mdiCog" color="var(--color-background)" />
|
2021-05-08 18:49:08 +02:00
|
|
|
</Link>
|
|
|
|
</Container>
|
2021-09-06 12:24:01 +02:00
|
|
|
);
|
|
|
|
};
|