"use client";
import { useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { subscribeAgentEvents } from './agent-events-bus.js';

// Mounted once on /agents. Asks Next.js to re-render the server tree whenever
// an agent is created out-of-band (WhatsApp, scheduler, sub-agent spawn) or
// a run completes (so last_heartbeat / status surface immediately).
export default function AgentsLiveRefresh() {
  const router = useRouter();
  useEffect(() => {
    return subscribeAgentEvents((event) => {
      if (event.type === 'agent_created' || event.type === 'agent_run_complete') {
        router.refresh();
      }
    });
  }, [router]);
  return null;
}
