llama-stack-mirror/llama_stack/ui/hooks/useInfiniteScroll.ts
Eric Huang 66e217fea7 feat(ui): add infinite scroll pagination to chat completions/responses logs table
## Summary:

  This commit adds infinite scroll pagination to the
  chat completions and responses tables.


## Test Plan:
  1. Run unit tests: npm run test
  2. Manual testing: Navigate to chat
  completions/responses pages
  3. Verify infinite scroll triggers when approaching
  bottom
  4. Added playwright tests: npm run test:e2e
2025-06-18 09:54:23 -07:00

55 lines
1.4 KiB
TypeScript

"use client";
import { useRef, useEffect } from "react";
interface UseInfiniteScrollOptions {
/** Whether the feature is enabled (e.g., hasMore data) */
enabled?: boolean;
/** Threshold for intersection (0-1, how much of sentinel must be visible) */
threshold?: number;
/** Margin around root to trigger earlier (e.g., "100px" to load 100px before visible) */
rootMargin?: string;
}
/**
* Custom hook for infinite scroll using Intersection Observer
*
* @param onLoadMore - Callback to load more data
* @param options - Configuration options
* @returns ref to attach to sentinel element
*/
export function useInfiniteScroll(
onLoadMore: (() => void) | undefined,
options: UseInfiniteScrollOptions = {},
) {
const { enabled = true, threshold = 0.1, rootMargin = "100px" } = options;
const sentinelRef = useRef<HTMLTableRowElement>(null);
useEffect(() => {
if (!onLoadMore || !enabled) return;
const observer = new IntersectionObserver(
(entries) => {
const [entry] = entries;
if (entry.isIntersecting) {
onLoadMore();
}
},
{
threshold,
rootMargin,
},
);
const sentinel = sentinelRef.current;
if (sentinel) {
observer.observe(sentinel);
}
return () => {
observer.disconnect();
};
}, [onLoadMore, enabled, threshold, rootMargin]);
return sentinelRef;
}