- Feature: Introduce 'auto' theme mode
- Detect system preference via matchMedia('(prefers-color-scheme: dark)')
- Add useActualTheme context to expose the effective theme ('light'|'dark')
- Persist selected mode in localStorage ('theme-mode') with 'auto' as default
- Apply/remove `dark` class on <html> and sync `theme-mode` on <body>
- Broadcast effective theme to iframes
- UI: Redesign ThemeToggle with Dropdown items and custom highlight
- Replace non-existent IconMonitor with IconRefresh
- Use Dropdown.Menu + Dropdown.Item with built-in icon prop
- Selected state uses custom background highlight; hover state preserved
- Remove checkmark; selection relies on background styling
- Current button icon reflects selected mode
- Performance: reduce re-renders and unnecessary effects
- Memoize theme options and current button icon (useMemo)
- Simplify handleThemeToggle to accept only explicit modes ('light'|'dark'|'auto')
- Minimize useEffect dependencies; remove unrelated deps
- Header: streamline useHeaderBar
- Use useActualTheme for iframe theme messaging
- Remove unused statusDispatch
- Remove isNewYear from theme effect dependencies
- Home: send effective theme (useActualTheme) to external content iframes
- i18n: add/enhance theme-related copy in locales (en/zh)
- Chore: minor code cleanup and consistency
- Improve readability and maintainability
- Lint clean; no functional regressions
150 lines
4.3 KiB
JavaScript
150 lines
4.3 KiB
JavaScript
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import { useState, useEffect, useContext } from 'react';
|
|
import { useNavigate, useLocation } from 'react-router-dom';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { UserContext } from '../../context/User';
|
|
import { StatusContext } from '../../context/Status';
|
|
import { useSetTheme, useTheme, useActualTheme } from '../../context/Theme';
|
|
import { getLogo, getSystemName, API, showSuccess } from '../../helpers';
|
|
import { useIsMobile } from './useIsMobile';
|
|
import { useSidebarCollapsed } from './useSidebarCollapsed';
|
|
import { useMinimumLoadingTime } from './useMinimumLoadingTime';
|
|
|
|
export const useHeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
|
|
const { t, i18n } = useTranslation();
|
|
const [userState, userDispatch] = useContext(UserContext);
|
|
const [statusState] = useContext(StatusContext);
|
|
const isMobile = useIsMobile();
|
|
const [collapsed, toggleCollapsed] = useSidebarCollapsed();
|
|
const [logoLoaded, setLogoLoaded] = useState(false);
|
|
const navigate = useNavigate();
|
|
const [currentLang, setCurrentLang] = useState(i18n.language);
|
|
const location = useLocation();
|
|
|
|
const loading = statusState?.status === undefined;
|
|
const isLoading = useMinimumLoadingTime(loading);
|
|
|
|
const systemName = getSystemName();
|
|
const logo = getLogo();
|
|
const currentDate = new Date();
|
|
const isNewYear = currentDate.getMonth() === 0 && currentDate.getDate() === 1;
|
|
|
|
const isSelfUseMode = statusState?.status?.self_use_mode_enabled || false;
|
|
const docsLink = statusState?.status?.docs_link || '';
|
|
const isDemoSiteMode = statusState?.status?.demo_site_enabled || false;
|
|
|
|
const isConsoleRoute = location.pathname.startsWith('/console');
|
|
|
|
const theme = useTheme();
|
|
const actualTheme = useActualTheme();
|
|
const setTheme = useSetTheme();
|
|
|
|
// Logo loading effect
|
|
useEffect(() => {
|
|
setLogoLoaded(false);
|
|
if (!logo) return;
|
|
const img = new Image();
|
|
img.src = logo;
|
|
img.onload = () => setLogoLoaded(true);
|
|
}, [logo]);
|
|
|
|
// Send theme to iframe
|
|
useEffect(() => {
|
|
const iframe = document.querySelector('iframe');
|
|
if (iframe) {
|
|
iframe.contentWindow.postMessage({ themeMode: actualTheme }, '*');
|
|
}
|
|
}, [actualTheme]);
|
|
|
|
// Language change effect
|
|
useEffect(() => {
|
|
const handleLanguageChanged = (lng) => {
|
|
setCurrentLang(lng);
|
|
const iframe = document.querySelector('iframe');
|
|
if (iframe) {
|
|
iframe.contentWindow.postMessage({ lang: lng }, '*');
|
|
}
|
|
};
|
|
|
|
i18n.on('languageChanged', handleLanguageChanged);
|
|
return () => {
|
|
i18n.off('languageChanged', handleLanguageChanged);
|
|
};
|
|
}, [i18n]);
|
|
|
|
// Actions
|
|
const logout = async () => {
|
|
await API.get('/api/user/logout');
|
|
showSuccess(t('注销成功!'));
|
|
userDispatch({ type: 'logout' });
|
|
localStorage.removeItem('user');
|
|
navigate('/login');
|
|
};
|
|
|
|
const handleLanguageChange = (lang) => {
|
|
i18n.changeLanguage(lang);
|
|
};
|
|
|
|
const handleThemeToggle = (newTheme) => {
|
|
if (!newTheme || (newTheme !== 'light' && newTheme !== 'dark' && newTheme !== 'auto')) {
|
|
return;
|
|
}
|
|
setTheme(newTheme);
|
|
};
|
|
|
|
const handleMobileMenuToggle = () => {
|
|
if (isMobile) {
|
|
onMobileMenuToggle();
|
|
} else {
|
|
toggleCollapsed();
|
|
}
|
|
};
|
|
|
|
return {
|
|
// State
|
|
userState,
|
|
statusState,
|
|
isMobile,
|
|
collapsed,
|
|
logoLoaded,
|
|
currentLang,
|
|
location,
|
|
isLoading,
|
|
systemName,
|
|
logo,
|
|
isNewYear,
|
|
isSelfUseMode,
|
|
docsLink,
|
|
isDemoSiteMode,
|
|
isConsoleRoute,
|
|
theme,
|
|
drawerOpen,
|
|
|
|
// Actions
|
|
logout,
|
|
handleLanguageChange,
|
|
handleThemeToggle,
|
|
handleMobileMenuToggle,
|
|
navigate,
|
|
t,
|
|
};
|
|
};
|