fix(desktop): late-contributed keybind actions reach the Keybinds settings map

Same class as the plugin-route bug: KeybindSettings subscribed to
useContributions(KEYBINDS_AREA) but discarded the snapshot and called the
impure allKeybindActions() in render, so the React Compiler memoized the
action list without the subscription as a visible input. A plugin keybind
registered after the tab mounted never appeared, despite the comment
promising "appear/disappear live".

allKeybindActions()/contributedKeybinds() take an optional contribution
snapshot (default: registry read, so the store/imperative callers are
unchanged) and the settings tab passes its subscription through. One test,
red on origin/main.
This commit is contained in:
teknium1
2026-09-15 11:31:42 -07:00
committed by Teknium
parent 51c9e8932a
commit eaef52371f
3 changed files with 55 additions and 8 deletions
@@ -0,0 +1,42 @@
/**
* A keybind action contributed AFTER the settings tab mounts must appear in
* the map. Same class as the late plugin-route bug (#109063): the component
* subscribed to `useContributions(KEYBINDS_AREA)` but discarded the snapshot
* and called the impure `allKeybindActions()` in render, so the React
* Compiler memoized the list without the subscription as an input. Uses the
* real registry + hook under the compiler-enabled vitest ui project.
*/
import { act, cleanup, render, screen } from '@testing-library/react'
import { afterEach, describe, expect, it } from 'vitest'
import { registry } from '@/contrib/registry'
import { I18nProvider } from '@/i18n'
import { KEYBINDS_AREA } from '@/lib/keybinds/actions'
import { KeybindSettings } from './keybind-settings'
afterEach(cleanup)
describe('KeybindSettings late-contributed actions', () => {
it('lists an action whose keybind registers after mount', () => {
render(
<I18nProvider configClient={null} initialLocale="en">
<KeybindSettings />
</I18nProvider>
)
expect(screen.queryByText('Late Plugin Action')).toBeNull()
let dispose = () => {}
act(() => {
dispose = registry.register({
area: KEYBINDS_AREA,
id: 'late-plugin:action',
data: { id: 'late-plugin:action', label: 'Late Plugin Action', category: 'view', run: () => {} }
})
})
expect(screen.getByText('Late Plugin Action')).toBeTruthy()
act(() => dispose())
})
})
@@ -37,9 +37,11 @@ export function KeybindSettings() {
const bindings = useStore($bindings)
const k = t.keybinds
const [collapsed, setCollapsed] = useState<ReadonlySet<string>>(new Set())
// Subscribe so contributed actions appear/disappear live in the map.
useContributions(KEYBINDS_AREA)
const actionList = allKeybindActions()
// Subscribe so contributed actions appear/disappear live in the map. The
// snapshot feeds the list: under React Compiler an independently called
// allKeybindActions() stays memoized across that registration.
const contributions = useContributions(KEYBINDS_AREA)
const actionList = allKeybindActions(contributions)
const [query, setQuery] = useState('')
const openCombo = bindings[KEYBIND_PANEL_ACTION]?.[0]
+8 -5
View File
@@ -6,6 +6,7 @@
// add a hotkey, add a row here and a handler there — nothing else.
import { registry } from '@/contrib/registry'
import type { Contribution } from '@/contrib/types'
import { IS_MAC } from './combo'
@@ -202,18 +203,20 @@ export interface KeybindContribution {
run: () => void
}
export function contributedKeybinds(): KeybindContribution[] {
return registry
.getArea(KEYBINDS_AREA)
// React consumers pass their `useContributions(KEYBINDS_AREA)` snapshot in:
// with React Compiler enabled, an independently-called `contributedKeybinds()`
// can stay memoized across a late registration the subscription DID deliver.
export function contributedKeybinds(contributions: readonly Contribution[] = registry.getArea(KEYBINDS_AREA)): KeybindContribution[] {
return contributions
.map(c => c.data as KeybindContribution)
.filter(k => Boolean(k?.id && k.label) && typeof k?.run === 'function' && !ACTION_BY_ID.has(k.id))
}
/** Built-ins + contributed, one metadata list (panel, bindings, conflicts). */
export function allKeybindActions(): KeybindActionMeta[] {
export function allKeybindActions(contributions?: readonly Contribution[]): KeybindActionMeta[] {
return [
...KEYBIND_ACTIONS,
...contributedKeybinds().map(k => ({
...contributedKeybinds(contributions).map(k => ({
id: k.id,
category: k.category ?? ('view' as const),
defaults: k.defaults ?? [],