The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
import { useEffect, useRef, useState } from 'react'
import {
normalizeTerminalFontFamily,
resolveTerminalFontFamily,
setTerminalFontFamilyFromConfig,
TERMINAL_FONT_SUGGESTIONS
} from '@/app/right-sidebar/terminal/terminal-font'
import { Button } from '@/components/ui/button'
import { saveHermesConfig } from '@/hermes'
import { useI18n } from '@/i18n'
import { notifyError } from '@/store/notifications'
import type { HermesConfigRecord } from '@/types/hermes'
import { setHermesConfigCache, useHermesConfigRecord } from '../hooks/use-config-record'
import { useOnProfileSwitch } from '../hooks/use-on-profile-switch'
import { useProfileSwitchLatch } from '../hooks/use-profile-switch-latch'
import { ComboboxInput } from './combobox-input'
import { getNested, setNested } from './helpers'
import { ListRow } from './primitives'
const AUTOSAVE_DELAY_MS = 550
function fontFamilyFromConfig(config: HermesConfigRecord): string {
return normalizeTerminalFontFamily(getNested(config, 'terminal.font_family'))
}
export function TerminalFontSetting() {
const { t } = useI18n()
const copy = t.settings.appearance
const { data: loadedConfig, dataUpdatedAt, writeScope } = useHermesConfigRecord()
// draft === null ⇔ unseeded: nothing painted yet for this profile. The
// 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)
// The seed effect refuses to reseed while the query still carries the
// previous profile's stamp.
const { arm: armProfileLatch, pending: profilePending } = useProfileSwitchLatch({ dataUpdatedAt })
const [saveVersion, setSaveVersion] = useState(0)
const saveVersionRef = useRef(0)
// Lexically outside every useEffect so async save callbacks can cancel the
// in-flight version without assigning to a ref inside an effect body.
const cancelPendingSave = () => {
saveVersionRef.current = 0
}
useEffect(() => {
if (!loadedConfig || draft !== null || profilePending) {
return
}
const value = fontFamilyFromConfig(loadedConfig)
setDraft(value)
setTerminalFontFamilyFromConfig(value)
}, [draft, loadedConfig, profilePending])
useOnProfileSwitch(() => {
saveVersionRef.current += 1
setDraft(null)
armProfileLatch()
setSaveVersion(0)
// Do not show the previous profile's font while the new profile loads.
setTerminalFontFamilyFromConfig('')
})
useEffect(() => {
if (draft === null || saveVersion === 0 || !loadedConfig) {
return
}
const version = saveVersion
const value = normalizeTerminalFontFamily(draft)
// Already persisted (or a cache refresh confirmed it) — nothing to save.
// This also terminates the effect re-run after a successful save updates
// the shared config cache.
if (value === fontFamilyFromConfig(loadedConfig)) {
return
}
// The last successfully saved value IS what the shared config cache
// holds — successful saves write it back via setHermesConfigCache, so
// rollback re-derives from there instead of mirroring into a ref.
const rollback = fontFamilyFromConfig(loadedConfig)
const timeout = window.setTimeout(() => {
const next = setNested(loadedConfig, 'terminal.font_family', value)
// Sparse patch: PUT /api/config deep-merges, and echoing the cached
// snapshot would overwrite keys other surfaces changed since it loaded.
void saveHermesConfig(setNested({}, 'terminal.font_family', value), writeScope)
.then(result => {
if (!result.ok) {
throw new Error(t.settings.config.autosaveFailed)
}
if (saveVersionRef.current !== version) {
return
}
setHermesConfigCache(next)
})
.catch(error => {
if (saveVersionRef.current !== version) {
return
}
cancelPendingSave()
setSaveVersion(0)
setDraft(rollback)
setTerminalFontFamilyFromConfig(rollback)
notifyError(error, t.settings.config.autosaveFailed)
})
}, AUTOSAVE_DELAY_MS)
return () => window.clearTimeout(timeout)
}, [draft, loadedConfig, saveVersion, t.settings.config.autosaveFailed, writeScope])
const update = (value: string) => {
saveVersionRef.current += 1
setDraft(value)
setSaveVersion(saveVersionRef.current)
setTerminalFontFamilyFromConfig(value)
}
const value = draft ?? ''
const previewFontFamily = resolveTerminalFontFamily(value)
return (
<ListRow
below={
<div className="mt-3 space-y-2">
<div className="flex items-center gap-3">
<ComboboxInput
aria-label={copy.terminalFontTitle}
className="flex-1"
disabled={draft === null}
onChange={update}
options={TERMINAL_FONT_SUGGESTIONS}
placeholder={copy.terminalFontPlaceholder}
renderOption={font => <span style={{ fontFamily: resolveTerminalFontFamily(font) }}>{font}</span>}
value={value}
/>
<Button disabled={!value || draft === null} onClick={() => update('')} size="inline" variant="text">
{copy.terminalFontReset}
</Button>
</div>
<div
aria-label={copy.terminalFontPreview}
className="overflow-hidden px-1 py-2 text-sm text-(--ui-text-secondary)"
style={{ fontFamily: previewFontFamily }}
>
<span className="mr-2 text-[length:var(--conversation-caption-font-size)] text-(--ui-text-tertiary)">
{copy.terminalFontPreview}
</span>
<span> ~/project git:main ❯</span>
</div>
</div>
}
description={copy.terminalFontDesc}
title={copy.terminalFontTitle}
wide
/>
)
}