/* 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 . 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 (
{ // 就地改写 tab,保留其余 query(子页面可能带自己的筛选参数); // 直接传对象字面量会把整串 query 冲掉。 const next = new URLSearchParams(searchParams); next.set('tab', key); setSearchParams(next, { replace: true }); }} > {visible.map((item) => ( {item.node} ))}
); }; export default OrgSubpageTabs;