import { colorsTuple, createTheme, DEFAULT_THEME, MantineProvider, mergeMantineTheme, } from '@mantine/core'; import { Notifications } from '@mantine/notifications'; import { useCommonHooks, useQueryAPI } from 'hooks'; import { useEffect, useState } from 'react'; import { createBrowserRouter, createRoutesFromElements, Navigate, Route, RouterProvider, } from 'react-router-dom'; import { Loading, MainLayout } from './components'; import '@mantine/core/styles.css'; import '@mantine/notifications/styles.css'; import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; import { Homepage, Settings } from 'pages'; import type { Theme, ThemePlaylist } from 'types'; function App() { const { data: allThemes, isFetched: isAllThemesFetched } = useQueryAPI({ endpoint: 'api/themes', }); const { data: themePlaylists, isFetched: isThemePlaylistsFetched } = useQueryAPI({ endpoint: 'api/themePlaylists', }); const { theme, setTheme, themeName, handleThemeChange, currentThemePlaylist } = useCommonHooks(); const [loaded, setLoaded] = useState(false); const overrides = createTheme({ primaryColor: 'primary', colors: { primary: colorsTuple(theme.primaryColor), accent: colorsTuple(theme.accentColor), background: colorsTuple(theme.backgroundColor), border: colorsTuple(theme.borderColor), search: colorsTuple(theme.searchColor), }, white: theme?.backgroundColor, fontFamily: '-apple-system,Jost,BlinkMacSystemFont,Helvetica Neue,Roboto,sans-serif', autoContrast: true, }); const mantineTheme = mergeMantineTheme(DEFAULT_THEME, overrides); const router = createBrowserRouter( createRoutesFromElements( }> } /> } /> } /> } /> , ), ); useEffect(() => { if (allThemes && themeName) { if (currentThemePlaylist === 'All') { if (themeName !== 'random') { const temp = allThemes.find((t: Theme) => t.name === themeName); setTheme(temp); } else { handleThemeChange(allThemes, false); } } else if (currentThemePlaylist === 'Time of Day') { const themeList = JSON.parse( themePlaylists.find((t: ThemePlaylist) => t.name === currentThemePlaylist) .themeList, ); setTheme( allThemes.find( (t: Theme) => t.id === Number(themeList[Math.floor(new Date().getHours() / 6)]), ), ); } else if (currentThemePlaylist === 'Backgrounds') { handleThemeChange( allThemes.filter((a: any) => !a.backgroundImage.toLowerCase().includes('none')), false, ); } else if (currentThemePlaylist === 'No Backgrounds') { handleThemeChange( allThemes.filter((a: any) => a.backgroundImage.toLowerCase().includes('none')), false, ); } else { const themeList = JSON.parse( themePlaylists.find((t: ThemePlaylist) => t.name === currentThemePlaylist) .themeList, ); const filteredThemes = allThemes.filter((t: Theme) => themeList.includes(t.id)); setTheme('random'); handleThemeChange(filteredThemes, false); } setLoaded(true); } }, [ isAllThemesFetched, themeName, allThemes, currentThemePlaylist, isThemePlaylistsFetched, ]); return ( <> {theme && ( )} ); } export default App;