Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

Refresh label caches after creating a label

Tinc72eb6f22768
diff --git a/apps/web/src/hooks/mutations/label/use-create-label.ts b/apps/web/src/hooks/mutations/label/use-create-label.ts
index 2a31c27f8..413561ecb 100644
--- a/apps/web/src/hooks/mutations/label/use-create-label.ts
+++ b/apps/web/src/hooks/mutations/label/use-create-label.ts
@@ -1,9 +1,55 @@
-import { useMutation } from "@tanstack/react-query";
+import { useMutation, useQueryClient } from "@tanstack/react-query";
import createLabel from "@/fetchers/label/create-label";
+import type { CreateLabelRequest } from "@/fetchers/label/create-label";
function useCreateLabel() {
+ const queryClient = useQueryClient();
+
return useMutation({
mutationFn: createLabel,
+ onSuccess: (createdLabel, variables: CreateLabelRequest) => {
+ queryClient.setQueryData(
+ ["labels", variables.workspaceId],
+ (existingLabels: Array<typeof createdLabel> | undefined) => {
+ if (!existingLabels) return [createdLabel];
+
+ const alreadyExists = existingLabels.some(
+ (label) => label.id === createdLabel.id,
+ );
+
+ return alreadyExists
+ ? existingLabels
+ : [...existingLabels, createdLabel];
+ },
+ );
+
+ if (createdLabel.taskId) {
+ queryClient.setQueryData(
+ ["labels", createdLabel.taskId],
+ (existingLabels: Array<typeof createdLabel> | undefined) => {
+ if (!existingLabels) return [createdLabel];
+
+ const alreadyExists = existingLabels.some(
+ (label) => label.id === createdLabel.id,
+ );
+
+ return alreadyExists
+ ? existingLabels
+ : [...existingLabels, createdLabel];
+ },
+ );
+ }
+
+ void queryClient.invalidateQueries({
+ queryKey: ["labels", variables.workspaceId],
+ });
+
+ if (createdLabel.taskId) {
+ void queryClient.invalidateQueries({
+ queryKey: ["labels", createdLabel.taskId],
+ });
+ }
+ },
});
}