Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Add RQs for branch management, and init branch dropdown component

Joshen Lim74efa6a47fc2
diff --git a/studio/components/layouts/AppLayout/AppHeader.tsx b/studio/components/layouts/AppLayout/AppHeader.tsx
index 05ba788f92a..dfb07019887 100644
--- a/studio/components/layouts/AppLayout/AppHeader.tsx
+++ b/studio/components/layouts/AppLayout/AppHeader.tsx
@@ -10,6 +10,7 @@ import OrganizationDropdown from './OrganizationDropdown'
import ProjectDropdown from './ProjectDropdown'
import SettingsButton from './SettingsButton'
import UserSettingsDropdown from './UserSettingsDropdown'
+import BranchDropdown from './BranchDropdown'
const AppHeader = () => {
const router = useRouter()
@@ -30,6 +31,7 @@ const AppHeader = () => {
</Link>
<OrganizationDropdown />
{ref !== undefined && <ProjectDropdown />}
+ <BranchDropdown />
</div>
<div className="flex items-center space-x-4">
diff --git a/studio/components/layouts/AppLayout/BranchDropdown.tsx b/studio/components/layouts/AppLayout/BranchDropdown.tsx
new file mode 100644
index 00000000000..d3f8f38ebaf
--- /dev/null
+++ b/studio/components/layouts/AppLayout/BranchDropdown.tsx
@@ -0,0 +1,12 @@
+import { useParams } from 'common'
+import { useBranchesQuery } from 'data/branches/branches-query'
+
+const BranchDropdown = () => {
+ const { ref: projectRef } = useParams()
+ const { data } = useBranchesQuery({ projectRef })
+ console.log(data)
+
+ return <div>Branch</div>
+}
+
+export default BranchDropdown
diff --git a/studio/data/branches/branch-create-mutation.ts b/studio/data/branches/branch-create-mutation.ts
new file mode 100644
index 00000000000..a9296b688af
--- /dev/null
+++ b/studio/data/branches/branch-create-mutation.ts
@@ -0,0 +1,65 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { post } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { branchKeys } from './keys'
+
+export type BranchCreateVariables = {
+ projectRef: string
+ branchName: string
+ gitBranch?: string
+ region?: string
+}
+
+export async function createBranch({
+ projectRef,
+ branchName,
+ gitBranch,
+ region,
+}: BranchCreateVariables) {
+ const { data, error } = await post('/v1/projects/{ref}/branches', {
+ params: {
+ path: { ref: projectRef },
+ },
+ body: {
+ branch_name: branchName,
+ git_branch: gitBranch,
+ region: region,
+ },
+ })
+
+ if (error) throw error
+ return data
+}
+
+type BranchCreateData = Awaited<ReturnType<typeof createBranch>>
+
+export const useBranchCreateMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<BranchCreateData, ResponseError, BranchCreateVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+ return useMutation<BranchCreateData, ResponseError, BranchCreateVariables>(
+ (vars) => createBranch(vars),
+ {
+ async onSuccess(data, variables, context) {
+ const { projectRef } = variables
+ await queryClient.invalidateQueries(branchKeys.list(projectRef))
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to create branch: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}
diff --git a/studio/data/branches/branch-delete-mutation.ts b/studio/data/branches/branch-delete-mutation.ts
new file mode 100644
index 00000000000..2f5974fcce7
--- /dev/null
+++ b/studio/data/branches/branch-delete-mutation.ts
@@ -0,0 +1,51 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { del } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { branchKeys } from './keys'
+
+export type BranchDeleteVariables = {
+ id: string
+ projectRef: string
+}
+
+export async function deleteBranch({ id }: BranchDeleteVariables) {
+ const { data, error } = await del('/v1/branches/{branch_id}', {
+ params: { path: { branch_id: id } },
+ })
+
+ if (error) throw error
+ return data
+}
+
+type BranchDeleteData = Awaited<ReturnType<typeof deleteBranch>>
+
+export const useBranchDeleteMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<BranchDeleteData, ResponseError, BranchDeleteVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+ return useMutation<BranchDeleteData, ResponseError, BranchDeleteVariables>(
+ (vars) => deleteBranch(vars),
+ {
+ async onSuccess(data, variables, context) {
+ const { projectRef } = variables
+ await queryClient.invalidateQueries(branchKeys.list(projectRef))
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to delete branch: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}
diff --git a/studio/data/branches/branch-query.ts b/studio/data/branches/branch-query.ts
new file mode 100644
index 00000000000..3eba27d674f
--- /dev/null
+++ b/studio/data/branches/branch-query.ts
@@ -0,0 +1,31 @@
+import { useQuery, UseQueryOptions } from '@tanstack/react-query'
+import { get } from 'data/fetchers'
+import { branchKeys } from './keys'
+import { ResponseError } from 'types'
+
+export type BranchVariables = {
+ id: string
+}
+
+export async function getBranch({ id }: BranchVariables, signal?: AbortSignal) {
+ const { data, error } = await get(`/v1/branches/{branch_id}`, {
+ params: { path: { branch_id: id } },
+ signal,
+ })
+
+ if (error) throw error
+ return data
+}
+
+export type BranchData = Awaited<ReturnType<typeof getBranch>>
+export type BranchError = ResponseError
+
+export const useBranchQuery = <TData = BranchData>(
+ { id }: BranchVariables,
+ { enabled = true, ...options }: UseQueryOptions<BranchData, BranchError, TData> = {}
+) =>
+ useQuery<BranchData, BranchError, TData>(
+ branchKeys.detail(id),
+ ({ signal }) => getBranch({ id }, signal),
+ { enabled: enabled && typeof id !== 'undefined', ...options }
+ )
diff --git a/studio/data/branches/branch-update-mutation.ts b/studio/data/branches/branch-update-mutation.ts
new file mode 100644
index 00000000000..2b70d82bb48
--- /dev/null
+++ b/studio/data/branches/branch-update-mutation.ts
@@ -0,0 +1,59 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { patch } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { branchKeys } from './keys'
+
+export type BranchUpdateVariables = {
+ id: string
+ projectRef: string
+ branchName?: string
+ gitBranch?: string
+}
+
+export async function updateBranch({ id, branchName, gitBranch }: BranchUpdateVariables) {
+ const { data, error } = await patch('/v1/branches/{branch_id}', {
+ params: {
+ path: { branch_id: id },
+ },
+ body: {
+ branch_name: branchName,
+ git_branch: gitBranch,
+ },
+ })
+
+ if (error) throw error
+ return data
+}
+
+type BranchUpdateData = Awaited<ReturnType<typeof updateBranch>>
+
+export const useBranchUpdateMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<BranchUpdateData, ResponseError, BranchUpdateVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+ return useMutation<BranchUpdateData, ResponseError, BranchUpdateVariables>(
+ (vars) => updateBranch(vars),
+ {
+ async onSuccess(data, variables, context) {
+ const { projectRef } = variables
+ await queryClient.invalidateQueries(branchKeys.list(projectRef))
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to update branch: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}
diff --git a/studio/data/branches/branches-query.ts b/studio/data/branches/branches-query.ts
index 23ac1bcfa63..d7ed5e31e66 100644
--- a/studio/data/branches/branches-query.ts
+++ b/studio/data/branches/branches-query.ts
@@ -4,12 +4,12 @@ import { branchKeys } from './keys'
import { ResponseError } from 'types'
export type BranchesVariables = {
- projectRef: string
+ projectRef?: string
}
export async function getBranches({ projectRef }: BranchesVariables, signal?: AbortSignal) {
const { data, error } = await get(`/v1/projects/{ref}/branches`, {
- params: { path: { ref: projectRef } },
+ params: { path: { ref: projectRef ?? '' } },
signal,
})
@@ -20,7 +20,7 @@ export async function getBranches({ projectRef }: BranchesVariables, signal?: Ab
export type BranchesData = Awaited<ReturnType<typeof getBranches>>
export type BranchesError = ResponseError
-export const useResourcesQuery = <TData = BranchesData>(
+export const useBranchesQuery = <TData = BranchesData>(
{ projectRef }: BranchesVariables,
{ enabled = true, ...options }: UseQueryOptions<BranchesData, BranchesError, TData> = {}
) =>
diff --git a/studio/data/branches/keys.ts b/studio/data/branches/keys.ts
index 8826a7a812d..76180f83f72 100644
--- a/studio/data/branches/keys.ts
+++ b/studio/data/branches/keys.ts
@@ -1,3 +1,4 @@
export const branchKeys = {
list: (projectRef: string | undefined) => ['projects', projectRef, 'branches'] as const,
+ detail: (id: string | undefined) => ['branches', id] as const,
}