The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { LegendListRef } from "@legendapp/list/react"; |
| 2 | import { type RefObject } from "react"; |
| 3 | import { useStableCallback } from "./useStableCallback"; |
| 4 | |
| 5 | /** Refresh scroll-derived UI when layout changes without a scroll event. */ |
| 6 | export function useLegendListRef(listRef: RefObject<LegendListRef | null>, onChange: () => void) { |
| 7 | const notify = useStableCallback(onChange); |
| 8 | return (list: LegendListRef | null) => { |
| 9 | listRef.current = list; |
| 10 | if (!list) return; |
| 11 | const state = list.getState(); |
| 12 | const stops = [ |
| 13 | state.listen("totalSize", notify), |
| 14 | state.listen("headerSize", notify), |
| 15 | state.listen("footerSize", notify), |
| 16 | state.listen("isAtStart", notify), |
| 17 | state.listen("isAtEnd", notify), |
| 18 | ]; |
| 19 | notify(); |
| 20 | return () => { |
| 21 | for (const stop of stops) stop(); |
| 22 | listRef.current = null; |
| 23 | }; |
| 24 | }; |
| 25 | } |