Files
monkey-ai-web/src/pages/Organization/components/OrgSubpageTabs.jsx
T

80 lines
3.4 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
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 } from 'react';
import { TabPane, Tabs } from '@/components/ui';
import { useSearchParams } from 'react-router-dom';
/**
* 企业中心合并页通用的二级页签容器。
*
* 页签状态同步到 ?tab=,让旧路由的重定向(如 /org/skill-library →
* /org/skill-center?tab=library)与收藏链接可以直达指定页签。
* tabs 数组允许传 falsy 项(站点级模块开关关掉的页签),会被过滤;
* 只剩一个可见页签时不渲染页签条,直接铺内容,避免"单页签"的怪异观感。
*/
const OrgSubpageTabs = ({ tabs }) => {
const [searchParams, setSearchParams] = useSearchParams();
const visible = (tabs || []).filter(Boolean);
const requested = searchParams.get('tab');
const activeKey =
visible.length > 0 && visible.some((item) => item.key === requested)
? requested
: visible[0]?.key;
// ?tab= 指向不存在/已被模块开关关掉的页签时,回落到第一个可见页签并把 URL
// 一并纠正——否则地址栏还挂着无效的 tab,转发给同事仍会落到这里再回落一次,
// URL 与实际内容长期不符。hook 顺序要求这个 effect 在任何 early return 之前。
useEffect(() => {
if (!activeKey || requested === activeKey) return;
const next = new URLSearchParams(searchParams);
next.set('tab', activeKey);
setSearchParams(next, { replace: true });
}, [requested, activeKey, searchParams, setSearchParams]);
if (visible.length === 0) return null;
if (visible.length === 1) return visible[0].node;
return (
<div className='org-subpage-tabs'>
<Tabs
type='line'
// 必须惰性渲染:Semi 默认 lazyRender=false 会把所有 TabPane 的 children
// 立即挂载,于是两个子页会同时拉数、各自的 PageGuide 抽屉(SideSheet 走
// Portal,不受非激活 pane 的 display:none 约束)在首访时叠着弹出,
// 且两个 guideKey 一起被 markSeen,用户再也看不到本该出现的那一份。
// keepDOM 默认 true,切回已访问过的页签不会重新拉数。
lazyRender
activeKey={activeKey}
onChange={(key) => {
// 就地改写 tab,保留其余 query(子页面可能带自己的筛选参数);
// 直接传对象字面量会把整串 query 冲掉。
const next = new URLSearchParams(searchParams);
next.set('tab', key);
setSearchParams(next, { replace: true });
}}
>
{visible.map((item) => (
<TabPane key={item.key} tab={item.label} itemKey={item.key}>
{item.node}
</TabPane>
))}
</Tabs>
</div>
);
};
export default OrgSubpageTabs;