import { useEffect } from "react";
import { useQueryClient } from "@tanstack/react-query";
import { quoteKeys } from "./queries";

export function useQuoteEvents() {
  const queryClient = useQueryClient();

  useEffect(() => {
    const eventSource = new EventSource("/api/quotes/events");

    eventSource.addEventListener("quotes-updated", () => {
      queryClient.invalidateQueries({ queryKey: quoteKeys.all });
    });

    return () => eventSource.close();
  }, [queryClient]);
}
