2025-01-25 19:22:24 +01:00
|
|
|
/*
|
|
|
|
|
* 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-08-16 02:14:49 +02:00
|
|
|
import React, { ReactNode, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2025-07-28 02:38:18 +02:00
|
|
|
import { Direction, ThemeProvider } from '@mui/material/styles';
|
2025-01-25 19:22:24 +01:00
|
|
|
import { CacheProvider } from '@emotion/react';
|
2025-09-20 18:19:34 +02:00
|
|
|
import { AppTheme, getTheme } from '@/features/theme/services/AppThemes.ts';
|
2025-05-17 21:04:44 +02:00
|
|
|
import {
|
|
|
|
|
createUpdateMetadataServerSettings,
|
|
|
|
|
useMetadataServerSettings,
|
2025-08-15 22:02:58 +02:00
|
|
|
} from '@/features/settings/services/ServerSettingsMetadata.ts';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { useLocalStorage } from '@/base/hooks/useStorage.tsx';
|
2025-08-16 02:14:49 +02:00
|
|
|
import { MUI_THEME_MODE_KEY } from '@/lib/mui/MUI.constants.ts';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { MediaQuery } from '@/base/utils/MediaQuery.tsx';
|
|
|
|
|
import { makeToast } from '@/base/utils/Toast.ts';
|
2025-05-17 21:04:44 +02:00
|
|
|
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
|
2025-08-16 02:14:49 +02:00
|
|
|
import { createAndSetTheme } from '@/features/theme/services/ThemeCreator.ts';
|
2025-07-28 00:20:16 +02:00
|
|
|
import { AppStorage } from '@/lib/storage/AppStorage.ts';
|
2025-08-16 02:14:49 +02:00
|
|
|
import { DIRECTION_TO_CACHE } from '@/features/theme/ThemeDirectionCache.ts';
|
2025-09-20 18:19:34 +02:00
|
|
|
import { TAppThemeContext, ThemeMode } from '@/features/theme/AppTheme.types.ts';
|
2025-08-16 02:14:49 +02:00
|
|
|
|
|
|
|
|
export const AppThemeContext = React.createContext<TAppThemeContext>({
|
|
|
|
|
appTheme: 'default',
|
|
|
|
|
setAppTheme: (): void => {},
|
|
|
|
|
themeMode: ThemeMode.SYSTEM,
|
|
|
|
|
setThemeMode: (): void => {},
|
|
|
|
|
shouldUsePureBlackMode: false,
|
|
|
|
|
setShouldUsePureBlackMode: (): void => {},
|
|
|
|
|
dynamicColor: null,
|
|
|
|
|
setDynamicColor: (): void => {},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const useAppThemeContext = () => useContext(AppThemeContext);
|
2025-01-25 19:22:24 +01:00
|
|
|
|
|
|
|
|
export const AppThemeContextProvider = ({ children }: { children: ReactNode }) => {
|
2025-05-17 21:04:44 +02:00
|
|
|
const { t, i18n } = useTranslation();
|
2025-01-25 19:22:24 +01:00
|
|
|
const {
|
2025-07-28 00:04:51 +02:00
|
|
|
request: metadataServerSettingsRequest,
|
|
|
|
|
settings: { appTheme: serverAppTheme, themeMode, shouldUsePureBlackMode, customThemes },
|
2025-01-25 19:22:24 +01:00
|
|
|
} = useMetadataServerSettings();
|
2025-07-28 00:04:51 +02:00
|
|
|
const [localAppTheme, setLocalAppTheme] = useLocalStorage<AppTheme>(
|
|
|
|
|
'appTheme',
|
|
|
|
|
getTheme(serverAppTheme, customThemes),
|
|
|
|
|
);
|
2025-07-28 02:38:18 +02:00
|
|
|
const [localThemeMode] = useLocalStorage(MUI_THEME_MODE_KEY, themeMode);
|
2025-01-25 19:22:24 +01:00
|
|
|
|
|
|
|
|
const directionRef = useRef<Direction>('ltr');
|
|
|
|
|
|
|
|
|
|
const [systemThemeMode, setSystemThemeMode] = useState<ThemeMode>(MediaQuery.getSystemThemeMode());
|
2025-01-26 03:53:19 +01:00
|
|
|
const [dynamicColor, setDynamicColor] = useState<TAppThemeContext['dynamicColor']>(null);
|
2025-01-25 19:22:24 +01:00
|
|
|
|
2025-07-28 02:38:18 +02:00
|
|
|
const areMetadataServerSettingsReady =
|
|
|
|
|
!metadataServerSettingsRequest.loading && !metadataServerSettingsRequest.error;
|
|
|
|
|
|
|
|
|
|
const appTheme = areMetadataServerSettingsReady ? serverAppTheme : localAppTheme.id;
|
|
|
|
|
const actualThemeMode = areMetadataServerSettingsReady ? themeMode : localThemeMode;
|
2025-01-25 19:22:24 +01:00
|
|
|
const currentDirection = i18n.dir();
|
|
|
|
|
|
2025-05-17 21:04:44 +02:00
|
|
|
const updateSetting = createUpdateMetadataServerSettings<'appTheme' | 'themeMode' | 'shouldUsePureBlackMode'>((e) =>
|
|
|
|
|
makeToast(t('global.error.label.failed_to_save_changes'), 'error', getErrorMessage(e)),
|
|
|
|
|
);
|
|
|
|
|
|
2025-01-25 19:22:24 +01:00
|
|
|
const appThemeContext = useMemo(
|
2025-05-17 21:04:44 +02:00
|
|
|
() =>
|
|
|
|
|
({
|
|
|
|
|
appTheme,
|
|
|
|
|
setAppTheme: (value) => updateSetting('appTheme', value),
|
|
|
|
|
themeMode,
|
|
|
|
|
setThemeMode: (value) => updateSetting('themeMode', value),
|
|
|
|
|
shouldUsePureBlackMode,
|
|
|
|
|
setShouldUsePureBlackMode: (value) => updateSetting('shouldUsePureBlackMode', value),
|
|
|
|
|
dynamicColor,
|
|
|
|
|
setDynamicColor,
|
|
|
|
|
}) satisfies TAppThemeContext,
|
|
|
|
|
[themeMode, shouldUsePureBlackMode, appTheme, dynamicColor],
|
2025-01-25 19:22:24 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const theme = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
createAndSetTheme(
|
|
|
|
|
actualThemeMode as ThemeMode,
|
2025-07-28 00:04:51 +02:00
|
|
|
getTheme(appTheme, { [localAppTheme.id]: localAppTheme, ...customThemes }),
|
2025-05-17 21:04:44 +02:00
|
|
|
shouldUsePureBlackMode,
|
2025-01-25 19:22:24 +01:00
|
|
|
currentDirection,
|
2025-01-26 03:53:19 +01:00
|
|
|
dynamicColor,
|
2025-01-25 19:22:24 +01:00
|
|
|
),
|
2025-05-17 21:04:44 +02:00
|
|
|
[
|
|
|
|
|
actualThemeMode,
|
|
|
|
|
currentDirection,
|
|
|
|
|
systemThemeMode,
|
|
|
|
|
shouldUsePureBlackMode,
|
|
|
|
|
appTheme,
|
|
|
|
|
customThemes,
|
|
|
|
|
dynamicColor,
|
|
|
|
|
],
|
2025-01-25 19:22:24 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
const unsubscribe = MediaQuery.listenToSystemThemeChange(setSystemThemeMode);
|
|
|
|
|
|
|
|
|
|
return () => unsubscribe();
|
|
|
|
|
}, []);
|
|
|
|
|
|
2025-07-28 00:04:51 +02:00
|
|
|
useEffect(() => {
|
2025-07-28 02:38:18 +02:00
|
|
|
if (!areMetadataServerSettingsReady) {
|
2025-07-28 00:04:51 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (serverAppTheme !== localAppTheme.id) {
|
|
|
|
|
setLocalAppTheme(getTheme(serverAppTheme, customThemes));
|
|
|
|
|
}
|
|
|
|
|
}, [serverAppTheme, localAppTheme]);
|
|
|
|
|
|
2025-07-28 00:20:16 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
// The set background color is not necessary anymore, since the theme has been loaded
|
|
|
|
|
document.documentElement.style.backgroundColor = '';
|
|
|
|
|
|
|
|
|
|
AppStorage.local.setItem('theme_background', theme.palette.background.default);
|
|
|
|
|
}, [theme.palette.background.default]);
|
|
|
|
|
|
2025-01-25 19:22:24 +01:00
|
|
|
if (directionRef.current !== currentDirection) {
|
|
|
|
|
document.dir = currentDirection;
|
|
|
|
|
directionRef.current = currentDirection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<AppThemeContext.Provider value={appThemeContext}>
|
|
|
|
|
<CacheProvider value={DIRECTION_TO_CACHE[currentDirection]}>
|
|
|
|
|
<ThemeProvider theme={theme}>{children}</ThemeProvider>
|
|
|
|
|
</CacheProvider>
|
|
|
|
|
</AppThemeContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
};
|