From 687ef50bb501d4d27ced192af30f80cdc1f60380 Mon Sep 17 00:00:00 2001 From: WuYongJie Date: Sat, 12 Sep 2026 17:02:38 +0800 Subject: [PATCH] fix(home): workbench entry inside quick-links fold with unified styles - expand list now starts with a fixed workbench entry (router.push to the workspace picker) followed by configured links - replace next/link with button + router.push inside the nav item so styled-jsx scoped styles apply; fixes icon/label wrapping and keeps font size and spacing consistent across entries - tests updated for the new push-based navigation and roles --- .../components/SupplyChainSidebar.test.tsx | 3 +- .../layout/HomeQuickLinksNavItem.tsx | 35 ++++++++++---- .../layout/__tests__/AppSideNav.test.tsx | 6 ++- .../__tests__/HomeQuickLinksNavItem.test.tsx | 47 +++++++++++++------ 4 files changed, 66 insertions(+), 25 deletions(-) diff --git a/furniture-3d-system/src/app/supply-chain/components/SupplyChainSidebar.test.tsx b/furniture-3d-system/src/app/supply-chain/components/SupplyChainSidebar.test.tsx index d615a919..d352f6e7 100644 --- a/furniture-3d-system/src/app/supply-chain/components/SupplyChainSidebar.test.tsx +++ b/furniture-3d-system/src/app/supply-chain/components/SupplyChainSidebar.test.tsx @@ -5,6 +5,7 @@ const usePathnameMock = jest.fn() jest.mock('next/navigation', () => ({ usePathname: () => usePathnameMock(), + useRouter: () => ({ push: jest.fn(), refresh: jest.fn() }), })) describe('SupplyChainSidebar', () => { @@ -24,7 +25,7 @@ describe('SupplyChainSidebar', () => { ) expect(screen.getByRole('navigation', { name: '供应链导航' })).toBeInTheDocument() - const homeLink = screen.getByRole('link', { name: '首页' }) + const homeLink = screen.getByRole('button', { name: '首页' }) const workbenchLink = screen.getByRole('link', { name: '工作台' }) expect( homeLink.compareDocumentPosition(workbenchLink) & Node.DOCUMENT_POSITION_FOLLOWING, diff --git a/furniture-3d-system/src/components/layout/HomeQuickLinksNavItem.tsx b/furniture-3d-system/src/components/layout/HomeQuickLinksNavItem.tsx index ad8de425..c5089ac7 100644 --- a/furniture-3d-system/src/components/layout/HomeQuickLinksNavItem.tsx +++ b/furniture-3d-system/src/components/layout/HomeQuickLinksNavItem.tsx @@ -1,9 +1,8 @@ 'use client' import { useCallback, useEffect, useState } from 'react' -import { usePathname } from 'next/navigation' -import Link from 'next/link' -import { Home as HomeIcon, ChevronDown } from 'lucide-react' +import { usePathname, useRouter } from 'next/navigation' +import { Home as HomeIcon, ChevronDown, LayoutDashboard } from 'lucide-react' import { resolveHomeQuickLinkIcon } from '@/lib/home-quick-link-icons' import type { HomeQuickLinkDto } from '@/server/services/home-quick-links.service' @@ -11,8 +10,9 @@ export type HomeQuickLinksNavItemVariant = 'product' | 'supply-chain' /** * 左侧导航「首页」项(产品开发与供应链两个侧边栏共用): - * - 管理员配置了快捷网址时,点击「首页」展开/收起网址列表(点击网址新标签页跳转) - * - 没有配置时保持原行为:点击跳转首页工作台 + * - 管理员配置了快捷网址时,「首页」点击展开/收起网址列表(点击网址新标签页跳转), + * 其下新增「工作台」项指向原首页工作台选择页; + * - 没有配置时保持原行为:点击跳转首页工作台。 * 样式完全自包含,不依赖宿主侧边栏的全局类,保证两个侧边栏下表现一致。 */ export function HomeQuickLinksNavItem({ @@ -21,6 +21,7 @@ export function HomeQuickLinksNavItem({ variant?: HomeQuickLinksNavItemVariant }) { const pathname = usePathname() + const router = useRouter() const [links, setLinks] = useState([]) const [loaded, setLoaded] = useState(false) const [expanded, setExpanded] = useState(false) @@ -72,18 +73,32 @@ export function HomeQuickLinksNavItem({ /> ) : ( - router.push('/')} className={`home-quick-links-row ${active ? 'active' : ''}`} aria-current={active ? 'page' : undefined} > 首页 - + )} {hasLinks && expanded ? (
    +
  • + +
  • {links.map((link) => { const icon = resolveHomeQuickLinkIcon(link.iconKey) const Icon = icon.Icon @@ -220,6 +235,10 @@ export function HomeQuickLinksNavItem({ justify-content: center; border-radius: 5px; } + :global(.home-quick-links-workbench-icon) { + background: #eef4ff; + color: #2563eb; + } .home-quick-links-subitem-label { flex: 1 1 auto; min-width: 0; diff --git a/furniture-3d-system/src/components/layout/__tests__/AppSideNav.test.tsx b/furniture-3d-system/src/components/layout/__tests__/AppSideNav.test.tsx index 8153ed2a..2d8416d0 100644 --- a/furniture-3d-system/src/components/layout/__tests__/AppSideNav.test.tsx +++ b/furniture-3d-system/src/components/layout/__tests__/AppSideNav.test.tsx @@ -85,7 +85,9 @@ describe('AppSideNav', () => { />, ) const navigation = screen.getByRole('navigation', { name: '产品开发导航' }) - const labels = Array.from(navigation.querySelectorAll('a')).map((link) => link.textContent) + const labels = Array.from(navigation.querySelectorAll('a, button')).map( + (link) => link.textContent, + ) expect(labels).toEqual([ '首页', @@ -100,7 +102,7 @@ describe('AppSideNav', () => { 'AI出图', '安装演示', ]) - expect(screen.getByRole('link', { name: '首页' })).toHaveAttribute('href', '/') + expect(screen.getByRole('button', { name: '首页' })).toBeInTheDocument() expect(screen.queryByRole('button', { name: '用户中心' })).not.toBeInTheDocument() expect(container.querySelector('.app-side-nav-workbench')).toBeInTheDocument() }) diff --git a/furniture-3d-system/src/components/layout/__tests__/HomeQuickLinksNavItem.test.tsx b/furniture-3d-system/src/components/layout/__tests__/HomeQuickLinksNavItem.test.tsx index 713247b8..9863e407 100644 --- a/furniture-3d-system/src/components/layout/__tests__/HomeQuickLinksNavItem.test.tsx +++ b/furniture-3d-system/src/components/layout/__tests__/HomeQuickLinksNavItem.test.tsx @@ -3,6 +3,14 @@ import { HomeQuickLinksNavItem } from '@/components/layout/HomeQuickLinksNavItem const openSpy = jest.spyOn(window, 'open').mockImplementation(() => null) +const usePathnameMock = jest.fn() +const pushMock = jest.fn() + +jest.mock('next/navigation', () => ({ + usePathname: () => usePathnameMock(), + useRouter: () => ({ push: pushMock }), +})) + function buildLink(overrides: Partial> = {}) { return { id: 'link-1', @@ -28,21 +36,24 @@ beforeEach(() => { }) describe('HomeQuickLinksNavItem', () => { - it('接口失败时「首页」保持原有跳转行为', async () => { + it('接口失败时「首页」保持原有跳转行为(router.push /)', async () => { + usePathnameMock.mockReturnValue('/') global.fetch = jest.fn().mockRejectedValue(new Error('network error')) render() await waitFor(() => expect(global.fetch).toHaveBeenCalled()) - const homeLink = screen.getByRole('link', { name: /首页/ }) - expect(homeLink).toHaveAttribute('href', '/') + fireEvent.click(screen.getByRole('button', { name: /首页/ })) + expect(pushMock).toHaveBeenCalledWith('/') }) it('没有配置链接时「首页」保持原有跳转行为', async () => { + usePathnameMock.mockReturnValue('/') mockLinksResponse([]) render() await waitFor(() => expect(global.fetch).toHaveBeenCalled()) - expect(screen.getByRole('link', { name: /首页/ })).toHaveAttribute('href', '/') + fireEvent.click(screen.getByRole('button', { name: /首页/ })) + expect(pushMock).toHaveBeenCalledWith('/') }) it('配置了链接后点击「首页」展开网址列表', async () => { @@ -51,7 +62,7 @@ describe('HomeQuickLinksNavItem', () => { render() await waitFor(() => expect(screen.queryByText('素材库')).not.toBeInTheDocument()) - fireEvent.click(screen.getByRole('button', { name: /首页/ })) + fireEvent.click(screen.getByRole('button', { name: /首页/, expanded: false })) expect(screen.getByText('聚水潭')).toBeInTheDocument() expect(screen.getByText('素材库')).toBeInTheDocument() @@ -63,19 +74,33 @@ describe('HomeQuickLinksNavItem', () => { render() await waitFor(() => expect(screen.queryByText('聚水潭')).not.toBeInTheDocument()) - fireEvent.click(screen.getByRole('button', { name: /首页/ })) + fireEvent.click(screen.getByRole('button', { name: /首页/, expanded: false })) expect(screen.getByText('聚水潭')).toBeInTheDocument() - fireEvent.click(screen.getByRole('button', { name: /首页/ })) + fireEvent.click(screen.getByRole('button', { name: /首页/, expanded: true })) expect(screen.queryByText('聚水潭')).not.toBeInTheDocument() }) + it('展开列表第一项为「工作台」,点击当前页跳转工作台选择页', async () => { + usePathnameMock.mockReturnValue('/intake') + mockLinksResponse([buildLink()]) + render() + + fireEvent.click(await screen.findByRole('button', { name: /首页/, expanded: false })) + + const workbenchItems = screen.getAllByRole('button', { name: /工作台/ }) + expect(workbenchItems.length).toBeGreaterThan(0) + + fireEvent.click(workbenchItems[0]) + expect(pushMock).toHaveBeenCalledWith('/') + }) + it('点击网址项在新标签页打开对应网址', async () => { usePathnameMock.mockReturnValue('/') mockLinksResponse([buildLink()]) render() - fireEvent.click(await screen.findByRole('button', { name: /首页/ })) + fireEvent.click(await screen.findByRole('button', { name: /首页/, expanded: false })) fireEvent.click(screen.getByRole('button', { name: /聚水潭/ })) expect(openSpy).toHaveBeenCalledWith( @@ -85,9 +110,3 @@ describe('HomeQuickLinksNavItem', () => { ) }) }) - -const usePathnameMock = jest.fn() - -jest.mock('next/navigation', () => ({ - usePathname: () => usePathnameMock(), -}))