hermes-agent/ui-tui/src/hooks/useQueue.ts
Brooklyn Nicholson 718088c382 fix(tui): copilot review on #16707 — naming, label consistency, esc priority
- Rename `removeAt` → `removeAtInPlace` and document the mutation
  contract; the old name read like a non-mutating helper.
- Hotkey table + queue header: use `Ctrl+X` / `Esc` to match the
  rest of the UI (was `⌃X` / `esc`).
- Render the queued header as a single template literal so JSX
  text-node whitespace can't sneak into the rendered line.
- Make `Esc` while editing beat the `terminal.hasSelection` clear:
  the header promises 'Esc cancel', so an active selection
  shouldn't silently consume the keystroke.
2026-04-27 15:37:54 -05:00

76 lines
1.6 KiB
TypeScript

import { useCallback, useRef, useState } from 'react'
// Mutates `arr` in place; returned reference is the same input array, kept
// so callers can chain. Use `Array.prototype.toSpliced` if you need a copy.
export function removeAtInPlace<T>(arr: T[], i: number): T[] {
if (i < 0 || i >= arr.length) {
return arr
}
arr.splice(i, 1)
return arr
}
export function useQueue() {
const queueRef = useRef<string[]>([])
const [queuedDisplay, setQueuedDisplay] = useState<string[]>([])
const queueEditRef = useRef<number | null>(null)
const [queueEditIdx, setQueueEditIdx] = useState<number | null>(null)
const syncQueue = useCallback(() => setQueuedDisplay([...queueRef.current]), [])
const setQueueEdit = useCallback((idx: number | null) => {
queueEditRef.current = idx
setQueueEditIdx(idx)
}, [])
const enqueue = useCallback(
(text: string) => {
queueRef.current.push(text)
syncQueue()
},
[syncQueue]
)
const dequeue = useCallback(() => {
const head = queueRef.current.shift()
syncQueue()
return head
}, [syncQueue])
const replaceQ = useCallback(
(i: number, text: string) => {
queueRef.current[i] = text
syncQueue()
},
[syncQueue]
)
const removeQ = useCallback(
(i: number) => {
const before = queueRef.current.length
removeAtInPlace(queueRef.current, i)
if (queueRef.current.length !== before) {
syncQueue()
}
},
[syncQueue]
)
return {
dequeue,
enqueue,
queueEditIdx,
queueEditRef,
queueRef,
queuedDisplay,
removeQ,
replaceQ,
setQueueEdit,
syncQueue
}
}