Skip to content

Commit 4caed01

Browse files
authored
fix: Fix a timing issue when deleting a branch (supabase#30736)
* Add a timeout for invalidating the branch list queries. Add an optimistic deletion from the query cache when deleting a branch. * Don't refetch the branches list if one of the branches is faulty. The branch will be updated via useBranchQuery.
1 parent 9e5cb99 commit 4caed01

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

apps/studio/components/interfaces/BranchManagement/BranchManagement.tsx

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,7 @@ const BranchManagement = () => {
7878
isLoading: isLoadingBranches,
7979
isError: isErrorBranches,
8080
isSuccess: isSuccessBranches,
81-
} = useBranchesQuery(
82-
{ projectRef },
83-
{
84-
refetchInterval(data) {
85-
if (
86-
data?.some(
87-
(branch) =>
88-
branch.status === 'CREATING_PROJECT' ||
89-
branch.status === 'RUNNING_MIGRATIONS' ||
90-
branch.status === 'MIGRATIONS_FAILED'
91-
)
92-
) {
93-
return 1000 * 3 // 3 seconds
94-
}
95-
96-
return false
97-
},
98-
}
99-
)
81+
} = useBranchesQuery({ projectRef })
10082
const [[mainBranch], previewBranchesUnsorted] = partition(branches, (branch) => branch.is_default)
10183
const previewBranches = previewBranchesUnsorted.sort((a, b) =>
10284
new Date(a.updated_at) < new Date(b.updated_at) ? 1 : -1

apps/studio/data/branches/branch-delete-mutation.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { toast } from 'sonner'
33

44
import { del, handleError } from 'data/fetchers'
55
import type { ResponseError } from 'types'
6+
import { BranchesData } from './branches-query'
67
import { branchKeys } from './keys'
78

89
export type BranchDeleteVariables = {
@@ -35,7 +36,18 @@ export const useBranchDeleteMutation = ({
3536
{
3637
async onSuccess(data, variables, context) {
3738
const { projectRef } = variables
38-
await queryClient.invalidateQueries(branchKeys.list(projectRef))
39+
setTimeout(() => {
40+
queryClient.invalidateQueries(branchKeys.list(projectRef))
41+
}, 5000)
42+
43+
const branches: BranchesData | undefined = queryClient.getQueryData(
44+
branchKeys.list(projectRef)
45+
)
46+
if (branches) {
47+
const updatedBranches = branches.filter((branch) => branch.id !== variables.id)
48+
queryClient.setQueryData(branchKeys.list(projectRef), updatedBranches)
49+
}
50+
3951
await onSuccess?.(data, variables, context)
4052
},
4153
async onError(data, variables, context) {

0 commit comments

Comments
 (0)