Refactor
This commit is contained in:
@@ -1,18 +1,16 @@
|
||||
import { Stack } from '@mantine/core';
|
||||
import { useDisclosure, useLocalStorage } from '@mantine/hooks';
|
||||
import { date, greet, useQueryAPI, useWeatherAPI } from 'hooks';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useQueryAPI, useWeatherAPI } from '../../hooks/useAPI';
|
||||
import stackClasses from '../../styles/Stack.module.css';
|
||||
import Applications from './components/Applications';
|
||||
import Bookmarks from './components/Bookmarks';
|
||||
import Header from './components/Header';
|
||||
import Search from './components/Search';
|
||||
import { stackClasses } from 'styles';
|
||||
import type { CurrentWeather, OutlookWeather } from 'types';
|
||||
import Applications from './components/Applications/Applications';
|
||||
import Bookmarks from './components/Bookmarks/Bookmarks';
|
||||
import Header from './components/Header/Header';
|
||||
import Search from './components/Search/Search';
|
||||
import SettingsButton from './components/SettingsButton';
|
||||
import type { CurrentWeather, OutlookWeather } from './types/weather';
|
||||
|
||||
export default function Homepage({ theme }: any) {
|
||||
const { data, isFetching } = useQueryAPI({ endpoint: 'api/messages' });
|
||||
const [displayedGreeting, setDisplayedGreeting] = useState('');
|
||||
export function Homepage({ theme }: any) {
|
||||
const [appFilter, setAppFilter] = useLocalStorage<[any]>({
|
||||
key: 'appFilter',
|
||||
defaultValue: ['All'],
|
||||
@@ -21,8 +19,12 @@ export default function Homepage({ theme }: any) {
|
||||
key: 'bookmarkFilter',
|
||||
defaultValue: ['All'],
|
||||
});
|
||||
const { data, isFetching } = useQueryAPI({ endpoint: 'api/messages' });
|
||||
const { data: basicWeather } = useWeatherAPI({ endpoint: 'baseline' });
|
||||
const { data: weather } = useWeatherAPI({ endpoint: 'current' });
|
||||
const { data: forecast } = useWeatherAPI({ endpoint: 'forecast' });
|
||||
const { data: hourly } = useWeatherAPI({ endpoint: 'hourly' });
|
||||
const [displayedGreeting, setDisplayedGreeting] = useState('');
|
||||
const [current, setCurrent] = useState<CurrentWeather>({
|
||||
icon: '',
|
||||
temperature: 0,
|
||||
@@ -45,42 +47,9 @@ export default function Homepage({ theme }: any) {
|
||||
windDirection: 'N',
|
||||
},
|
||||
]);
|
||||
const { data: forecast } = useWeatherAPI({ endpoint: 'forecast' });
|
||||
const { data: hourly } = useWeatherAPI({ endpoint: 'hourly' });
|
||||
const isMobile = window.matchMedia('(max-width: 600px)').matches;
|
||||
const [weatherModalOpened, { open: openWeatherModal, close: closeWeatherModal }] =
|
||||
useDisclosure(false);
|
||||
const isMobile = window.matchMedia('(max-width: 600px)').matches;
|
||||
function date() {
|
||||
const currentDate = new Date();
|
||||
const dateOptions: Intl.DateTimeFormatOptions = !isMobile
|
||||
? {
|
||||
weekday: 'long',
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
}
|
||||
: {};
|
||||
const date = currentDate.toLocaleDateString('en-US', dateOptions);
|
||||
return date;
|
||||
}
|
||||
function greet(list: any) {
|
||||
const currentTime = new Date();
|
||||
const greet = Math.floor(currentTime.getHours() / 6);
|
||||
const easterEggChance = Math.floor(Math.random() * 100);
|
||||
let timeBased: any = [];
|
||||
let customGreetings: any = [];
|
||||
for (const l of list) {
|
||||
if (l.customGreeting === 0) {
|
||||
timeBased = timeBased.concat(l);
|
||||
} else {
|
||||
customGreetings = customGreetings.concat(l);
|
||||
}
|
||||
}
|
||||
if (easterEggChance > 10 || customGreetings.length === 0) {
|
||||
return timeBased[greet].message;
|
||||
}
|
||||
return customGreetings[Math.floor(Math.random() * customGreetings.length)].message;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
@@ -160,12 +129,12 @@ export default function Homepage({ theme }: any) {
|
||||
openWeatherModal={openWeatherModal}
|
||||
/>
|
||||
<Header
|
||||
date={date()}
|
||||
date={date(isMobile)}
|
||||
greet={displayedGreeting}
|
||||
isMobile={isMobile}
|
||||
current={current}
|
||||
outlook={outlook}
|
||||
setCurrent={setCurrent}
|
||||
outlook={outlook}
|
||||
weather={weather}
|
||||
basicWeather={basicWeather}
|
||||
forecast={forecast}
|
||||
|
||||
Reference in New Issue
Block a user