Files
supabase/apps/studio/data/replication/tables-query.ts
T
Alaister YoungandJoshen Lim 8855d05803 chore(studio): swap react-query to object syntax (#39842)
* chore(studio): swap react-query to object syntax

* Fix small issues found

* Fix realtime settings

* Nit

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-10-27 09:38:27 +01:00

39 lines
1.4 KiB
TypeScript

import { UseQueryOptions, useQuery } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import { ResponseError } from 'types'
import { replicationKeys } from './keys'
type ReplicationTablesParams = { projectRef?: string; sourceId?: number }
async function fetchReplicationTables(
{ projectRef, sourceId }: ReplicationTablesParams,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
if (!sourceId) throw new Error('sourceId is required')
const { data, error } = await get('/platform/replication/{ref}/sources/{source_id}/tables', {
params: { path: { ref: projectRef, source_id: sourceId } },
signal,
})
if (error) {
handleError(error)
}
return data
}
export type ReplicationTablesData = Awaited<ReturnType<typeof fetchReplicationTables>>
export const useReplicationTablesQuery = <TData = ReplicationTablesData>(
{ projectRef, sourceId }: ReplicationTablesParams,
{ enabled = true, ...options }: UseQueryOptions<ReplicationTablesData, ResponseError, TData> = {}
) =>
useQuery<ReplicationTablesData, ResponseError, TData>({
queryKey: replicationKeys.tables(projectRef, sourceId),
queryFn: ({ signal }) => fetchReplicationTables({ projectRef, sourceId }, signal),
enabled: enabled && typeof projectRef !== 'undefined' && typeof sourceId !== 'undefined',
...options,
})