Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
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, useCallback, useMemo } 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,
|
||||
clearUserData,
|
||||
showSuccess,
|
||||
} from '../../helpers';
|
||||
import { rememberUserLanguage } from '../../i18n/bootstrap';
|
||||
import { useIsMobile } from './useIsMobile';
|
||||
import { useSidebarCollapsed } from './useSidebarCollapsed';
|
||||
import { useMinimumLoadingTime } from './useMinimumLoadingTime';
|
||||
import { useOrgMembership } from './useOrgMembership';
|
||||
import {
|
||||
normalizeHeaderNavModules,
|
||||
normalizeOrgUserNavModules,
|
||||
} from '../../constants/headerNavModules';
|
||||
import {
|
||||
hasEnabledOrgCenterModule,
|
||||
normalizeSidebarModulesAdmin,
|
||||
} from '../../constants/sidebarModules';
|
||||
|
||||
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 || userState?.authChecking;
|
||||
const isLoading = useMinimumLoadingTime(loading, 200);
|
||||
|
||||
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 headerNavModulesConfig = statusState?.status?.HeaderNavModules;
|
||||
// 企业员工(仅企业资源模式)专用顶栏配置:隐藏 C 端入口,只保留企业相关
|
||||
const orgUserNavModulesConfig = statusState?.status?.HeaderNavModulesOrgUser;
|
||||
const isOrgUser = userState?.user?.org_only_mode === true;
|
||||
|
||||
// 使用useMemo确保headerNavModules正确响应statusState变化
|
||||
// 企业员工(org_only)硬性默认:以企业预设(隐藏 C 端入口)为基线,叠加管理员保存的覆盖配置。
|
||||
// 即「开箱即生效」,无需管理员先配置;管理员可在运营设置里再放开个别项。
|
||||
const headerNavModules = useMemo(() => {
|
||||
if (isOrgUser) {
|
||||
return normalizeOrgUserNavModules(orgUserNavModulesConfig);
|
||||
}
|
||||
return normalizeHeaderNavModules(headerNavModulesConfig);
|
||||
}, [isOrgUser, headerNavModulesConfig, orgUserNavModulesConfig]);
|
||||
|
||||
const sidebarModulesAdmin = useMemo(
|
||||
() =>
|
||||
normalizeSidebarModulesAdmin(statusState?.status?.SidebarModulesAdmin),
|
||||
[statusState?.status?.SidebarModulesAdmin],
|
||||
);
|
||||
|
||||
// 企业中心一级入口:用户属于企业才展示,并跟随后台 orgCenter 模块开关
|
||||
const orgCenterAvailable = useMemo(() => {
|
||||
const section = sidebarModulesAdmin?.orgCenter;
|
||||
return hasEnabledOrgCenterModule(section);
|
||||
}, [sidebarModulesAdmin]);
|
||||
// 仅当 orgCenter 功能可用时才拉取企业列表,避免无谓的接口请求
|
||||
const { hasOrg, orgEntryPath } = useOrgMembership(
|
||||
userState?.user?.id,
|
||||
orgCenterAvailable,
|
||||
);
|
||||
const showOrgEntry = hasOrg && orgCenterAvailable;
|
||||
|
||||
// 获取模型广场权限配置
|
||||
const pricingRequireAuth = useMemo(() => {
|
||||
if (headerNavModules?.pricing) {
|
||||
return typeof headerNavModules.pricing === 'object'
|
||||
? headerNavModules.pricing.requireAuth
|
||||
: false; // 默认不需要登录
|
||||
}
|
||||
return false; // 默认不需要登录
|
||||
}, [headerNavModules]);
|
||||
|
||||
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(() => {
|
||||
try {
|
||||
const iframe = document.querySelector('iframe');
|
||||
const cw = iframe && iframe.contentWindow;
|
||||
if (cw) {
|
||||
cw.postMessage({ themeMode: actualTheme }, '*');
|
||||
}
|
||||
} catch (e) {
|
||||
// Silently ignore cross-origin or access errors
|
||||
}
|
||||
}, [actualTheme]);
|
||||
|
||||
// Language change effect
|
||||
useEffect(() => {
|
||||
const handleLanguageChanged = (lng) => {
|
||||
setCurrentLang(lng);
|
||||
try {
|
||||
const iframe = document.querySelector('iframe');
|
||||
const cw = iframe && iframe.contentWindow;
|
||||
if (cw) {
|
||||
cw.postMessage({ lang: lng }, '*');
|
||||
}
|
||||
} catch (e) {
|
||||
// Silently ignore cross-origin or access errors
|
||||
}
|
||||
};
|
||||
|
||||
i18n.on('languageChanged', handleLanguageChanged);
|
||||
return () => {
|
||||
i18n.off('languageChanged', handleLanguageChanged);
|
||||
};
|
||||
}, [i18n]);
|
||||
|
||||
// Actions
|
||||
const logout = useCallback(async () => {
|
||||
await API.get('/api/user/logout');
|
||||
showSuccess(t('注销成功!'));
|
||||
userDispatch({ type: 'logout' });
|
||||
clearUserData();
|
||||
navigate('/org/login');
|
||||
}, [navigate, t, userDispatch]);
|
||||
|
||||
const handleLanguageChange = useCallback(
|
||||
(lang) => {
|
||||
// 记为显式选择:此后优先于 IP 国家判定(后台关闭顶栏切换时失效)
|
||||
rememberUserLanguage(lang);
|
||||
i18n.changeLanguage(lang);
|
||||
},
|
||||
[i18n],
|
||||
);
|
||||
|
||||
const handleThemeToggle = useCallback(
|
||||
(newTheme) => {
|
||||
if (
|
||||
!newTheme ||
|
||||
(newTheme !== 'light' && newTheme !== 'dark' && newTheme !== 'auto')
|
||||
) {
|
||||
return;
|
||||
}
|
||||
setTheme(newTheme);
|
||||
},
|
||||
[setTheme],
|
||||
);
|
||||
|
||||
const handleMobileMenuToggle = useCallback(() => {
|
||||
if (isMobile) {
|
||||
onMobileMenuToggle();
|
||||
} else {
|
||||
toggleCollapsed();
|
||||
}
|
||||
}, [isMobile, onMobileMenuToggle, toggleCollapsed]);
|
||||
|
||||
return {
|
||||
// State
|
||||
userState,
|
||||
statusState,
|
||||
isMobile,
|
||||
collapsed,
|
||||
logoLoaded,
|
||||
currentLang,
|
||||
location,
|
||||
isLoading,
|
||||
systemName,
|
||||
logo,
|
||||
isNewYear,
|
||||
isSelfUseMode,
|
||||
docsLink,
|
||||
isDemoSiteMode,
|
||||
isConsoleRoute,
|
||||
theme,
|
||||
drawerOpen,
|
||||
headerNavModules,
|
||||
sidebarModulesAdmin,
|
||||
pricingRequireAuth,
|
||||
showOrgEntry,
|
||||
orgEntryPath,
|
||||
isOrgUser,
|
||||
|
||||
// Actions
|
||||
logout,
|
||||
handleLanguageChange,
|
||||
handleThemeToggle,
|
||||
handleMobileMenuToggle,
|
||||
navigate,
|
||||
t,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user