import type { DockTab } from '../../store/app/appTypes'; /** 页签数量上限:超出后淘汰最旧的非当前页签(LRU 式),避免无限堆积。 */ export const MAX_DOCK_TABS = 20; function clampToLimit(list: readonly DockTab[], activeKey: string): DockTab[] { // 未超限时保留原引用,避免触发无谓的 tab 列表重渲染 if (list.length <= MAX_DOCK_TABS) return list as DockTab[]; // 恢复/迁移或新增后超出上限:保留当前页签 + 最新的其余页签(LRU 式淘汰) const active = list.find((tab) => tab.key === activeKey); const rest = list.filter((tab) => tab.key !== activeKey); const keptRest = rest.slice(rest.length - (MAX_DOCK_TABS - 1)); return active ? [active, ...keptRest] : keptRest.slice(-MAX_DOCK_TABS); } /** * 路由页签合并:按 pathname 建 tab,重复时更新标题,并始终把列表压回上限。 */ export function upsertDockTab( tabs: readonly DockTab[], activeKey: string, label: string, ): DockTab[] { const existing = tabs.find((tab) => tab.key === activeKey); if (existing) { const updated = existing.label === label ? tabs : tabs.map((tab) => (tab.key === activeKey ? { ...tab, label } : tab)); return clampToLimit(updated, activeKey); } return clampToLimit([...tabs, { key: activeKey, label }], activeKey); }