fix(tui): address latest review feedback

This commit is contained in:
Brooklyn Nicholson 2026-04-26 13:56:26 -05:00
parent 2be5e181a9
commit a8bfe72d35
5 changed files with 17 additions and 87 deletions

View file

@ -1,6 +1,6 @@
import type { ScrollBoxHandle } from '@hermes/ink'
import type { RefObject } from 'react'
import { useCallback, useSyncExternalStore } from 'react'
import { useCallback, useMemo, useSyncExternalStore } from 'react'
export interface ViewportSnapshot {
atBottom: boolean
@ -45,6 +45,19 @@ export function viewportSnapshotKey(v: ViewportSnapshot) {
return `${v.atBottom ? 1 : 0}:${v.top}:${v.viewportHeight}:${v.scrollHeight}:${v.pending}`
}
const snapshotFromKey = (key: string): ViewportSnapshot => {
const [atBottom = '1', top = '0', viewportHeight = '0', scrollHeight = '0', pending = '0'] = key.split(':')
return {
atBottom: atBottom === '1',
bottom: Number(top) + Number(viewportHeight),
pending: Number(pending),
scrollHeight: Number(scrollHeight),
top: Number(top),
viewportHeight: Number(viewportHeight)
}
}
export function useViewportSnapshot(scrollRef: RefObject<ScrollBoxHandle | null>): ViewportSnapshot {
const key = useSyncExternalStore(
useCallback((cb: () => void) => scrollRef.current?.subscribe(cb) ?? (() => {}), [scrollRef]),
@ -52,7 +65,5 @@ export function useViewportSnapshot(scrollRef: RefObject<ScrollBoxHandle | null>
() => viewportSnapshotKey(EMPTY)
)
void key
return getViewportSnapshot(scrollRef.current)
return useMemo(() => snapshotFromKey(key), [key])
}