fix: space deletion not switching to Nova Spaces and new space not appearing in dropdown (#747)

### fix: space selector state bugs on create and delete

Two related bugs in the space selector caused by query cache key mismatches in `use-project-mutations.ts`:

1. New spaces not appearing in dropdown until refresh — wrong query cache was being invalidated after creation.
 2. App stuck on deleted space instead of switching to Nova Spaces — delete handler was reading from an empty cache, so the selection never updated.
This commit is contained in:
Prasanna721 2026-02-19 06:54:32 +00:00
parent cda3bca0d0
commit cc2dcaa306

View file

@ -4,7 +4,7 @@ import { $fetch } from "@lib/api"
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { toast } from "sonner"
import { useProject } from "@/stores"
import type { Project } from "@repo/lib/types"
import type { ContainerTagListType } from "@repo/lib/types"
export function useProjectMutations() {
const queryClient = useQueryClient()
@ -29,6 +29,7 @@ export function useProjectMutations() {
onSuccess: (data) => {
toast.success("Project created successfully!")
queryClient.invalidateQueries({ queryKey: ["projects"] })
queryClient.invalidateQueries({ queryKey: ["container-tags"] })
if (data?.containerTag) {
setSelectedProjects([data.containerTag])
@ -63,14 +64,12 @@ export function useProjectMutations() {
},
onSuccess: (_, variables) => {
toast.success("Project deleted successfully")
queryClient.invalidateQueries({ queryKey: ["projects"] })
queryClient.invalidateQueries({ queryKey: ["container-tags"] })
const allProjects =
queryClient.getQueryData<Project[]>(["projects"]) || []
const deletedProject = allProjects.find(
(p) => p.id === variables.projectId,
)
const allTags =
queryClient.getQueryData<ContainerTagListType[]>(["container-tags"]) ||
[]
const deletedProject = allTags.find((p) => p.id === variables.projectId)
if (
deletedProject?.containerTag &&
selectedProjects.includes(deletedProject.containerTag)
@ -79,6 +78,9 @@ export function useProjectMutations() {
selectedProjects.filter((tag) => tag !== deletedProject.containerTag),
)
}
queryClient.invalidateQueries({ queryKey: ["projects"] })
queryClient.invalidateQueries({ queryKey: ["container-tags"] })
},
onError: (error) => {
toast.error("Failed to delete project", {