import { useQuery } from '@tanstack/react-query' import { get, handleError } from 'data/fetchers' import { IS_PLATFORM } from 'lib/constants' import type { ResponseError, UseCustomQueryOptions } from 'types' import { branchKeys } from './keys' export type BranchDiffVariables = { branchRef: string projectRef: string includedSchemas?: string } export async function getBranchDiff({ branchRef, includedSchemas, }: Pick) { const { data: diffData, error } = await get('/v1/branches/{branch_id_or_ref}/diff', { params: { path: { branch_id_or_ref: branchRef }, query: includedSchemas ? { included_schemas: includedSchemas } : undefined, }, headers: { Accept: 'text/plain', }, parseAs: 'text', }) if (error) { handleError(error) } // Handle empty object responses (when no diff exists) if (typeof diffData === 'object' && Object.keys(diffData).length === 0) { return '' } return diffData || '' } type BranchDiffData = Awaited> export const useBranchDiffQuery = ( { branchRef, projectRef, includedSchemas }: BranchDiffVariables, { enabled = true, ...options }: Omit, 'queryKey' | 'queryFn'> = {} ) => useQuery({ queryKey: branchKeys.diff(projectRef, branchRef), queryFn: () => getBranchDiff({ branchRef, includedSchemas }), enabled: IS_PLATFORM && enabled && Boolean(branchRef), ...options, })