Skip to content

The agent that grows with you

fix(desktop): reseed font controls after config refetch

KoNit-Kdf832c86a2da
diff --git a/apps/desktop/src/app/settings/chat-font-setting.test.tsx b/apps/desktop/src/app/settings/chat-font-setting.test.tsx
index b3705ea64e4..a279c9ff1fb 100644
--- a/apps/desktop/src/app/settings/chat-font-setting.test.tsx
+++ b/apps/desktop/src/app/settings/chat-font-setting.test.tsx
@@ -8,8 +8,10 @@ import { ChatFontSetting } from './chat-font-setting'
const mocks = vi.hoisted(() => ({
cache: vi.fn(),
+ configUpdatedAt: 1,
loadedConfig: {} as Record<string, unknown>,
notifyError: vi.fn(),
+ profileSwitch: null as null | (() => void),
save: vi.fn()
}))
@@ -41,11 +43,13 @@ vi.mock('@/store/notifications', () => ({
vi.mock('../hooks/use-config-record', () => ({
setHermesConfigCache: (config: Record<string, unknown>) => mocks.cache(config),
- useHermesConfigRecord: () => ({ data: mocks.loadedConfig })
+ useHermesConfigRecord: () => ({ data: mocks.loadedConfig, dataUpdatedAt: mocks.configUpdatedAt })
}))
vi.mock('../hooks/use-on-profile-switch', () => ({
- useOnProfileSwitch: () => {}
+ useOnProfileSwitch: (callback: () => void) => {
+ mocks.profileSwitch = callback
+ }
}))
async function flushAutosave() {
@@ -59,8 +63,10 @@ async function flushAutosave() {
describe('ChatFontSetting', () => {
beforeEach(() => {
vi.useFakeTimers()
+ mocks.configUpdatedAt = 1
mocks.loadedConfig = { desktop: { font_family: '', repo_scan_enabled: true } }
mocks.save.mockResolvedValue({ ok: true })
+ mocks.profileSwitch = null
$chatFontFamily.set('')
})
@@ -95,6 +101,23 @@ describe('ChatFontSetting', () => {
expect($chatFontFamily.get()).toBe('Lexend')
expect(mocks.notifyError).toHaveBeenCalledWith(expect.any(Error), 'Autosave failed')
})
+
+ it('reseeds after a profile refetch reuses the cached config record', () => {
+ const sharedConfig = { desktop: { font_family: 'Avenir' } }
+ mocks.loadedConfig = sharedConfig
+ const view = render(<ChatFontSetting />)
+
+ expect($chatFontFamily.get()).toBe('Avenir')
+ act(() => mocks.profileSwitch?.())
+ expect($chatFontFamily.get()).toBe('')
+ expect((screen.getByRole('combobox', { name: 'Chat Font' }) as HTMLInputElement).disabled).toBe(true)
+
+ mocks.configUpdatedAt = 2
+ view.rerender(<ChatFontSetting />)
+
+ expect((screen.getByRole('combobox', { name: 'Chat Font' }) as HTMLInputElement).value).toBe('Avenir')
+ expect($chatFontFamily.get()).toBe('Avenir')
+ })
})
describe('resolveChatFontFamily', () => {
diff --git a/apps/desktop/src/app/settings/chat-font-setting.tsx b/apps/desktop/src/app/settings/chat-font-setting.tsx
index dbd1f4e7b0e..f8a11d15e5d 100644
--- a/apps/desktop/src/app/settings/chat-font-setting.tsx
+++ b/apps/desktop/src/app/settings/chat-font-setting.tsx
@@ -29,30 +29,43 @@ function fontFamilyFromConfig(config: HermesConfigRecord): string {
export function ChatFontSetting() {
const { t } = useI18n()
const copy = t.settings.appearance
- const { data: loadedConfig } = useHermesConfigRecord()
+ const { data: loadedConfig, dataUpdatedAt } = useHermesConfigRecord()
const [draft, setDraft] = useState<string | null>(null)
- const [staleConfig, setStaleConfig] = useState<HermesConfigRecord | null>(null)
+ const [profilePending, setProfilePending] = useState(false)
const [saveVersion, setSaveVersion] = useState(0)
const saveVersionRef = useRef(0)
+ const staleConfigStamp = useRef<null | number>(null)
const cancelPendingSave = () => {
saveVersionRef.current = 0
}
useEffect(() => {
- if (!loadedConfig || draft !== null || loadedConfig === staleConfig) {
+ if (!loadedConfig || draft !== null || profilePending) {
return
}
const value = fontFamilyFromConfig(loadedConfig)
setDraft(value)
setChatFontFamilyFromConfig(value)
- }, [draft, loadedConfig, staleConfig])
+ }, [draft, loadedConfig, profilePending])
+
+ // A profile switch invalidates without clearing the shared query. Wait for
+ // its next successful result rather than comparing object identity: React
+ // Query may structurally share an unchanged config record across refetches.
+ // eslint-disable-next-line no-restricted-syntax -- query freshness stamp is a non-rendering latch
+ useEffect(() => {
+ if (profilePending && staleConfigStamp.current !== null && dataUpdatedAt !== staleConfigStamp.current) {
+ staleConfigStamp.current = null
+ setProfilePending(false)
+ }
+ }, [dataUpdatedAt, profilePending])
useOnProfileSwitch(() => {
saveVersionRef.current += 1
setDraft(null)
- setStaleConfig(loadedConfig ?? null)
+ staleConfigStamp.current = dataUpdatedAt
+ setProfilePending(true)
setSaveVersion(0)
setChatFontFamilyFromConfig('')
})
diff --git a/apps/desktop/src/app/settings/terminal-font-setting.test.tsx b/apps/desktop/src/app/settings/terminal-font-setting.test.tsx
index b3ba19733aa..3f708ed1807 100644
--- a/apps/desktop/src/app/settings/terminal-font-setting.test.tsx
+++ b/apps/desktop/src/app/settings/terminal-font-setting.test.tsx
@@ -8,6 +8,7 @@ import { TerminalFontSetting } from './terminal-font-setting'
const mocks = vi.hoisted(() => ({
cache: vi.fn(),
+ configUpdatedAt: 1,
loadedConfig: {} as Record<string, unknown>,
notifyError: vi.fn(),
profileSwitch: null as null | (() => void),
@@ -41,7 +42,7 @@ vi.mock('@/store/notifications', () => ({
vi.mock('../hooks/use-config-record', () => ({
setHermesConfigCache: (config: Record<string, unknown>) => mocks.cache(config),
- useHermesConfigRecord: () => ({ data: mocks.loadedConfig })
+ useHermesConfigRecord: () => ({ data: mocks.loadedConfig, dataUpdatedAt: mocks.configUpdatedAt })
}))
vi.mock('../hooks/use-on-profile-switch', () => ({
@@ -61,6 +62,7 @@ async function flushAutosave() {
describe('TerminalFontSetting', () => {
beforeEach(() => {
vi.useFakeTimers()
+ mocks.configUpdatedAt = 1
mocks.loadedConfig = {
display: { skin: 'hermes' },
terminal: { backend: 'local', cwd: '/workspace', font_family: '' }
@@ -134,8 +136,9 @@ describe('TerminalFontSetting', () => {
expect(mocks.notifyError).toHaveBeenCalledWith(expect.any(Error), 'Autosave failed')
})
- it('drops the prior profile font and reseeds from the next profile', () => {
- mocks.loadedConfig = { terminal: { font_family: 'MesloLGS NF' } }
+ it('drops the prior profile font and reseeds after a refetch reuses its cached record', () => {
+ const sharedConfig = { terminal: { font_family: 'MesloLGS NF' } }
+ mocks.loadedConfig = sharedConfig
const view = render(<TerminalFontSetting />)
expect($terminalFontFamily.get()).toBe('MesloLGS NF')
@@ -143,10 +146,10 @@ describe('TerminalFontSetting', () => {
expect($terminalFontFamily.get()).toBe('')
expect((screen.getByRole('combobox', { name: 'Terminal Font' }) as HTMLInputElement).disabled).toBe(true)
- mocks.loadedConfig = { terminal: { font_family: 'Hack Nerd Font' } }
+ mocks.configUpdatedAt = 2
view.rerender(<TerminalFontSetting />)
- expect((screen.getByRole('combobox', { name: 'Terminal Font' }) as HTMLInputElement).value).toBe('Hack Nerd Font')
- expect($terminalFontFamily.get()).toBe('Hack Nerd Font')
+ expect((screen.getByRole('combobox', { name: 'Terminal Font' }) as HTMLInputElement).value).toBe('MesloLGS NF')
+ expect($terminalFontFamily.get()).toBe('MesloLGS NF')
})
})
diff --git a/apps/desktop/src/app/settings/terminal-font-setting.tsx b/apps/desktop/src/app/settings/terminal-font-setting.tsx
index 52b10bc28f7..94a8078c43a 100644
--- a/apps/desktop/src/app/settings/terminal-font-setting.tsx
+++ b/apps/desktop/src/app/settings/terminal-font-setting.tsx
@@ -28,17 +28,16 @@ function fontFamilyFromConfig(config: HermesConfigRecord): string {
export function TerminalFontSetting() {
const { t } = useI18n()
const copy = t.settings.appearance
- const { data: loadedConfig } = useHermesConfigRecord()
+ const { data: loadedConfig, dataUpdatedAt } = useHermesConfigRecord()
// draft === null ⇔ unseeded: nothing painted yet for this profile. The
- // profile-switch handler resets it to null and records the config object
- // it was looking at (`staleConfig`) — the seed effect refuses to re-seed
- // from that same object, so the previous profile's cached record can't
- // repopulate the field; the next profile's fetch (a new object) seeds it.
- // `draft` itself is the seed marker (no ref mirroring, per the lint rule).
+ // profile-switch handler keeps it unseeded until a config refetch completes;
+ // the timestamp is the freshness proof because React Query can reuse the
+ // same config object when the next profile has identical settings.
const [draft, setDraft] = useState<string | null>(null)
- const [staleConfig, setStaleConfig] = useState<HermesConfigRecord | null>(null)
+ const [profilePending, setProfilePending] = useState(false)
const [saveVersion, setSaveVersion] = useState(0)
const saveVersionRef = useRef(0)
+ const staleConfigStamp = useRef<null | number>(null)
// Lexically outside every useEffect so async save callbacks can cancel the
// in-flight version without assigning to a ref inside an effect body.
@@ -47,19 +46,28 @@ export function TerminalFontSetting() {
}
useEffect(() => {
- if (!loadedConfig || draft !== null || loadedConfig === staleConfig) {
+ if (!loadedConfig || draft !== null || profilePending) {
return
}
const value = fontFamilyFromConfig(loadedConfig)
setDraft(value)
setTerminalFontFamilyFromConfig(value)
- }, [draft, loadedConfig, staleConfig])
+ }, [draft, loadedConfig, profilePending])
+
+ // eslint-disable-next-line no-restricted-syntax -- query freshness stamp is a non-rendering latch
+ useEffect(() => {
+ if (profilePending && staleConfigStamp.current !== null && dataUpdatedAt !== staleConfigStamp.current) {
+ staleConfigStamp.current = null
+ setProfilePending(false)
+ }
+ }, [dataUpdatedAt, profilePending])
useOnProfileSwitch(() => {
saveVersionRef.current += 1
setDraft(null)
- setStaleConfig(loadedConfig ?? null)
+ staleConfigStamp.current = dataUpdatedAt
+ setProfilePending(true)
setSaveVersion(0)
// Do not show the previous profile's font while the new profile loads.
setTerminalFontFamilyFromConfig('')