import { useQuery } from "@tanstack/react-query";
import { getTableData } from "@/lib/tauri";

export function useTableData(params: {
  connectionId: string | null;
  schema: string;
  table: string;
  page: number;
  pageSize: number;
  sortColumn?: string;
  sortDirection?: string;
  filter?: string;
}) {
  const { connectionId, ...rest } = params;
  return useQuery({
    queryKey: ["table-data", connectionId, rest],
    queryFn: () =>
      getTableData({
        connectionId: connectionId!,
        ...rest,
      }),
    enabled: !!connectionId && !!params.schema && !!params.table,
    placeholderData: (prev) => prev,
  });
}
