import { useQuery } from "@tanstack/react-query"; import { getBridgeClient, resolveBridgeUrl } from "../services/bridge-client"; import type { BridgeView } from "../types/database-view.types"; export const viewsQueryKey = (tableId: string, bridgeUrl: string) => ["bridge-views", tableId, bridgeUrl] as const; /** * Fetches the list of views for a given table. * Used in the insert-database-modal step 2. */ export function useViews( tableId: string | null | undefined, bridgeUrl?: string | null, ) { const url = resolveBridgeUrl(bridgeUrl); return useQuery({ queryKey: viewsQueryKey(tableId ?? "", url), enabled: Boolean(tableId), queryFn: async () => { const client = getBridgeClient(url); const res = await (client.get( `/api/v1/views/table/${tableId}`, ) as unknown as Promise<{ data: BridgeView[] } | BridgeView[]>); return Array.isArray(res) ? res : (res as { data: BridgeView[] }).data ?? []; }, staleTime: 30_000, }); }