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
This commit is contained in:
WuYongJie 2026-09-12 17:02:38 +08:00
parent 2b192afd86
commit 687ef50bb5
4 changed files with 66 additions and 25 deletions

View File

@ -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,

View File

@ -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<HomeQuickLinkDto[]>([])
const [loaded, setLoaded] = useState(false)
const [expanded, setExpanded] = useState(false)
@ -72,18 +73,32 @@ export function HomeQuickLinksNavItem({
/>
</button>
) : (
<Link
href="/"
<button
type="button"
onClick={() => router.push('/')}
className={`home-quick-links-row ${active ? 'active' : ''}`}
aria-current={active ? 'page' : undefined}
>
<HomeIcon className="row-icon" size={16} strokeWidth={1.5} />
<span className="row-label">首页</span>
</Link>
</button>
)}
{hasLinks && expanded ? (
<ul className="home-quick-links-sublist" role="list" aria-label="快捷网址列表">
<li>
<button
type="button"
onClick={() => router.push('/')}
className="home-quick-links-subitem"
aria-current={active ? 'page' : undefined}
>
<span className="home-quick-links-subitem-icon home-quick-links-workbench-icon">
<LayoutDashboard size={12} strokeWidth={1.8} />
</span>
<span className="home-quick-links-subitem-label">工作台</span>
</button>
</li>
{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;

View File

@ -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()
})

View File

@ -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<Record<string, unknown>> = {}) {
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(<HomeQuickLinksNavItem />)
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(<HomeQuickLinksNavItem />)
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(<HomeQuickLinksNavItem />)
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(<HomeQuickLinksNavItem />)
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(<HomeQuickLinksNavItem />)
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(<HomeQuickLinksNavItem />)
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(),
}))