Files
supabase/apps/studio/data/branches/branch-diff-query.ts
T
Han QiaoandCharis 77561e8e09 chore: replace branch id with branch ref (#38735)
* chore: remove unused mutation to disable branching

* chore: use branch ref in delete mutation

* chore: use branch ref in diff query

* chore: use branch ref in push mutation

* chore: use branch ref in detail query

* chore: use branch ref in update mutation

* Update apps/studio/data/branches/branch-query.ts

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

---------

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>
2025-09-17 10:31:01 +08:00

58 lines
1.5 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import { IS_PLATFORM } from 'lib/constants'
import type { ResponseError } from 'types'
import { branchKeys } from './keys'
export type BranchDiffVariables = {
branchRef: string
projectRef: string
includedSchemas?: string
}
export async function getBranchDiff({
branchRef,
includedSchemas,
}: Pick<BranchDiffVariables, 'branchRef' | 'includedSchemas'>) {
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<ReturnType<typeof getBranchDiff>>
export const useBranchDiffQuery = (
{ branchRef, projectRef, includedSchemas }: BranchDiffVariables,
{
enabled = true,
...options
}: Omit<UseQueryOptions<BranchDiffData, ResponseError>, 'queryKey' | 'queryFn'> = {}
) =>
useQuery<BranchDiffData, ResponseError>(
branchKeys.diff(projectRef, branchRef),
() => getBranchDiff({ branchRef, includedSchemas }),
{
enabled: IS_PLATFORM && enabled && Boolean(branchRef),
...options,
}
)