import { QueryClient, useQuery, UseQueryOptions } from '@tanstack/react-query' import { executeSql, ExecuteSqlError } from '../sql/execute-sql-query' import { tableEditorKeys } from './keys' import { getTableEditorSql } from './table-editor-query-sql' import { Entity } from './table-editor-types' type TableEditorArgs = { id?: number } export type TableEditorVariables = TableEditorArgs & { projectRef?: string connectionString?: string | null } export async function getTableEditor( { projectRef, connectionString, id }: TableEditorVariables, signal?: AbortSignal ) { if (!id) { throw new Error('id is required') } const sql = getTableEditorSql(id) const { result } = await executeSql( { projectRef, connectionString, sql, queryKey: ['table-editor', id], }, signal ) return (result[0]?.entity ?? undefined) as Entity | undefined } export type TableEditorData = Awaited> export type TableEditorError = ExecuteSqlError export const useTableEditorQuery = ( { projectRef, connectionString, id }: TableEditorVariables, { enabled = true, ...options }: UseQueryOptions = {} ) => useQuery( tableEditorKeys.tableEditor(projectRef, id), ({ signal }) => getTableEditor({ projectRef, connectionString, id }, signal), { enabled: enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined' && !isNaN(id), refetchOnWindowFocus: false, refetchOnMount: false, staleTime: 5 * 60 * 1000, // 5 minutes ...options, } ) export function prefetchTableEditor( client: QueryClient, { projectRef, connectionString, id }: TableEditorVariables ) { return client.fetchQuery(tableEditorKeys.tableEditor(projectRef, id), ({ signal }) => getTableEditor( { projectRef, connectionString, id, }, signal ) ) }