Vectrum
Sign In
17 lines · 594 Bytes · TypeScript
"use client";

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

/** Re-renders the page every few seconds while something on it is still running (and the tab is visible). */
export function LiveRefresh({ active, interval = 2000 }: { active: boolean; interval?: number }) {
  const router = useRouter();
  useEffect(() => {
    if (!active) return;
    const timer = setInterval(() => {
      if (document.visibilityState === "visible") router.refresh();
    }, interval);
    return () => clearInterval(timer);
  }, [active, interval, router]);
  return null;
}