refactor(tui): keep useMainApp under its size cap after the image-row changes

The inline-images and zero-height fixes added lines to useMainApp, which is
already over the 300-line target and may only shrink. Move the height-cache
bucket key and the measured-height copy into named helpers above it.
This commit is contained in:
Teknium committed 2026-11-04 13:26:30 -08:00
1 parent 32a2c3bad6
commit ce82757d2a
1 file changed
+28 -11
+28 -11
View File
@@ -258,6 +258,32 @@ export async function startPromptLiveSession({
return sid
}
// One bucket per layout that changes row heights (width, prompt, compact, inline images, details).
function heightCacheBucket(
ui: { compact?: boolean; inlineImages?: boolean; sid?: null | string; theme: { brand: { prompt: string } } },
cols: number,
detailsLayoutKey: string
): string {
const flags = `${ui.compact ? '1' : '0'}:${ui.inlineImages ? '1' : '0'}`
return `${ui.sid ?? 'draft'}:${cols}:${composerPromptWidth(ui.theme.brand.prompt)}:${flags}:${detailsLayoutKey}`
}
// 0 is a real measurement (a row that draws nothing), never a miss.
function copyMeasuredHeights(
heights: ReadonlyMap<string, number>,
rows: readonly { key: string }[],
cache: Map<string, number>
): void {
for (const row of rows) {
const h = heights.get(row.key)
if (h !== undefined) {
cache.set(row.key, h)
}
}
}
export function useMainApp(gw: GatewayClient) {
const { exit } = useApp()
const { stdout } = useStdout()
@@ -479,9 +505,7 @@ export function useMainApp(gw: GatewayClient) {
ui
)
const userPromptWidth = composerPromptWidth(ui.theme.brand.prompt)
// Inline images on/off changes which rows reserve image rows: a fresh bucket.
const heightCacheKey = `${ui.sid ?? 'draft'}:${cols}:${userPromptWidth}:${ui.compact ? '1' : '0'}:${ui.inlineImages ? '1' : '0'}:${detailsLayoutKey}`
const heightCacheKey = heightCacheBucket(ui, cols, detailsLayoutKey)
// Build a render-local snapshot. Registering/pruning the shared cache is a
// post-commit transition below, so an abandoned concurrent render cannot
@@ -542,14 +566,7 @@ export function useMainApp(gw: GatewayClient) {
const syncHeightCache = useCallback(
(heights: ReadonlyMap<string, number>) => {
for (const row of virtualRows) {
const h = heights.get(row.key)
// 0 is a real measurement (a row that draws nothing), not a miss.
if (h !== undefined) {
activeHeightCache.set(row.key, h)
}
}
copyMeasuredHeights(heights, virtualRows, activeHeightCache)
},
[activeHeightCache, virtualRows]
)