fix(tui): isolate turn state from app render

This commit is contained in:
Brooklyn Nicholson 2026-04-26 15:40:38 -05:00
parent 6a3873942f
commit f6846205cc
3 changed files with 61 additions and 8 deletions

View file

@ -5,6 +5,8 @@ import { pick, toolTrailLabel } from '../lib/text.js'
import type { ActiveTool } from '../types.js'
import { turnController } from './turnController.js'
import { useTurnSelector } from './turnStore.js'
import { getUiState } from './uiStore.js'
const DELAY_MS = 8_000
const INTERVAL_MS = 10_000
@ -15,21 +17,28 @@ interface Slot {
lastAt: number
}
export function useLongRunToolCharms(busy: boolean, tools: ActiveTool[]) {
export function useLongRunToolCharms() {
const tools = useTurnSelector(state => state.tools)
const slots = useRef(new Map<string, Slot>())
useEffect(() => {
if (!busy || !tools.length) {
if (!getUiState().busy || !tools.length) {
slots.current.clear()
return
}
const tick = () => {
if (!getUiState().busy) {
slots.current.clear()
return
}
const now = Date.now()
const liveIds = new Set(tools.map(t => t.id))
for (const key of [...slots.current.keys()]) {
for (const key of Array.from(slots.current.keys())) {
if (!liveIds.has(key)) {
slots.current.delete(key)
}
@ -57,5 +66,5 @@ export function useLongRunToolCharms(busy: boolean, tools: ActiveTool[]) {
const id = setInterval(tick, 1000)
return () => clearInterval(id)
}, [busy, tools])
}, [tools])
}