Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

918 B
Raw Blame
"use client";

import { useEffect, useRef } from "react";
import { APP_CONSTANTS } from "@/lib/constants";

// Infinite scroll: auto-load next page when sentinel is visible
export function useJobsInfiniteScroll(
  loadedCount: number,
  totalJobs: number,
  loadingMore: boolean,
  onLoadMore: () => void,
) {
  const sentinelRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const sentinel = sentinelRef.current;
    if (!sentinel) return;

    const observer = new IntersectionObserver(
      (entries) => {
        if (
          entries[0].isIntersecting &&
          !loadingMore &&
          loadedCount < totalJobs
        ) {
          onLoadMore();
        }
      },
      { threshold: APP_CONSTANTS.INTERSECTION_OBSERVER_THRESHOLD },
    );

    observer.observe(sentinel);
    return () => observer.disconnect();
  }, [loadedCount, totalJobs, loadingMore, onLoadMore]);

  return sentinelRef;
}