Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
1.5 KB
Raw Blame
"use client";

import { useEffect, useRef, type ReactNode } from "react";
import { Loader2 } from "@multica/ui/icons";

/** Sentinel that triggers once per visibility transition. Callback/loading
 * changes are read through refs/props without rebuilding the observer, so a
 * completed page fetch cannot retrigger while the sentinel remains visible. */
export function InfiniteScrollSentinel({
  onVisible,
  loading,
  label,
  rootMargin = "100px",
  className = "flex items-center justify-center gap-1.5 py-2",
}: {
  onVisible: () => void;
  loading: boolean;
  /** Shown next to the spinner while loading (e.g. "Loading…"). Gives the
   * bare spinner context so a slow page fetch does not read as "stuck". */
  label?: ReactNode;
  rootMargin?: string;
  className?: string;
}) {
  const sentinelRef = useRef<HTMLDivElement>(null);
  const onVisibleRef = useRef(onVisible);
  onVisibleRef.current = onVisible;

  useEffect(() => {
    const node = sentinelRef.current;
    if (!node) return;
    const observer = new IntersectionObserver(
      ([entry]) => { if (entry?.isIntersecting) onVisibleRef.current(); },
      { rootMargin },
    );
    observer.observe(node);
    return () => observer.disconnect();
  }, [rootMargin]);

  return (
    <div ref={sentinelRef} className={className} aria-hidden>
      {loading && (
        <>
          <Loader2 className="size-3 animate-spin text-muted-foreground" />
          {label && (
            <span className="text-caption text-muted-foreground">{label}</span>
          )}
        </>
      )}
    </div>
  );
}