import { useQuery, useQueryClient } from "@tanstack/react-query";
import { ipc } from "@/ipc/types";
import { queryKeys } from "@/lib/queryKeys";

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

  const { data, isLoading, error } = useQuery({
    queryKey: queryKeys.connectors.all,
    queryFn: async () => {
      const response = await ipc.connector.listConnectors();
      return response.connectors;
    },
  });

  const refreshConnectors = () => {
    return queryClient.invalidateQueries({
      queryKey: queryKeys.connectors.all,
    });
  };

  return {
    connectors: data ?? [],
    loading: isLoading,
    error: error ?? null,
    refreshConnectors,
  };
}
