2022-12-03 20:02:13 +00:00
|
|
|
import '/styles/globals.css'
|
2022-12-15 23:11:30 +00:00
|
|
|
import { Fragment, ReactElement, ReactNode } from 'react'
|
2022-12-03 20:02:13 +00:00
|
|
|
import Layout from '../components/layout'
|
|
|
|
import type { NextPage } from 'next'
|
|
|
|
import { AppProps } from 'next/app';
|
2022-12-14 18:35:27 +00:00
|
|
|
import { DefaultTheme, ThemeProvider } from 'styled-components';
|
|
|
|
import { createContext, useContext, useEffect, useState } from 'react'
|
2022-12-15 23:11:30 +00:00
|
|
|
import { darkTheme, GlobalStyle } from '../components/themes';
|
2022-12-15 20:24:38 +00:00
|
|
|
import { getTheme } from '../components/themeselector';
|
2022-12-03 20:02:13 +00:00
|
|
|
|
|
|
|
export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & {
|
|
|
|
getLayout?: (page: ReactElement) => ReactNode
|
|
|
|
}
|
|
|
|
|
|
|
|
export type AppPropsWithLayout = AppProps & {
|
|
|
|
Component: NextPageWithLayout
|
|
|
|
}
|
|
|
|
|
2022-12-14 18:35:27 +00:00
|
|
|
|
|
|
|
const ThemeUpdateContext = createContext(
|
|
|
|
(theme: DefaultTheme) => console.error("attempted to set theme outside of a ThemeUpdateContext.Provider")
|
|
|
|
)
|
|
|
|
|
|
|
|
export const useUpdateTheme = () => useContext(ThemeUpdateContext);
|
|
|
|
|
|
|
|
|
2022-12-03 20:02:13 +00:00
|
|
|
export default function Website({ Component, pageProps }: AppPropsWithLayout) {
|
2022-12-14 18:35:27 +00:00
|
|
|
const [selectedTheme, setselectedTheme] = useState(darkTheme);
|
2022-12-15 20:24:38 +00:00
|
|
|
|
2022-12-14 18:35:27 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-12-15 20:24:38 +00:00
|
|
|
const storedThemeIdTemp = localStorage.getItem("theme");
|
|
|
|
// get stored theme data
|
|
|
|
// if theme data differs set it
|
|
|
|
// if not just exit
|
|
|
|
if (storedThemeIdTemp && parseInt(storedThemeIdTemp) !== selectedTheme.themeId) {
|
|
|
|
setselectedTheme(getTheme(parseInt(storedThemeIdTemp)))
|
|
|
|
}
|
2022-12-14 18:35:27 +00:00
|
|
|
}, [selectedTheme])
|
|
|
|
|
|
|
|
|
2022-12-03 20:02:13 +00:00
|
|
|
// Use the layout defined at the page level, if available
|
|
|
|
const getLayout = Component.getLayout ?? ((page) => (
|
|
|
|
<Layout>{page}</Layout>))
|
|
|
|
|
2022-12-14 18:35:27 +00:00
|
|
|
return (
|
2022-12-15 23:11:30 +00:00
|
|
|
<Fragment>
|
|
|
|
<GlobalStyle />
|
|
|
|
<ThemeProvider theme={selectedTheme}>
|
2022-12-14 18:35:27 +00:00
|
|
|
<ThemeUpdateContext.Provider value={setselectedTheme}>
|
|
|
|
{getLayout(<Component {...pageProps} />)}
|
|
|
|
</ThemeUpdateContext.Provider>
|
|
|
|
</ThemeProvider>
|
2022-12-15 23:11:30 +00:00
|
|
|
</Fragment>
|
|
|
|
|
2022-12-14 18:35:27 +00:00
|
|
|
)
|
|
|
|
}
|