fix(native): single-page tab chips take their title from the tab registry
(cherry picked from commit 5c65dd6bf49969983d83ddc612834c8dbe568b4c)
This commit is contained in:
1 parent
f65ac689fc
commit
1f1f29743c
2 files changed
+92
-1
No files matched your search
@@ -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
|
||||
|
||||
@@ -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<typeof createNativeTabRecords>,
|
||||
service: ReturnType<typeof createBetterSidebarService>,
|
||||
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()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user