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
|
|
|
|
import { connect } from 'react-redux';
|
2021-05-23 18:38:39 +02:00
|
|
|
import { getApps, getCategories } from '../../store/actions';
|
2021-05-13 18:23:12 +02:00
|
|
|
|
2021-05-23 18:55:27 +02:00
|
|
|
// Typescript
|
|
|
|
import { GlobalState } from '../../interfaces/GlobalState';
|
2021-10-22 13:31:02 +02:00
|
|
|
import { App, Category, Config } from '../../interfaces';
|
2021-05-08 18:49:08 +02:00
|
|
|
|
2021-05-23 18:55:27 +02:00
|
|
|
// UI
|
|
|
|
import Icon from '../UI/Icons/Icon/Icon';
|
2021-05-08 18:49:08 +02:00
|
|
|
import { Container } from '../UI/Layout/Layout';
|
2021-05-09 19:02:50 +02:00
|
|
|
import SectionHeadline from '../UI/Headlines/SectionHeadline/SectionHeadline';
|
2021-05-13 18:23:12 +02:00
|
|
|
import Spinner from '../UI/Spinner/Spinner';
|
2021-05-23 18:55:27 +02:00
|
|
|
|
|
|
|
// CSS
|
|
|
|
import classes from './Home.module.css';
|
|
|
|
|
|
|
|
// Components
|
|
|
|
import AppGrid from '../Apps/AppGrid/AppGrid';
|
2021-05-23 18:38:39 +02:00
|
|
|
import BookmarkGrid from '../Bookmarks/BookmarkGrid/BookmarkGrid';
|
2021-05-23 18:55:27 +02:00
|
|
|
import WeatherWidget from '../Widgets/WeatherWidget/WeatherWidget';
|
2021-06-25 10:42:44 +02:00
|
|
|
import SearchBar from '../SearchBar/SearchBar';
|
2021-05-13 18:23:12 +02:00
|
|
|
|
2021-06-13 01:06:42 +02:00
|
|
|
// Functions
|
|
|
|
import { greeter } from './functions/greeter';
|
|
|
|
import { dateTime } from './functions/dateTime';
|
|
|
|
|
2021-05-13 18:23:12 +02:00
|
|
|
interface ComponentProps {
|
|
|
|
getApps: Function;
|
2021-05-23 18:38:39 +02:00
|
|
|
getCategories: Function;
|
|
|
|
appsLoading: boolean;
|
2021-05-13 18:23:12 +02:00
|
|
|
apps: App[];
|
2021-05-23 18:38:39 +02:00
|
|
|
categoriesLoading: boolean;
|
|
|
|
categories: Category[];
|
2021-10-22 13:31:02 +02:00
|
|
|
config: Config;
|
2021-05-13 18:23:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Home = (props: ComponentProps): JSX.Element => {
|
2021-06-13 01:06:42 +02:00
|
|
|
const {
|
|
|
|
getApps,
|
|
|
|
apps,
|
|
|
|
appsLoading,
|
|
|
|
getCategories,
|
|
|
|
categories,
|
2021-09-06 12:24:01 +02:00
|
|
|
categoriesLoading,
|
2021-06-13 01:06:42 +02:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
const [header, setHeader] = useState({
|
|
|
|
dateTime: dateTime(),
|
2021-09-06 12:24:01 +02:00
|
|
|
greeting: greeter(),
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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-06-14 12:13:38 +02:00
|
|
|
}, [getApps]);
|
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-06-14 12:13:38 +02:00
|
|
|
}, [getCategories]);
|
2021-05-08 18:49:08 +02:00
|
|
|
|
2021-06-13 01:06:42 +02:00
|
|
|
// Refresh greeter and time
|
|
|
|
useEffect(() => {
|
2021-06-13 23:21:35 +02:00
|
|
|
let interval: any;
|
|
|
|
|
|
|
|
// Start interval only when hideHeader is false
|
2021-10-22 13:31:02 +02:00
|
|
|
if (!props.config.hideHeader) {
|
2021-06-13 23:21:35 +02:00
|
|
|
interval = setInterval(() => {
|
|
|
|
setHeader({
|
|
|
|
dateTime: dateTime(),
|
2021-09-06 12:24:01 +02:00
|
|
|
greeting: greeter(),
|
|
|
|
});
|
2021-06-13 23:21:35 +02:00
|
|
|
}, 1000);
|
|
|
|
}
|
2021-05-09 19:02:50 +02:00
|
|
|
|
2021-06-13 01:06:42 +02:00
|
|
|
return () => clearInterval(interval);
|
2021-09-06 12:24:01 +02:00
|
|
|
}, []);
|
|
|
|
|
2021-10-27 11:52:57 +02:00
|
|
|
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]);
|
2021-09-06 13:22:47 +02:00
|
|
|
|
2021-05-08 18:49:08 +02:00
|
|
|
return (
|
|
|
|
<Container>
|
2021-10-22 13:31:02 +02:00
|
|
|
{!props.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-10-22 13:31:02 +02:00
|
|
|
{!props.config.hideHeader ? (
|
2021-09-06 12:24:01 +02:00
|
|
|
<header className={classes.Header}>
|
|
|
|
<p>{header.dateTime}</p>
|
|
|
|
<Link to="/settings" className={classes.SettingsLink}>
|
|
|
|
Go to Settings
|
|
|
|
</Link>
|
|
|
|
<span className={classes.HeaderMain}>
|
|
|
|
<h1>{header.greeting}</h1>
|
|
|
|
<WeatherWidget />
|
|
|
|
</span>
|
|
|
|
</header>
|
|
|
|
) : (
|
|
|
|
<div></div>
|
|
|
|
)}
|
|
|
|
|
2021-10-22 13:31:02 +02:00
|
|
|
{!props.config.hideApps ? (
|
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>
|
|
|
|
) : (
|
|
|
|
<div></div>
|
|
|
|
)}
|
|
|
|
|
2021-10-22 13:31:02 +02:00
|
|
|
{!props.config.hideCategories ? (
|
2021-09-06 12:24:01 +02:00
|
|
|
<Fragment>
|
|
|
|
<SectionHeadline title="Bookmarks" link="/bookmarks" />
|
|
|
|
{categoriesLoading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
<BookmarkGrid
|
2021-09-06 13:22:47 +02:00
|
|
|
categories={
|
2021-10-27 11:52:57 +02:00
|
|
|
!bookmarkSearchResult
|
2021-09-06 13:22:47 +02:00
|
|
|
? categories.filter(({ isPinned }) => isPinned)
|
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-09-06 12:24:01 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Fragment>
|
|
|
|
) : (
|
|
|
|
<div></div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<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
|
|
|
);
|
|
|
|
};
|
2021-05-08 18:49:08 +02:00
|
|
|
|
2021-05-13 18:23:12 +02:00
|
|
|
const mapStateToProps = (state: GlobalState) => {
|
|
|
|
return {
|
2021-05-23 18:38:39 +02:00
|
|
|
appsLoading: state.app.loading,
|
|
|
|
apps: state.app.apps,
|
|
|
|
categoriesLoading: state.bookmark.loading,
|
2021-09-06 12:24:01 +02:00
|
|
|
categories: state.bookmark.categories,
|
2021-10-22 13:31:02 +02:00
|
|
|
config: state.config.config,
|
2021-09-06 12:24:01 +02:00
|
|
|
};
|
|
|
|
};
|
2021-05-13 18:23:12 +02:00
|
|
|
|
2021-09-06 12:24:01 +02:00
|
|
|
export default connect(mapStateToProps, { getApps, getCategories })(Home);
|