"use client";

import { useEffect } from "react";
import { useRouter } from "next/navigation";

type AutoRefreshProps = {
  intervalMs?: number;
};

export default function AutoRefresh({ intervalMs = 15000 }: AutoRefreshProps): null {
  const router = useRouter();

  useEffect(() => {
    if (intervalMs <= 0) return undefined;
    const id = setInterval(() => {
      router.refresh();
    }, intervalMs);
    return () => {
      clearInterval(id);
    };
  }, [intervalMs, router]);

  return null;
}
