import { useQuery } from "@tanstack/react-query"; import { listTables, type BaserowTableSummary } from "../services/admin-client"; import type { BridgeTable } from "../types/database-view.types"; export const TABLES_QUERY_KEY = ["bridge-admin-tables"] as const; /** * Fetch tables of a Baserow database via the bridge admin endpoint. * * The legacy `GET /api/v1/tables` requires a Baserow user JWT and a databaseId * filter — neither was wired into the modal — so we list via the admin client * (`GET /api/v1/admin/tables?databaseId=X`) which uses a service-account JWT. * * The query is disabled until a databaseId is provided. */ export function useTables( databaseId?: number | null, bridgeUrl?: string | null, ) { return useQuery({ queryKey: [...TABLES_QUERY_KEY, databaseId ?? null, bridgeUrl ?? null], queryFn: async () => { if (!databaseId) return []; const rows = await listTables(databaseId, bridgeUrl); return rows.map( (t: BaserowTableSummary): BridgeTable => ({ id: String(t.id), name: t.name, databaseId: t.database_id, }), ); }, enabled: Boolean(databaseId), staleTime: 30_000, }); }