Files
monkey-ai-web/src/pages/Setting/Operation/SettingsOrgUserNavModules.jsx
T
2026-11-18 19:09:00 +08:00

265 lines
8.5 KiB
React

/*
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 React, { useEffect, useState, useContext } from 'react';
import {
Banner,
Button,
Card,
Col,
Form,
Row,
Switch,
Typography,
} from '@/components/ui';
import { API, showError, showSuccess } from '../../../helpers';
import { useTranslation } from 'react-i18next';
import { StatusContext } from '../../../context/Status';
import {
getOrgUserDefaultNavModules,
normalizeOrgUserNavModules,
isHeaderModuleEnabled,
} from '../../../constants/headerNavModules';
const { Text } = Typography;
const OPTION_KEY = 'HeaderNavModulesOrgUser';
// 中部一级导航模块
const moduleKeys = [
'home',
'experienceCenter',
'userCenter',
'console',
'pricing',
'skillMarket',
'docs',
'weyai',
'about',
];
// 右侧按钮 / 头像菜单入口
const rightActionKeys = [
'tutorial',
'promotion',
'recharge',
// 下面 rightActionConfigs 每多一项,这里必须同步登记,否则开关只显示不落库
'weyaiDownload',
'notifications',
'theme',
'newYear',
'userSettings',
'keyManagement',
'orgCenter',
];
export default function SettingsOrgUserNavModules(props) {
const { t } = useTranslation();
const [loading, setLoading] = useState(false);
const [statusState, statusDispatch] = useContext(StatusContext);
const [modules, setModules] = useState({});
const [rightActions, setRightActions] = useState({});
// 仅展示对企业员工真实生效的开关:
// - home/about 在顶栏导航中无对应链接(全仓无 isHeaderModuleEnabled 消费点);
// - console 链接带 adminOnly 且 /console 不在企业员工路由放行清单,开了也进不去;
// - rightActions.orgCenter 全仓无读取点。
// 上述死开关从 UI 移除(保存结构仍保留 key,由预设默认值兜底),
// 避免"打开对应开关并保存"的承诺对这些项不成立。
const moduleConfigs = [
{ key: 'experienceCenter', title: t('体验中心') },
{ key: 'userCenter', title: t('用户中心') },
{ key: 'pricing', title: t('模型广场') },
// 技能超市是企业员工基线里唯一默认开启的 C 端入口(方案决策点 3):
// 纯只读、不产生 C 端消费,且是员工理解「我有哪些技能可用」的唯一途径。
// 其路由已在 App.jsx 的 ORG_USER_NAV_PATHS 中放行,开关与可达性一致。
{ key: 'skillMarket', title: t('技能超市') },
{ key: 'docs', title: t('文档') },
{ key: 'weyai', title: t('WeyAi') },
];
const rightActionConfigs = [
{ key: 'tutorial', title: t('新手教程按钮') },
{ key: 'promotion', title: t('推广按钮') },
{ key: 'recharge', title: t('充值按钮') },
{ key: 'weyaiDownload', title: t('下载 WeyAi 按钮') },
{ key: 'notifications', title: t('公告按钮') },
{ key: 'theme', title: t('主题按钮') },
{ key: 'newYear', title: t('新年按钮') },
{ key: 'userSettings', title: t('个人设置快捷入口') },
{ key: 'keyManagement', title: t('密钥管理快捷入口') },
];
// 以企业硬性默认预设为基线,叠加已保存配置 → 反映实际生效状态
useEffect(() => {
const eff = normalizeOrgUserNavModules(props.options?.[OPTION_KEY] || '');
const m = {};
moduleKeys.forEach((k) => (m[k] = isHeaderModuleEnabled(eff, k)));
setModules(m);
const r = {};
rightActionKeys.forEach((k) => (r[k] = eff.rightActions?.[k] !== false));
setRightActions(r);
}, [props.options]);
// 恢复企业默认(隐藏 C 端入口的预设)
const applyRecommended = () => {
const preset = getOrgUserDefaultNavModules();
const m = {};
moduleKeys.forEach((k) => (m[k] = isHeaderModuleEnabled(preset, k)));
setModules(m);
const r = {};
rightActionKeys.forEach((k) => (r[k] = preset.rightActions?.[k] !== false));
setRightActions(r);
showSuccess(t('已恢复企业默认配置,记得点击保存'));
};
const onSubmit = async () => {
setLoading(true);
try {
const out = {};
moduleKeys.forEach((k) => (out[k] = { enabled: !!modules[k] }));
out.rightActions = {};
rightActionKeys.forEach((k) => (out.rightActions[k] = !!rightActions[k]));
const value = JSON.stringify(out);
const res = await API.put('/api/option/', { key: OPTION_KEY, value });
if (res.data.success) {
showSuccess(t('保存成功'));
statusDispatch({
type: 'set',
payload: { ...statusState.status, [OPTION_KEY]: value },
});
if (props.refresh) await props.refresh();
} else {
showError(res.data.message);
}
} catch {
showError(t('保存失败,请重试'));
} finally {
setLoading(false);
}
};
const renderSwitchCard = (cfg, checked, onChange) => (
<Col key={cfg.key} xs={24} sm={12} md={8} lg={6} xl={6}>
<Card
style={{
borderRadius: '8px',
border: '1px solid var(--ui-color-border)',
background: 'var(--ui-color-bg-1)',
minHeight: '64px',
}}
shadows='hover'
styles={{ body: { padding: '16px' } }}
>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<div
style={{
fontWeight: 600,
fontSize: '14px',
color: 'var(--ui-color-text-0)',
}}
>
{cfg.title}
</div>
<Switch checked={checked} onChange={onChange} />
</div>
</Card>
</Col>
);
return (
<Card>
<Form.Section
text={t('企业员工顶栏')}
extraText={t(
'控制「仅企业资源」员工账号登录后顶栏可见的入口;普通用户不受影响',
)}
>
<Banner
type='info'
closeIcon={null}
description={t(
'仅对 org_only(仅企业资源)模式的员工账号生效。已默认隐藏 C 端入口(密钥/模型超市/用户中心/充值/推广等),开箱即生效;如需放开某项,打开对应开关并保存。',
)}
style={{ marginBlock: '16px' }}
/>
<div style={{ fontWeight: 600, marginBottom: '12px' }}>
{t('中部导航')}
</div>
<Row gutter={[16, 16]} style={{ marginBottom: '24px' }}>
{moduleConfigs.map((cfg) =>
renderSwitchCard(cfg, !!modules[cfg.key], (v) =>
setModules((prev) => ({ ...prev, [cfg.key]: v })),
),
)}
</Row>
<div style={{ fontWeight: 600, marginBottom: '12px' }}>
{t('右侧按钮 / 头像菜单')}
</div>
<Row gutter={[16, 16]} style={{ marginBottom: '24px' }}>
{rightActionConfigs.map((cfg) =>
renderSwitchCard(cfg, !!rightActions[cfg.key], (v) =>
setRightActions((prev) => ({ ...prev, [cfg.key]: v })),
),
)}
</Row>
<div
style={{
display: 'flex',
gap: '12px',
paddingTop: '8px',
borderTop: '1px solid var(--ui-color-border)',
}}
>
<Button type='tertiary' onClick={applyRecommended}>
{t('恢复企业默认')}
</Button>
<Button
type='primary'
onClick={onSubmit}
loading={loading}
style={{ minWidth: '100px' }}
>
{t('保存设置')}
</Button>
</div>
<Text
type='tertiary'
size='small'
style={{ display: 'block', marginTop: '8px' }}
>
{t(
'提示:本设置仅控制顶栏显隐;个人资源访问由企业「仅企业资源」模式在后端拦截。',
)}
</Text>
</Form.Section>
</Card>
);
}