diff --git a/application/state/sidePanelLiveTabIsolation.test.ts b/application/state/sidePanelLiveTabIsolation.test.ts index 665a50d8f..10f220359 100644 --- a/application/state/sidePanelLiveTabIsolation.test.ts +++ b/application/state/sidePanelLiveTabIsolation.test.ts @@ -2,7 +2,7 @@ import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import test from 'node:test'; import vm from 'node:vm'; -import React, { useCallback, useRef, useSyncExternalStore } from 'react'; +import React, { memo, useCallback, useRef, useSyncExternalStore } from 'react'; import { act, create } from 'react-test-renderer'; import ts from 'typescript'; import { resolveSystemSidebarSession } from '../../domain/systemManager/resolveSystemSession'; @@ -25,10 +25,10 @@ const env = globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boole test('workspace browser follows focus without replacing the earlier terminal context', async () => { const previous = env.IS_REACT_ACT_ENVIRONMENT; env.IS_REACT_ACT_ENVIRONMENT = true; - const browserSlot = ast.statements.find((node) => ts.isFunctionDeclaration(node) && node.name?.text === 'SidePanelBrowserSlot'); + const browserSlot = ast.statements.find((node) => ts.isFunctionDeclaration(node) && node.name?.text === 'SidePanelBrowserSlotInner'); assert.ok(browserSlot); const slotCode = ts.transpile(browserSlot.getText(ast), { target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.React }); - const Slot = vm.runInNewContext(`${slotCode}; SidePanelBrowserSlot`, { + const Slot = vm.runInNewContext(`${slotCode}; SidePanelBrowserSlotInner`, { React, useRef, useSidePanelLiveSnapshotForTab: useSnapshot, resolveSystemSidebarSession, collectSessionIds, sidePanelHiddenPanelClassName: (hidden: boolean) => hidden ? 'hidden' : 'visible', HostBrowserSidePanel: (props: object) => React.createElement('div', { ...props, 'data-browser-context': true }), @@ -133,3 +133,74 @@ test('new panels wait for their own snapshot and workspace panes accept focus ch env.IS_REACT_ACT_ENVIRONMENT = previous; } }); + +test('SidePanelBrowserSlot is memoized and skips re-renders when props are unchanged', async () => { + const previous = env.IS_REACT_ACT_ENVIRONMENT; + env.IS_REACT_ACT_ENVIRONMENT = true; + assert.match( + source, + /export const SidePanelBrowserSlot = memo\(SidePanelBrowserSlotInner\);/, + 'SidePanelBrowserSlot must be wrapped in React.memo like sibling side-panel slots', + ); + const browserSlot = ast.statements.find( + (node) => ts.isFunctionDeclaration(node) && node.name?.text === 'SidePanelBrowserSlotInner', + ); + assert.ok(browserSlot); + const slotCode = ts.transpile(browserSlot.getText(ast), { + target: ts.ScriptTarget.ES2022, + jsx: ts.JsxEmit.React, + }); + let renderCount = 0; + const MemoSlot = vm.runInNewContext( + `${slotCode}; memo(function TrackedBrowserSlot(props) { onRender(); return SidePanelBrowserSlotInner(props); })`, + { + React, + memo, + onRender: () => { renderCount += 1; }, + useRef, + useSidePanelLiveSnapshotForTab: useSnapshot, + resolveSystemSidebarSession, + collectSessionIds, + sidePanelHiddenPanelClassName: (hidden: boolean) => (hidden ? 'hidden' : 'visible'), + HostBrowserSidePanel: (props: object) => React.createElement('div', { ...props, 'data-browser-context': true }), + }, + ) as React.ComponentType<{ tabId: string; ctx: object; isVisible: boolean }>; + + const sessionA = { id: 'session-a', hostId: 'host-a', protocol: 'ssh' }; + const ctx = { + sessions: [sessionA], + workspaceById: new Map(), + sessionHostsMap: new Map([['session-a', { id: 'host-a', hostname: 'a', protocol: 'ssh' }]]), + sftpAuthHostBySessionId: new Map(), + effectiveHosts: [], + keys: [], + identities: [], + knownHosts: [], + terminalSettings: {}, + t: (key: string) => key, + }; + + let renderer: ReturnType | undefined; + try { + await act(async () => { + renderer = create(React.createElement(MemoSlot, { tabId: 'session-a', ctx, isVisible: false })); + }); + assert.equal(renderCount, 1); + + // Re-rendering the parent with identical tabId/ctx/isVisible must be skipped by memo. + await act(async () => { + renderer!.update(React.createElement(MemoSlot, { tabId: 'session-a', ctx, isVisible: false })); + }); + assert.equal(renderCount, 1); + + // Visibility changes must still re-render the slot. + await act(async () => { + renderer!.update(React.createElement(MemoSlot, { tabId: 'session-a', ctx, isVisible: true })); + }); + assert.equal(renderCount, 2); + } finally { + await act(async () => renderer?.unmount()); + sidePanelLiveStore.update(SIDE_PANEL_INACTIVE_LIVE_SNAPSHOT); + env.IS_REACT_ACT_ENVIRONMENT = previous; + } +}); diff --git a/components/terminalLayer/terminalLayerSidePanelSlots.tsx b/components/terminalLayer/terminalLayerSidePanelSlots.tsx index acc9ece93..544df18e1 100644 --- a/components/terminalLayer/terminalLayerSidePanelSlots.tsx +++ b/components/terminalLayer/terminalLayerSidePanelSlots.tsx @@ -243,7 +243,7 @@ function SidePanelSftpSlotInner({ export const SidePanelSftpSlot = memo(SidePanelSftpSlotInner); SidePanelSftpSlot.displayName = 'SidePanelSftpSlot'; -function SidePanelBrowserSlot({ tabId, ctx, isVisible }: { +function SidePanelBrowserSlotInner({ tabId, ctx, isVisible }: { tabId: string; ctx: SidePanelStableContext; isVisible: boolean; @@ -294,6 +294,9 @@ function SidePanelBrowserSlot({ tabId, ctx, isVisible }: { ); } +export const SidePanelBrowserSlot = memo(SidePanelBrowserSlotInner); +SidePanelBrowserSlot.displayName = 'SidePanelBrowserSlot'; + function SidePanelSystemSlotInner({ tabId, ctx, diff --git a/components/terminalLayer/useTerminalThemePanelState.test.ts b/components/terminalLayer/useTerminalThemePanelState.test.ts new file mode 100644 index 000000000..658f4b425 --- /dev/null +++ b/components/terminalLayer/useTerminalThemePanelState.test.ts @@ -0,0 +1,544 @@ +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import test from 'node:test'; +import vm from 'node:vm'; +import React, { useCallback, useEffect, useMemo, useRef } from 'react'; +import { act, create, type ReactTestRenderer } from 'react-test-renderer'; +import ts from 'typescript'; + +import { useAppearanceChromeStore } from '../../application/state/appearanceChromeStore'; +import { isSavedVaultHost } from '../../domain/ephemeralHosts'; +import { + clearHostFontFamilyOverride, + clearHostFontSizeOverride, + clearHostFontWeightOverride, + clearHostThemeOverride, + hasHostFontFamilyOverride, + hasHostFontSizeOverride, + hasHostFontWeightOverride, + hasHostThemeOverride, + resolveHostTerminalFontFamilyId, + resolveHostTerminalFontSize, + resolveHostTerminalFontWeight, + resolveHostTerminalThemeId, +} from '../../domain/terminalAppearance'; +import type { ResolvedAppearance, TerminalAppearanceHostScope } from '../../domain/terminalAppearanceRuntime'; +import { isSameResolvedTerminalFont } from '../../infrastructure/config/fonts'; +import type { Host, TerminalSession, TerminalTheme, Workspace } from '../../types'; +import { getScopedTopTabsThemeId } from '../terminalTopTabsTheme'; +import { + terminalLayerSidePanelStableCtxEqual, + terminalLayerViewCtxEqual, +} from './terminalLayerViewMemo'; +import { useTerminalThemePanelState } from './useTerminalThemePanelState'; + +const env = globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }; + +const CALLBACK_KEYS = [ + 'handleThemeChangeForFocusedSession', + 'handleThemeResetForFocusedSession', + 'handleFontFamilyChangeForFocusedSession', + 'handleFontFamilyResetForFocusedSession', + 'handleFontSizeChangeForFocusedSession', + 'handleFontSizeResetForFocusedSession', + 'handleFontWeightChangeForFocusedSession', + 'handleFontWeightResetForFocusedSession', +] as const; + +const makeTheme = (id: string): TerminalTheme => ({ + id, + name: id, + type: 'dark', + colors: { + background: '#111111', + foreground: '#eeeeee', + cursor: '#ffffff', + selection: '#333333', + black: '#000000', + red: '#ff0000', + green: '#00ff00', + yellow: '#ffff00', + blue: '#0000ff', + magenta: '#ff00ff', + cyan: '#00ffff', + white: '#ffffff', + brightBlack: '#666666', + brightRed: '#ff6666', + brightGreen: '#66ff66', + brightYellow: '#ffff66', + brightBlue: '#6666ff', + brightMagenta: '#ff66ff', + brightCyan: '#66ffff', + brightWhite: '#ffffff', + }, +}); + +const makeHost = (id: string, overrides: Partial = {}): Host => ({ + id, + label: `Host ${id}`, + hostname: `${id}.example.com`, + username: 'root', + port: 22, + protocol: 'ssh', + tags: [], + os: 'linux', + createdAt: 1, + ...overrides, +}); + +const makeSession = (id: string, hostId: string, overrides: Partial = {}): TerminalSession => ({ + id, + hostId, + hostLabel: `Host ${hostId}`, + username: 'root', + hostname: `${hostId}.example.com`, + status: 'connected', + ...overrides, +}); + +const makeWorkspace = (focusedSessionId: string): Workspace => ({ + id: 'workspace-1', + title: 'Workspace', + viewMode: 'split', + focusedSessionId, + root: { + id: 'split-1', + type: 'split', + direction: 'vertical', + sizes: [0.5, 0.5], + children: [ + { id: 'pane-a', type: 'pane', sessionId: 'session-a' }, + { id: 'pane-b', type: 'pane', sessionId: 'session-b' }, + ], + }, +}); + +type HookOptions = Parameters[0]; +type HookResult = ReturnType; + +function createBaseOptions(overrides: Partial = {}): { + options: HookOptions; + calls: { + clearIntent: number; + pickTheme: Array; + updatedHosts: Host[]; + updatedTerminalThemeIds: string[]; + updatedTerminalFontFamilyIds: string[]; + updatedTerminalFontSizes: number[]; + updatedTerminalFontWeights: number[]; + updatedSessionFontSizes: Array<{ sessionId: string; fontSize: number }>; + clearedSessionFontSizes: string[]; + }; +} { + const hostA = makeHost('host-a'); + const hostB = makeHost('host-b'); + const sessionA = makeSession('session-a', 'host-a'); + const sessionB = makeSession('session-b', 'host-b'); + const defaultTheme = makeTheme('default-dark'); + + const calls = { + clearIntent: 0, + pickTheme: [] as Array, + updatedHosts: [] as Host[], + updatedTerminalThemeIds: [] as string[], + updatedTerminalFontFamilyIds: [] as string[], + updatedTerminalFontSizes: [] as number[], + updatedTerminalFontWeights: [] as number[], + updatedSessionFontSizes: [] as Array<{ sessionId: string; fontSize: number }>, + clearedSessionFontSizes: [] as string[], + }; + + const options: HookOptions = { + activeSession: sessionA, + activeSidePanelTab: 'theme', + activeWorkspace: undefined, + clearIntent: () => { calls.clearIntent += 1; }, + followAppTerminalTheme: false, + focusedSessionId: 'session-a', + fontSize: 14, + hostMap: new Map([ + [hostA.id, hostA], + [hostB.id, hostB], + ]), + isSidePanelOpenForCurrentTab: true, + isVisible: true, + onUpdateHost: (host) => { calls.updatedHosts.push(host); }, + onUpdateTerminalFontFamilyId: (id) => { calls.updatedTerminalFontFamilyIds.push(id); }, + onUpdateTerminalFontSize: (size) => { calls.updatedTerminalFontSizes.push(size); }, + onUpdateTerminalFontWeight: (weight) => { calls.updatedTerminalFontWeights.push(weight); }, + onUpdateTerminalThemeId: (themeId) => { calls.updatedTerminalThemeIds.push(themeId); }, + onUpdateSessionFontSize: (sessionId, size) => { + calls.updatedSessionFontSizes.push({ sessionId, fontSize: size }); + }, + onClearSessionFontSizeOverride: (sessionId) => { + calls.clearedSessionFontSizes.push(sessionId); + }, + pickTheme: ((...args: unknown[]) => { calls.pickTheme.push(args); }) as HookOptions['pickTheme'], + resolveFocusedAppearance: (hostScope: TerminalAppearanceHostScope): ResolvedAppearance => { + const themeId = hostScope.host?.themeOverride && hostScope.host.theme + ? hostScope.host.theme + : defaultTheme.id; + return { + themeId, + theme: makeTheme(themeId), + themeSource: hostScope.host?.themeOverride ? 'host' : 'global', + } as ResolvedAppearance; + }, + sessionHostsMap: new Map([ + [sessionA.id, hostA], + [sessionB.id, hostB], + ]), + terminalFontFamilyId: 'jetbrains-mono', + terminalSettings: { fontWeight: 400 }, + terminalTheme: defaultTheme, + ...overrides, + }; + + return { options, calls }; +} + +test('all 8 theme panel callbacks keep identical references and preserve terminalLayerSidePanelStableCtxEqual across focus/host changes', async () => { + const previous = env.IS_REACT_ACT_ENVIRONMENT; + env.IS_REACT_ACT_ENVIRONMENT = true; + const { options } = createBaseOptions(); + let latestResult!: HookResult; + + function Harness({ hookOptions }: { hookOptions: HookOptions }) { + latestResult = useTerminalThemePanelState(hookOptions); + return null; + } + + let renderer: ReactTestRenderer | undefined; + try { + await act(async () => { + renderer = create(React.createElement(Harness, { hookOptions: options })); + }); + + const initialCallbacks = Object.fromEntries( + CALLBACK_KEYS.map((key) => [key, latestResult[key]]), + ); + const prevCtx = { + mountedSftpTabIds: ['session-a', 'session-b'], + sidePanelOpenTabs: new Map([['session-a', 'theme']]), + ...initialCallbacks, + }; + + // Switch standalone terminal tab to a different host + different workspace focus. + const hostB = options.hostMap.get('host-b')!; + const sessionB = makeSession('session-b', hostB.id); + const nextOptions: HookOptions = { + ...options, + activeSession: sessionB, + activeWorkspace: makeWorkspace('session-b'), + focusedSessionId: 'session-b', + fontSize: 16, + terminalSettings: { fontWeight: 500 }, + }; + + await act(async () => { + renderer!.update(React.createElement(Harness, { hookOptions: nextOptions })); + }); + + const nextCallbacks = Object.fromEntries( + CALLBACK_KEYS.map((key) => [key, latestResult[key]]), + ); + + for (const key of CALLBACK_KEYS) { + assert.equal( + nextCallbacks[key], + initialCallbacks[key], + `${key} must keep a stable reference across focus/host changes`, + ); + } + + const nextCtx = { + ...prevCtx, + ...nextCallbacks, + }; + assert.equal(terminalLayerSidePanelStableCtxEqual(prevCtx, nextCtx), true); + assert.equal(terminalLayerViewCtxEqual(prevCtx, nextCtx), true); + } finally { + await act(async () => { + renderer?.unmount(); + }); + env.IS_REACT_ACT_ENVIRONMENT = previous; + } +}); + +test('stable callbacks read latest focused host/session after focus changes', async () => { + const previous = env.IS_REACT_ACT_ENVIRONMENT; + env.IS_REACT_ACT_ENVIRONMENT = true; + const hostA = makeHost('host-a', { + theme: 'nord', + themeOverride: true, + fontFamily: 'fira-code', + fontFamilyOverride: true, + fontSize: 15, + fontSizeOverride: true, + fontWeight: 500, + fontWeightOverride: true, + }); + const hostB = makeHost('host-b', { + theme: 'solarized', + themeOverride: true, + fontFamily: 'menlo', + fontFamilyOverride: true, + fontSize: 13, + fontSizeOverride: true, + fontWeight: 400, + fontWeightOverride: true, + }); + const sessionA = makeSession('session-a', 'host-a'); + const sessionB = makeSession('session-b', 'host-b'); + + const { options, calls } = createBaseOptions({ + activeSession: sessionA, + focusedSessionId: 'session-a', + hostMap: new Map([ + [hostA.id, hostA], + [hostB.id, hostB], + ]), + sessionHostsMap: new Map([ + [sessionA.id, hostA], + [sessionB.id, hostB], + ]), + }); + + let latestResult!: HookResult; + function Harness({ hookOptions }: { hookOptions: HookOptions }) { + latestResult = useTerminalThemePanelState(hookOptions); + return null; + } + + let renderer: ReactTestRenderer | undefined; + try { + await act(async () => { + renderer = create(React.createElement(Harness, { hookOptions: options })); + }); + + // Capture callbacks from the first render (while focused on host-a). + const firstRender = latestResult; + + // Switch focus to host-b (standalone session). + await act(async () => { + renderer!.update(React.createElement(Harness, { + hookOptions: { + ...options, + activeSession: sessionB, + focusedSessionId: 'session-b', + }, + })); + }); + + await act(async () => { + firstRender.handleThemeChangeForFocusedSession('dracula'); + firstRender.handleThemeResetForFocusedSession(); + firstRender.handleFontFamilyChangeForFocusedSession('jetbrains-mono'); + firstRender.handleFontFamilyResetForFocusedSession(); + firstRender.handleFontSizeChangeForFocusedSession(18); + firstRender.handleFontSizeResetForFocusedSession(); + firstRender.handleFontWeightChangeForFocusedSession(600); + firstRender.handleFontWeightResetForFocusedSession(); + }); + + assert.deepEqual(calls.pickTheme, [ + ['dracula', { followApp: false, scopeHostId: 'host-b' }], + ]); + assert.equal(calls.clearIntent, 1); + assert.equal(calls.updatedHosts.length, 8); + assert.ok( + calls.updatedHosts.every((host) => host.id === 'host-b'), + 'all host mutations must target the latest focused host (host-b)', + ); + assert.equal(calls.updatedHosts[0].theme, 'dracula'); + assert.equal(calls.updatedHosts[0].themeOverride, true); + assert.equal(calls.updatedHosts[1].theme, undefined); + assert.equal(calls.updatedHosts[1].themeOverride, false); + assert.equal(calls.updatedHosts[2].fontFamily, 'jetbrains-mono'); + assert.equal(calls.updatedHosts[2].fontFamilyOverride, true); + assert.equal(calls.updatedHosts[3].fontFamily, undefined); + assert.equal(calls.updatedHosts[3].fontFamilyOverride, false); + assert.equal(calls.updatedHosts[4].fontSize, 18); + assert.equal(calls.updatedHosts[4].fontSizeOverride, true); + assert.equal(calls.updatedHosts[5].fontSize, undefined); + assert.equal(calls.updatedHosts[5].fontSizeOverride, false); + assert.equal(calls.updatedHosts[6].fontWeight, 600); + assert.equal(calls.updatedHosts[6].fontWeightOverride, true); + assert.equal(calls.updatedHosts[7].fontWeight, undefined); + assert.equal(calls.updatedHosts[7].fontWeightOverride, false); + + // Switch into a split workspace focused on session-b and verify session-scoped font size calls. + await act(async () => { + renderer!.update(React.createElement(Harness, { + hookOptions: { + ...options, + activeSession: undefined, + activeWorkspace: makeWorkspace('session-b'), + focusedSessionId: 'session-b', + }, + })); + }); + + await act(async () => { + firstRender.handleFontSizeChangeForFocusedSession(20); + firstRender.handleFontSizeResetForFocusedSession(); + }); + + assert.deepEqual(calls.updatedSessionFontSizes, [{ sessionId: 'session-b', fontSize: 20 }]); + assert.deepEqual(calls.clearedSessionFontSizes, ['session-b']); + } finally { + await act(async () => { + renderer?.unmount(); + }); + env.IS_REACT_ACT_ENVIRONMENT = previous; + } +}); + +test('startTransition callbacks snapshot target host/session before a focus change commits', async () => { + const previous = env.IS_REACT_ACT_ENVIRONMENT; + env.IS_REACT_ACT_ENVIRONMENT = true; + + // Run the real hook body with a controllable startTransition so we can shift + // focus between callback invocation and transition execution. + const source = readFileSync(new URL('./useTerminalThemePanelState.ts', import.meta.url), 'utf8'); + const transpiled = ts.transpileModule(source, { + compilerOptions: { + module: ts.ModuleKind.CommonJS, + target: ts.ScriptTarget.ES2022, + }, + }).outputText; + + const pendingTransitions: Array<() => void> = []; + const deferredStartTransition = (callback: () => void) => { + pendingTransitions.push(callback); + }; + + const exportsObj: Record = {}; + const moduleObj = { exports: exportsObj }; + const deps: Record = { + react: { + startTransition: deferredStartTransition, + useCallback, + useEffect, + useMemo, + useRef, + }, + '../../application/state/appearanceChromeStore': { useAppearanceChromeStore }, + '../../domain/terminalAppearance': { + clearHostFontFamilyOverride, + clearHostFontSizeOverride, + clearHostFontWeightOverride, + clearHostThemeOverride, + hasHostFontFamilyOverride, + hasHostFontSizeOverride, + hasHostFontWeightOverride, + hasHostThemeOverride, + resolveHostTerminalFontFamilyId, + resolveHostTerminalFontSize, + resolveHostTerminalFontWeight, + resolveHostTerminalThemeId, + }, + '../../domain/ephemeralHosts': { isSavedVaultHost }, + '../../infrastructure/config/fonts': { isSameResolvedTerminalFont }, + '../terminalTopTabsTheme': { getScopedTopTabsThemeId }, + }; + + vm.runInNewContext(transpiled, { + exports: exportsObj, + module: moduleObj, + require: (specifier: string) => { + if (!(specifier in deps)) { + throw new Error(`Unexpected require in test harness: ${specifier}`); + } + return deps[specifier]; + }, + navigator: globalThis.navigator, + }); + + const useHookWithDeferredTransition = moduleObj.exports.useTerminalThemePanelState as typeof useTerminalThemePanelState; + + const { options, calls } = createBaseOptions(); + const sessionB = makeSession('session-b', 'host-b'); + + let latestResult!: HookResult; + function Harness({ hookOptions }: { hookOptions: HookOptions }) { + latestResult = useHookWithDeferredTransition(hookOptions); + return null; + } + + let renderer: ReactTestRenderer | undefined; + try { + // 1. Standalone saved host: invoke all 3 startTransition handlers while focused on host-a. + await act(async () => { + renderer = create(React.createElement(Harness, { hookOptions: options })); + }); + + latestResult.handleFontFamilyChangeForFocusedSession('fira-code'); + latestResult.handleFontSizeChangeForFocusedSession(17); + latestResult.handleFontWeightChangeForFocusedSession(700); + assert.equal(pendingTransitions.length, 3); + + // Move focus to host-b BEFORE any of the 3 transitions execute. + await act(async () => { + renderer!.update(React.createElement(Harness, { + hookOptions: { + ...options, + activeSession: sessionB, + focusedSessionId: 'session-b', + }, + })); + }); + + // Now flush the queued transitions and verify all 3 still mutate host-a. + while (pendingTransitions.length > 0) { + pendingTransitions.shift()!(); + } + + assert.equal(calls.updatedHosts.length, 3); + assert.deepEqual( + calls.updatedHosts.map((host) => host.id), + ['host-a', 'host-a', 'host-a'], + 'deferred startTransition updates must target the snapshotted host-a, not newly focused host-b', + ); + assert.equal(calls.updatedHosts[0].fontFamily, 'fira-code'); + assert.equal(calls.updatedHosts[1].fontSize, 17); + assert.equal(calls.updatedHosts[2].fontWeight, 700); + + // 2. Workspace split-pane focus change before transition commits. + await act(async () => { + renderer!.update(React.createElement(Harness, { + hookOptions: { + ...options, + activeSession: undefined, + activeWorkspace: makeWorkspace('session-a'), + focusedSessionId: 'session-a', + }, + })); + }); + + latestResult.handleFontSizeChangeForFocusedSession(19); + assert.equal(pendingTransitions.length, 1); + + // Focus shifts to pane B before the transition callback runs. + await act(async () => { + renderer!.update(React.createElement(Harness, { + hookOptions: { + ...options, + activeSession: undefined, + activeWorkspace: makeWorkspace('session-b'), + focusedSessionId: 'session-b', + }, + })); + }); + + pendingTransitions.shift()!(); + assert.deepEqual(calls.updatedSessionFontSizes, [ + { sessionId: 'session-a', fontSize: 19 }, + ]); + } finally { + await act(async () => { + renderer?.unmount(); + }); + env.IS_REACT_ACT_ENVIRONMENT = previous; + } +}); diff --git a/components/terminalLayer/useTerminalThemePanelState.ts b/components/terminalLayer/useTerminalThemePanelState.ts index e4b19aeda..00b4ee31b 100644 --- a/components/terminalLayer/useTerminalThemePanelState.ts +++ b/components/terminalLayer/useTerminalThemePanelState.ts @@ -1,4 +1,4 @@ -import { startTransition, useCallback, useEffect, useMemo } from 'react'; +import { startTransition, useCallback, useEffect, useMemo, useRef } from 'react'; import { useAppearanceChromeStore } from '../../application/state/appearanceChromeStore'; import type { ResolvedAppearance, TerminalAppearanceHostScope } from '../../domain/terminalAppearanceRuntime'; @@ -178,7 +178,68 @@ export function useTerminalThemePanelState({ ], ); + // Keep latest focus/host/font/theme inputs in a ref updated synchronously on + // render so all 8 theme/font handlers preserve stable references across tab + // switches and split-pane focus changes (these handlers live in + // SIDE_PANEL_STABLE_CTX_KEYS). + const latestRef = useRef({ + activeSession, + activeWorkspace, + clearIntent, + focusedFontFamilyId, + focusedFontSize, + focusedFontWeight, + focusedHost, + focusedSessionId, + followAppTerminalTheme, + hostMap, + isFocusedHostEphemeral, + listSelectedThemeId, + onClearSessionFontSizeOverride, + onUpdateHost, + onUpdateSessionFontSize, + onUpdateTerminalFontFamilyId, + onUpdateTerminalFontSize, + onUpdateTerminalFontWeight, + onUpdateTerminalThemeId, + pickTheme, + rawFocusedHost, + }); + latestRef.current = { + activeSession, + activeWorkspace, + clearIntent, + focusedFontFamilyId, + focusedFontSize, + focusedFontWeight, + focusedHost, + focusedSessionId, + followAppTerminalTheme, + hostMap, + isFocusedHostEphemeral, + listSelectedThemeId, + onClearSessionFontSizeOverride, + onUpdateHost, + onUpdateSessionFontSize, + onUpdateTerminalFontFamilyId, + onUpdateTerminalFontSize, + onUpdateTerminalFontWeight, + onUpdateTerminalThemeId, + pickTheme, + rawFocusedHost, + }; + const handleThemeChangeForFocusedSession = useCallback((themeId: string) => { + const { + focusedHost, + followAppTerminalTheme, + isFocusedHostEphemeral, + listSelectedThemeId, + onUpdateHost, + onUpdateTerminalThemeId, + pickTheme, + rawFocusedHost, + } = latestRef.current; if (themeId === listSelectedThemeId) return; if (!focusedHost && !followAppTerminalTheme) return; @@ -196,24 +257,32 @@ export function useTerminalThemePanelState({ } else if (rawFocusedHost) { onUpdateHost({ ...rawFocusedHost, theme: themeId, themeOverride: true }); } - }, [ - focusedHost, - followAppTerminalTheme, - isFocusedHostEphemeral, - listSelectedThemeId, - onUpdateHost, - onUpdateTerminalThemeId, - pickTheme, - rawFocusedHost, - ]); + }, []); const handleThemeResetForFocusedSession = useCallback(() => { + const { + clearIntent, + focusedHost, + isFocusedHostEphemeral, + onUpdateHost, + rawFocusedHost, + } = latestRef.current; clearIntent(); if (!focusedHost || isFocusedHostEphemeral || !rawFocusedHost) return; onUpdateHost(clearHostThemeOverride(rawFocusedHost)); - }, [clearIntent, focusedHost, isFocusedHostEphemeral, onUpdateHost, rawFocusedHost]); + }, []); const handleFontFamilyChangeForFocusedSession = useCallback((fontFamilyId: string) => { + // Snapshot target host/session state synchronously before startTransition + // so a focus change prior to transition commit cannot mutate another host. + const { + focusedHost, + focusedFontFamilyId, + isFocusedHostEphemeral, + onUpdateTerminalFontFamilyId, + onUpdateHost, + rawFocusedHost, + } = latestRef.current; if (!focusedHost || isSameResolvedTerminalFont(fontFamilyId, focusedFontFamilyId, navigatorPlatform)) return; startTransition(() => { if (isFocusedHostEphemeral) { @@ -224,14 +293,34 @@ export function useTerminalThemePanelState({ onUpdateHost({ ...rawFocusedHost, fontFamily: fontFamilyId, fontFamilyOverride: true }); } }); - }, [focusedHost, focusedFontFamilyId, isFocusedHostEphemeral, onUpdateTerminalFontFamilyId, onUpdateHost, rawFocusedHost]); + }, []); const handleFontFamilyResetForFocusedSession = useCallback(() => { + const { + focusedHost, + isFocusedHostEphemeral, + onUpdateHost, + rawFocusedHost, + } = latestRef.current; if (!focusedHost || isFocusedHostEphemeral || !rawFocusedHost) return; onUpdateHost(clearHostFontFamilyOverride(rawFocusedHost)); - }, [focusedHost, isFocusedHostEphemeral, onUpdateHost, rawFocusedHost]); + }, []); const handleFontSizeChangeForFocusedSession = useCallback((newFontSize: number) => { + // Snapshot target host/session state synchronously before startTransition + // so a focus change prior to transition commit cannot mutate another host. + const { + activeSession, + activeWorkspace, + focusedHost, + focusedFontSize, + focusedSessionId, + isFocusedHostEphemeral, + onUpdateSessionFontSize, + onUpdateTerminalFontSize, + onUpdateHost, + rawFocusedHost, + } = latestRef.current; if (!focusedHost || newFontSize === focusedFontSize) return; startTransition(() => { if (activeWorkspace && focusedSessionId) { @@ -253,9 +342,19 @@ export function useTerminalThemePanelState({ onUpdateHost({ ...rawFocusedHost, fontSize: newFontSize, fontSizeOverride: true }); } }); - }, [activeSession, activeWorkspace, focusedHost, focusedFontSize, focusedSessionId, isFocusedHostEphemeral, onUpdateSessionFontSize, onUpdateTerminalFontSize, onUpdateHost, rawFocusedHost]); + }, []); const handleFontSizeResetForFocusedSession = useCallback(() => { + const { + activeSession, + activeWorkspace, + focusedHost, + focusedSessionId, + isFocusedHostEphemeral, + onClearSessionFontSizeOverride, + onUpdateHost, + rawFocusedHost, + } = latestRef.current; if (!focusedHost) return; if (activeWorkspace && focusedSessionId) { onClearSessionFontSizeOverride?.(focusedSessionId); @@ -268,29 +367,45 @@ export function useTerminalThemePanelState({ } if (!rawFocusedHost) return; onUpdateHost(clearHostFontSizeOverride(rawFocusedHost)); - }, [activeSession, activeWorkspace, focusedHost, focusedSessionId, isFocusedHostEphemeral, onClearSessionFontSizeOverride, onUpdateHost, rawFocusedHost]); + }, []); const handleFontWeightChangeForFocusedSession = useCallback((newFontWeight: number) => { + // Snapshot target host/session state synchronously before startTransition + // so a focus change prior to transition commit cannot mutate another host. + const { + focusedHost, + focusedFontWeight, + hostMap, + isFocusedHostEphemeral, + onUpdateTerminalFontWeight, + onUpdateHost, + } = latestRef.current; if (!focusedHost || newFontWeight === focusedFontWeight) return; + const rawHost = isFocusedHostEphemeral ? undefined : hostMap.get(focusedHost.id); startTransition(() => { if (isFocusedHostEphemeral) { onUpdateTerminalFontWeight?.(newFontWeight); return; } - const rawHost = hostMap.get(focusedHost.id); if (rawHost) { onUpdateHost({ ...rawHost, fontWeight: newFontWeight, fontWeightOverride: true }); } }); - }, [focusedHost, focusedFontWeight, isFocusedHostEphemeral, onUpdateTerminalFontWeight, onUpdateHost, hostMap]); + }, []); const handleFontWeightResetForFocusedSession = useCallback(() => { + const { + focusedHost, + hostMap, + isFocusedHostEphemeral, + onUpdateHost, + } = latestRef.current; if (!focusedHost || isFocusedHostEphemeral) return; const rawHost = hostMap.get(focusedHost.id); if (rawHost) { onUpdateHost(clearHostFontWeightOverride(rawHost)); } - }, [focusedHost, isFocusedHostEphemeral, onUpdateHost, hostMap]); + }, []); const composeBarThemeColors = useMemo(() => { if (!activeWorkspace || !focusedSessionId) return terminalTheme.colors;