From 1f1f29743ce617615cfa52a4b7af9ef8fda3cdbc Mon Sep 17 00:00:00 2001 From: kousaka <124561870@qq.com> Date: Tue, 3 Nov 2026 19:42:18 +0900 Subject: [PATCH] fix(native): single-page tab chips take their title from the tab registry (cherry picked from commit 5c65dd6bf49969983d83ddc612834c8dbe568b4c) --- src/client/native/tab-adapter.tsx | 7 +- tests/native-tab-title-registry.spec.ts | 86 +++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 tests/native-tab-title-registry.spec.ts diff --git a/src/client/native/tab-adapter.tsx b/src/client/native/tab-adapter.tsx index deac3440..b895b475 100644 --- a/src/client/native/tab-adapter.tsx +++ b/src/client/native/tab-adapter.tsx @@ -629,11 +629,16 @@ export function NativeTabTitle(props: NativeTitleInjected & NativeBodyFrameworkP () => records.versionOf(sessionId, nativeTab.id), ) const record = records.get(sessionId, nativeTab.id) - const title = record?.tab.title ?? nativeTab.title // `version` is read so a title/path/meta mutation re-renders the chip; the // icon itself is derived from the record, never stored. void version const descriptor = service.getTab(descriptorId) ?? service.getTab(record?.tab.type ?? nativeTab.kind) + // A single page's title is its type's, read from the registry at render + // time: an open tab follows a renamed panel instead of keeping the title + // captured when it opened (the record and native tab both store that one). + const title = descriptor?.single === true + ? (typeof descriptor.title === 'function' ? descriptor.title() : descriptor.title) + : record?.tab.title ?? nativeTab.title // The record's own path wins (an in-place switch stores it there, and the // native address of a page kind cannot carry it); the address-derived path // is what keeps the glyph correct when the record is not there YET (the tab diff --git a/tests/native-tab-title-registry.spec.ts b/tests/native-tab-title-registry.spec.ts new file mode 100644 index 00000000..54d8dea9 --- /dev/null +++ b/tests/native-tab-title-registry.spec.ts @@ -0,0 +1,86 @@ +// @vitest-environment jsdom +/** + * The native tab CHIP's title for a single-instance page: it is the type's + * registered title, read at render time. The record and the native tab both + * keep the title captured when the tab opened, so a page renamed later (a + * plugin update or a locale change re-registering the descriptor) used to + * keep its stale title on every tab that was already open. + */ +import { describe, expect, it } from 'vitest' +import { createElement } from 'react' +import { createRoot, type Root } from 'react-dom/client' +import { act } from 'react-dom/test-utils' +import { + NativeTabTitle, + createNativeTabRecords, + type NativeTabInfo, +} from '../src/client/native/tab-adapter.tsx' +import { createBetterSidebarService } from '../src/client/service.ts' +import { createSidebarStore } from '../src/client/state.ts' + +const scope = { sessionId: 's1', cwd: '/work' } + +function nativeInfo(id: string, kind: string, title: string): NativeTabInfo { + return { + tab: { + id, + kind, + title, + contentId: `sidebar://${kind}`, + visible: false, + navigation: { address: `sidebar://${kind}`, params: undefined, revision: 0 }, + signal: new AbortController().signal, + }, + } +} + +function renderTitle( + records: ReturnType, + service: ReturnType, + info: NativeTabInfo, + descriptorId: string, +): { text: () => string; unmount: () => void } { + const host = document.createElement('div') + document.body.appendChild(host) + let root: Root | undefined + act(() => { + root = createRoot(host) + root.render(createElement(NativeTabTitle, { + records, + service, + descriptorId, + useTabInfo: () => info, + } as never)) + }) + return { + text: () => host.textContent ?? '', + unmount: () => { + act(() => { root?.unmount() }) + host.remove() + }, + } +} + +describe('NativeTabTitle registry title', () => { + it('shows a renamed single page by its registered title, not the one stored when it opened', () => { + const records = createNativeTabRecords() + const service = createBetterSidebarService(createSidebarStore()) + service.registerTab({ id: 'library', single: true, title: () => 'Library', component: () => createElement('div') }) + records.ensure({ id: 'tab1', kind: 'library', title: 'Old library name', scope } as never) + + const { text, unmount } = renderTitle(records, service, nativeInfo('tab1', 'library', 'Old library name'), 'library') + expect(text()).toBe('Library') + unmount() + }) + + it('keeps the record title for a multi-instance tab (an editor file, a side chat)', () => { + const records = createNativeTabRecords() + const service = createBetterSidebarService(createSidebarStore()) + service.registerTab({ id: 'chat', title: () => 'Chat', component: () => createElement('div') }) + records.ensure({ id: 'tab2', kind: 'chat', title: 'Why is the sky blue?', scope } as never) + + const { text, unmount } = renderTitle(records, service, nativeInfo('tab2', 'chat', 'Chat'), 'chat') + expect(text()).toBe('Why is the sky blue?') + unmount() + }) +})