/**
 * 60-second interval to refresh duty list when viewing the current month.
 * Replaces state.todayRefreshInterval from webapp/js/main.js.
 */

"use client";

import { useEffect, useRef } from "react";

const AUTO_REFRESH_INTERVAL_MS = 60000;

/**
 * When isCurrentMonth is true, calls refresh() immediately, then every 60 seconds.
 * When isCurrentMonth becomes false or on unmount, the interval is cleared.
 */
export function useAutoRefresh(
  refresh: () => void,
  isCurrentMonth: boolean
): void {
  const refreshRef = useRef(refresh);
  refreshRef.current = refresh;

  useEffect(() => {
    if (!isCurrentMonth) return;
    refreshRef.current();
    const id = setInterval(() => refreshRef.current(), AUTO_REFRESH_INTERVAL_MS);
    return () => clearInterval(id);
  }, [isCurrentMonth]);
}
