Files
suwayomi-material-you-webui/src/components/context/AppContext.tsx

107 lines
4.1 KiB
TypeScript
Raw Normal View History

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
2024-04-25 14:07:59 +02:00
import { Direction, StyledEngineProvider, ThemeProvider } from '@mui/material/styles';
2024-09-04 23:46:04 +02:00
import React, { useLayoutEffect, useMemo, useRef, useState } from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import { QueryParamProvider } from 'use-query-params';
import { ReactRouter6Adapter } from 'use-query-params/adapters/react-router-6';
2024-04-25 14:07:59 +02:00
import { useTranslation } from 'react-i18next';
import { CacheProvider, EmotionCache } from '@emotion/react';
import createCache from '@emotion/cache';
import { prefixer } from 'stylis';
import rtlPlugin from 'stylis-plugin-rtl';
2023-10-28 00:32:02 +02:00
import { createTheme } from '@/theme';
import { useLocalStorage } from '@/util/useStorage.tsx';
2024-09-04 23:46:04 +02:00
import { ThemeMode, ThemeModeContext } from '@/components/context/ThemeModeContext.tsx';
2023-10-28 00:32:02 +02:00
import { NavBarContextProvider } from '@/components/navbar/NavBarContextProvider';
import { LibraryOptionsContextProvider } from '@/components/library/LibraryOptionsProvider';
import { ActiveDevice, DEFAULT_DEVICE, setActiveDevice } from '@/util/device.ts';
2024-09-04 23:46:04 +02:00
import { MediaQuery } from '@/lib/ui/MediaQuery.tsx';
interface Props {
children: React.ReactNode;
}
2024-04-25 14:07:59 +02:00
const directionToCache: Record<Direction, EmotionCache> = {
ltr: createCache({
key: 'muiltr',
}),
rtl: createCache({
key: 'muirtl',
stylisPlugins: [prefixer, rtlPlugin],
}),
};
2023-10-28 00:32:02 +02:00
export const AppContext: React.FC<Props> = ({ children }) => {
2024-04-25 14:07:59 +02:00
const directionRef = useRef<Direction>('ltr');
const { i18n } = useTranslation();
const currentDirection = i18n.dir();
if (directionRef.current !== currentDirection) {
document.dir = currentDirection;
directionRef.current = currentDirection;
}
2024-09-04 23:46:04 +02:00
const [systemThemeMode, setSystemThemeMode] = useState<ThemeMode>(MediaQuery.getSystemThemeMode());
useLayoutEffect(() => {
const unsubscribe = MediaQuery.listenToSystemThemeChange(setSystemThemeMode);
return () => unsubscribe();
}, []);
const [themeMode, setThemeMode] = useLocalStorage<ThemeMode>('themeMode', ThemeMode.SYSTEM);
2024-09-05 00:08:23 +02:00
const [pureBlackMode, setPureBlackMode] = useLocalStorage<boolean>('pureBlackMode', false);
const [activeDevice, setActiveDeviceContext] = useLocalStorage('activeDevice', DEFAULT_DEVICE);
const darkThemeContext = useMemo(
() => ({
2024-09-04 23:46:04 +02:00
themeMode,
setThemeMode,
2024-09-05 00:08:23 +02:00
pureBlackMode,
setPureBlackMode,
}),
2024-09-05 00:08:23 +02:00
[themeMode, pureBlackMode],
);
const activeDeviceContext = useMemo(
() => ({ activeDevice, setActiveDevice: setActiveDeviceContext }),
[activeDevice],
);
2024-09-04 23:46:04 +02:00
const theme = useMemo(
2024-09-05 00:08:23 +02:00
() => createTheme(themeMode, currentDirection, undefined, pureBlackMode),
[themeMode, currentDirection, systemThemeMode, pureBlackMode],
2024-09-04 23:46:04 +02:00
);
setActiveDevice(activeDevice);
return (
2023-10-04 01:48:19 +02:00
<Router>
<StyledEngineProvider injectFirst>
2024-04-25 14:07:59 +02:00
<CacheProvider value={directionToCache[currentDirection]}>
<ThemeProvider theme={theme}>
2024-09-04 23:46:04 +02:00
<ThemeModeContext.Provider value={darkThemeContext}>
2024-04-25 14:07:59 +02:00
<QueryParamProvider adapter={ReactRouter6Adapter}>
<LibraryOptionsContextProvider>
<NavBarContextProvider>
<ActiveDevice.Provider value={activeDeviceContext}>
{children}
</ActiveDevice.Provider>
</NavBarContextProvider>
</LibraryOptionsContextProvider>
</QueryParamProvider>
2024-09-04 23:46:04 +02:00
</ThemeModeContext.Provider>
2024-04-25 14:07:59 +02:00
</ThemeProvider>
</CacheProvider>
2023-10-04 01:48:19 +02:00
</StyledEngineProvider>
</Router>
);
};