From d3a2504d371387d9810ff706128a63380ff93823 Mon Sep 17 00:00:00 2001 From: XiaoDaiGua-Ray <51957438+XiaoDaiGua-Ray@users.noreply.github.com> Date: Fri, 27 Nov 2026 16:56:57 +0800 Subject: [PATCH] refactor: migrate deprecated APIs and prefer component hooks --- AGENTS.md | 2 + __test__/app/wrapper-lifecycle.spec.tsx | 14 ++++- __test__/hooks/useAppRoot.spec.ts | 51 ------------------- __test__/hooks/useDevice.spec.ts | 13 +---- __test__/hooks/useI18n.spec.ts | 4 +- __test__/hooks/useSiderBar.spec.ts | 10 ++-- __test__/layout/preferences.spec.ts | 10 +++- __test__/router/initialization.spec.ts | 40 +++++++++++++-- __test__/utils/canUseDom.ts | 9 +--- __test__/utils/isBrowser.ts | 6 +-- .../app/AppAvatar/AccountAvatar.tsx | 11 ++-- .../app-components/app/AppAvatar/index.tsx | 5 +- apps/admin/src/hooks/template/useAppRoot.ts | 12 +++-- apps/admin/src/hooks/template/useI18n.ts | 12 +---- .../components/SettingDrawer/index.tsx | 4 +- apps/admin/src/locales/index.ts | 9 ++-- apps/admin/src/router/index.ts | 25 +++++++-- .../src/views/demo/curd/TagEditorModal.tsx | 6 +-- .../src/views/demo/curd/interactions.tsx | 1 - apps/admin/src/views/demo/curd/projects.tsx | 14 ++--- .../admin/src/views/demo/modal-demo/index.tsx | 11 ++-- apps/docs/en/guide/curd-examples.md | 4 +- apps/docs/en/reference/hooks.md | 2 +- apps/docs/en/reference/table-pro.md | 20 ++------ apps/docs/guide/curd-examples.md | 4 +- apps/docs/reference/hooks.md | 2 +- apps/docs/reference/table-pro.md | 20 ++------ docs/curd-conventions.md | 6 +++ docs/toolchain-upgrade.md | 6 +++ eslint.config.mjs | 21 +++++--- harness/context/toolchain.md | 2 + packages/ui/src/RModal/index.ts | 10 +++- packages/ui/src/RModal/src/hooks/useModal.ts | 14 ++--- packages/ui/src/RTablePro/README.md | 6 +-- tsconfig.eslint-scripts.json | 21 ++++++++ 35 files changed, 223 insertions(+), 184 deletions(-) delete mode 100644 __test__/hooks/useAppRoot.spec.ts create mode 100644 tsconfig.eslint-scripts.json diff --git a/AGENTS.md b/AGENTS.md index 85102e8f..cec2d24b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -5,6 +5,8 @@ A Vue 3 + TSX admin template with an admin application, a documentation site, an - To locate an implementation, choose a topic from the [task index](llms.txt) or `pnpm harness context `. Consult the [architecture map](docs/architecture.md) for changes that cross module boundaries. - Check `git status` first and preserve existing changes. Trace callers and existing data contracts before changing routing, persistence, requests, or shared components. - Prefer feature entry points for runtime imports. Follow the [runtime import boundaries](harness/README.md#runtime-import-boundaries) when adding exports or changing what loads at startup or on demand. +- Prefer supported component hooks for imperative actions. Bind the hook's registration callback when required and call its returned actions. Follow the [component action conventions](docs/curd-conventions.md#component-actions) for tables, forms, and modals. +- Use `void 0` for an undefined runtime value and `undefined` in type annotations. Use the recommended replacements for APIs marked `@deprecated`. - For complex tasks, the lead agent may choose to use subagents under the [delegation guidelines](harness/README.md#using-subagents-when-useful), when the host supports them and current permissions allow it. Delegation needs no separate approval. The lead agent owns integration and final verification. - Write agent guidance and internal engineering documentation in English. Preserve the intended languages of product copy and reader-facing documentation. - Follow the [comment style](docs/comment-style.md) when adding or editing comments. Maintain both Chinese and English product copy. For visible behavior changes, use [browser verification](harness/README.md#browser-verification) only for affected pages, actions, and dimensions. diff --git a/__test__/app/wrapper-lifecycle.spec.tsx b/__test__/app/wrapper-lifecycle.spec.tsx index f0a81863..26e9741f 100644 --- a/__test__/app/wrapper-lifecycle.spec.tsx +++ b/__test__/app/wrapper-lifecycle.spec.tsx @@ -28,15 +28,25 @@ vi.mock('@/hooks', () => ({ it('honors avatar shape, native size, and keyboard focus', () => { const wrapper = mount(AppAvatar, { - props: { round: false, size: 42, objectFit: 'contain' }, + props: { + round: false, + size: 42, + objectFit: 'contain', + color: 'rgb(18, 52, 86)', + }, }) try { - expect(wrapper.findComponent(NAvatar).props()).toMatchObject({ + const avatar = wrapper.findComponent(NAvatar) + + expect(avatar.props()).toMatchObject({ round: false, size: 42, objectFit: 'contain', }) + expect((avatar.element as HTMLElement).style.backgroundColor).toBe( + 'rgb(18, 52, 86)', + ) expect(wrapper.findComponent(NButton).props('focusable')).toBe(true) } finally { wrapper.unmount() diff --git a/__test__/hooks/useAppRoot.spec.ts b/__test__/hooks/useAppRoot.spec.ts deleted file mode 100644 index f878e369..00000000 --- a/__test__/hooks/useAppRoot.spec.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { useAppRoot } from '../../apps/admin/src/hooks/template/useAppRoot' -import setupMiniApp from '../utils/setupMiniApp' - -describe('useAppRoot', async () => { - await setupMiniApp() - - const { setRootRoute } = useAppRoot() - - it(`should return '/test' and 'test'`, () => { - setRootRoute({ - path: '/test', - name: 'test', - }) - - const { getRootPath, getRootName } = useAppRoot() - - expect(getRootPath.value).toBe('/test') - expect(getRootName.value).toBe('test') - }) - - it(`should be returned a object like: {path: /test2, name: test2}`, () => { - const baseRootRoute = { - path: '/test2', - name: 'test2', - } - - setRootRoute(baseRootRoute) - - const { getRootRoute } = useAppRoot() - - expect(getRootRoute.value).toEqual(baseRootRoute) - }) - - it('should update root route when setRootRoute is called', () => { - const baseRootRoute = { - path: '/test3', - name: 'test3', - } - - setRootRoute({ - path: '/test3', - name: 'test3', - }) - - const { getRootPath, getRootName, getRootRoute } = useAppRoot() - - expect(getRootPath.value).toBe('/test3') - expect(getRootName.value).toBe('test3') - expect(getRootRoute.value).toEqual(baseRootRoute) - }) -}) diff --git a/__test__/hooks/useDevice.spec.ts b/__test__/hooks/useDevice.spec.ts index c438458f..4fb5e8ac 100644 --- a/__test__/hooks/useDevice.spec.ts +++ b/__test__/hooks/useDevice.spec.ts @@ -2,18 +2,7 @@ import { useDevice } from '../../apps/admin/src/hooks/web/useDevice' describe('useDevice', () => { const addEventListenerSpy = vi.spyOn(window, 'addEventListener') - const matchMediaSpy = vi - .spyOn(window, 'matchMedia') - .mockImplementation((query) => ({ - matches: false, - media: query, - onchange: null, - addListener: vi.fn(), - removeListener: vi.fn(), - addEventListener: vi.fn(), - removeEventListener: vi.fn(), - dispatchEvent: vi.fn(), - })) + const matchMediaSpy = vi.spyOn(window, 'matchMedia') beforeEach(() => { addEventListenerSpy.mockClear() diff --git a/__test__/hooks/useI18n.spec.ts b/__test__/hooks/useI18n.spec.ts index 2edd9991..08b8f021 100644 --- a/__test__/hooks/useI18n.spec.ts +++ b/__test__/hooks/useI18n.spec.ts @@ -6,7 +6,6 @@ const { state } = vi.hoisted(() => ({ state: { instance: void 0 as | { - mode: string global: { t: (key: string, ...args: unknown[]) => string locale: Ref @@ -24,7 +23,6 @@ vi.mock('../../apps/admin/src/locales', () => ({ beforeEach(() => { state.instance = { - mode: 'composition', global: { t: (key) => key, locale: ref('zh-CN') }, } }) @@ -46,12 +44,12 @@ describe('useI18n namespaces', () => { expect(hook.t('code')).toBe('hooks.code') expectTypeOf(hook.t('code')).toEqualTypeOf() state.instance = { - mode: 'composition', global: { t: (key) => `translated:${key}`, locale: ref('zh-CN') }, } expect(hook.t('code')).toBe('translated:hooks.code') hook.locale('en-US') expect(state.instance.global.locale.value).toBe('en-US') + expect(document.documentElement.lang).toBe('en-US') state.instance.global.t = (key) => `updated:${key}` expect(hook.t('code')).toBe('updated:hooks.code') }) diff --git a/__test__/hooks/useSiderBar.spec.ts b/__test__/hooks/useSiderBar.spec.ts index e1519d11..16ef62f7 100644 --- a/__test__/hooks/useSiderBar.spec.ts +++ b/__test__/hooks/useSiderBar.spec.ts @@ -1,5 +1,4 @@ import { APP_CATCH_KEY } from '@/app-config' -import { useAppRoot } from '@/hooks/template/useAppRoot' import { useSiderBar } from '@/hooks/template/useSiderBar' import { setupI18n } from '@/locales' import { permissionRouter } from '@/router/utils/permission' @@ -120,7 +119,10 @@ it('starts with no active business page and never injects a missing cached dashb }) it('closes the final business tab, even when it is the legacy root preference', async () => { - useAppRoot().setRootRoute({ path: '/reports', name: 'Reports' }) + piniaSettingStore().updateSettingState('appRootRoute', { + path: '/reports', + name: 'Reports', + }) await router.push('/reports?sort=asc#summary') await flushPromises() expect(actions.getCurrentTagIndex()).toBe(0) @@ -177,7 +179,9 @@ it('closes left, right and other tabs without protecting any business path', asy it('closes all tags independently of missing or unauthorized root preferences', async () => { await router.push('/reports') await router.push('/orders') - useAppRoot().setRootRoute({ path: '/deleted-home' }) + piniaSettingStore().updateSettingState('appRootRoute', { + path: '/deleted-home', + }) await actions.closeAll() await flushPromises() expect(menu.menuTagOptions).toEqual([]) diff --git a/__test__/layout/preferences.spec.ts b/__test__/layout/preferences.spec.ts index a4e057de..d4a6dc79 100644 --- a/__test__/layout/preferences.spec.ts +++ b/__test__/layout/preferences.spec.ts @@ -62,8 +62,12 @@ describe('layout preferences', () => { const store = piniaSettingStore() expect(store.sideBarLogo?.url).toBe(migrated) - expect(store.appRootRoute.path).toBe('/old-unregistered-home') + expect(store.appRootRoute).toEqual({ + name: 'Old', + path: '/old-unregistered-home', + }) store.updateSettingState('sideBarLogo', { url: '/dashboard' }) + store.updateSettingState('appRootRoute', { path: '/saved-home' }) store.$persist() first.app.unmount() disposePinia(first.pinia) @@ -71,6 +75,10 @@ describe('layout preferences', () => { try { expect(piniaSettingStore().sideBarLogo?.url).toBe('/dashboard') + expect(piniaSettingStore().appRootRoute).toEqual({ + name: 'Old', + path: '/saved-home', + }) expect( piniaSettingStore().exportPreferences().preferences, ).not.toHaveProperty('workspaceLandingMigrated') diff --git a/__test__/router/initialization.spec.ts b/__test__/router/initialization.spec.ts index a14c85da..903ccf2b 100644 --- a/__test__/router/initialization.spec.ts +++ b/__test__/router/initialization.spec.ts @@ -6,9 +6,10 @@ import { setupRouter, } from '../../apps/admin/src/router' -const { load, register } = vi.hoisted(() => ({ +const { load, register, resetScroll } = vi.hoisted(() => ({ load: vi.fn(), register: vi.fn(), + resetScroll: vi.fn(), })) vi.mock('../../apps/admin/src/router/config/source', () => ({ @@ -31,7 +32,7 @@ vi.mock('../../apps/admin/src/router/app-route-modules', () => ({ getStaticAppRoutes: () => [], })) vi.mock('../../apps/admin/src/router/utils', () => ({ - scrollViewToTop: vi.fn(), + scrollViewToTop: resetScroll, vueRouterRegister: register, })) @@ -53,6 +54,39 @@ it('loads no business routes before login, then shares a retry and registers gua expect(register).toHaveBeenCalledOnce() } finally { clearAccountRoutes() - router.options.history.destroy() + } +}) + +afterAll(() => { + router.options.history.destroy() +}) + +it('resets workspace scroll after successful and repeated navigation', async () => { + const removeRoute = router.addRoute({ + path: '/scroll-target', + component: { render: () => null }, + }) + const removeBlockedRoute = router.addRoute({ + path: '/scroll-blocked', + component: { render: () => null }, + beforeEnter: () => false, + }) + + try { + resetScroll.mockClear() + await router.push('/scroll-target') + expect(resetScroll).toHaveBeenCalledExactlyOnceWith( + router.currentRoute.value, + ) + resetScroll.mockClear() + await router.push('/scroll-blocked') + expect(resetScroll).not.toHaveBeenCalled() + await router.push('/scroll-target') + expect(resetScroll).toHaveBeenCalledExactlyOnceWith( + router.currentRoute.value, + ) + } finally { + removeBlockedRoute() + removeRoute() } }) diff --git a/__test__/utils/canUseDom.ts b/__test__/utils/canUseDom.ts index aacc8c56..d4ff4587 100644 --- a/__test__/utils/canUseDom.ts +++ b/__test__/utils/canUseDom.ts @@ -5,12 +5,7 @@ * * 如果可以操作 DOM,则返回 true,否则返回 false。 */ -const canUseDom = () => { - return !!( - typeof window !== 'undefined' && - window.document && - window.document.createElement - ) -} +const canUseDom = () => + typeof window !== 'undefined' && !!window.document?.documentElement export default canUseDom diff --git a/__test__/utils/isBrowser.ts b/__test__/utils/isBrowser.ts index 33d59321..4c967a3a 100644 --- a/__test__/utils/isBrowser.ts +++ b/__test__/utils/isBrowser.ts @@ -6,10 +6,6 @@ * 如果是浏览器环境,则返回 true,否则返回 false。 */ const isBrowser = () => - !!( - typeof window !== 'undefined' && - window.document && - window.document.createElement - ) + typeof window !== 'undefined' && !!window.document?.documentElement export default isBrowser diff --git a/apps/admin/src/app-components/app/AppAvatar/AccountAvatar.tsx b/apps/admin/src/app-components/app/AppAvatar/AccountAvatar.tsx index 2ddbd06e..7b54482b 100644 --- a/apps/admin/src/app-components/app/AppAvatar/AccountAvatar.tsx +++ b/apps/admin/src/app-components/app/AppAvatar/AccountAvatar.tsx @@ -4,6 +4,11 @@ import { avatarProps, NAvatar } from 'naive-ui' const center = 20 const fontSize = 26 +export const accountAvatarProps = { + ...avatarProps, + color: String, +} + export const accountAvatarFont = { fontWeight: 600, fontFamily: 'system-ui, sans-serif', @@ -41,7 +46,7 @@ export function measureAccountText(text: string, size: number) { export default defineComponent({ name: 'AccountAvatar', props: { - ...avatarProps, + ...accountAvatarProps, name: { type: String, default: '' }, }, setup(props) { @@ -83,13 +88,13 @@ export default defineComponent({ } return () => { - const { name: _name, ...avatar } = props + const { name: _name, color, ...avatar } = props return (