"use client";

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

/**
 * Mounts a setInterval that calls router.refresh() every `intervalMs`.
 * router.refresh() re-runs the matching Server Component fetch and
 * patches the rendered output in — no full page reload, no flicker.
 *
 * Renders nothing.
 */
export default function AutoRefresh({
  intervalMs = 30_000,
}: {
  intervalMs?: number;
}) {
  const router = useRouter();
  useEffect(() => {
    const id = setInterval(() => router.refresh(), intervalMs);
    return () => clearInterval(id);
  }, [router, intervalMs]);
  return null;
}
