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.

fix(web): sync task label mutations into tasks cache

Tinfb504d3c99d0
diff --git a/apps/web/src/components/task/task-labels-popover.tsx b/apps/web/src/components/task/task-labels-popover.tsx
index 10867befe..816d368f5 100644
--- a/apps/web/src/components/task/task-labels-popover.tsx
+++ b/apps/web/src/components/task/task-labels-popover.tsx
@@ -145,12 +145,8 @@ export default function TaskLabelsPopover({
toast.success(t("tasks:popover.labels.addSuccess"));
}
- // Invalidate all relevant queries
await queryClient.invalidateQueries({
- queryKey: ["labels", task.id],
- });
- await queryClient.invalidateQueries({
- queryKey: ["labels", workspaceId],
+ queryKey: ["tasks", task.projectId],
});
} catch (error) {
toast.error(
@@ -188,12 +184,8 @@ export default function TaskLabelsPopover({
workspaceId,
});
- // Invalidate all relevant queries
- await queryClient.invalidateQueries({
- queryKey: ["labels", task.id],
- });
await queryClient.invalidateQueries({
- queryKey: ["labels", workspaceId],
+ queryKey: ["tasks", task.projectId],
});
toast.success(t("tasks:popover.labels.createSuccess"));
diff --git a/apps/web/src/hooks/mutations/label/sync-task-labels-cache.test.ts b/apps/web/src/hooks/mutations/label/sync-task-labels-cache.test.ts
new file mode 100644
index 000000000..47e3cb705
--- /dev/null
+++ b/apps/web/src/hooks/mutations/label/sync-task-labels-cache.test.ts
@@ -0,0 +1,177 @@
+import { describe, expect, it } from "vitest";
+import { updateTaskLabelsInProject } from "./sync-task-labels-cache";
+
+describe("updateTaskLabelsInProject", () => {
+ it("adds a label to the matching task without changing other tasks", () => {
+ const project = {
+ id: "project-1",
+ name: "Project",
+ slug: "PROJ",
+ icon: null,
+ description: null,
+ isPublic: false,
+ createdAt: "2026-04-16T00:00:00.000Z",
+ updatedAt: "2026-04-16T00:00:00.000Z",
+ workspaceId: "workspace-1",
+ columns: [
+ {
+ id: "todo",
+ name: "Todo",
+ isFinal: false,
+ tasks: [
+ {
+ id: "task-1",
+ title: "Task 1",
+ number: 1,
+ description: null,
+ status: "todo",
+ priority: null,
+ startDate: null,
+ dueDate: null,
+ position: 0,
+ createdAt: "2026-04-16T00:00:00.000Z",
+ updatedAt: "2026-04-16T00:00:00.000Z",
+ userId: null,
+ assigneeId: null,
+ assigneeName: null,
+ assigneeImage: null,
+ projectId: "project-1",
+ labels: [],
+ externalLinks: [],
+ },
+ {
+ id: "task-2",
+ title: "Task 2",
+ number: 2,
+ description: null,
+ status: "todo",
+ priority: null,
+ startDate: null,
+ dueDate: null,
+ position: 1,
+ createdAt: "2026-04-16T00:00:00.000Z",
+ updatedAt: "2026-04-16T00:00:00.000Z",
+ userId: null,
+ assigneeId: null,
+ assigneeName: null,
+ assigneeImage: null,
+ projectId: "project-1",
+ labels: [],
+ externalLinks: [],
+ },
+ ],
+ },
+ ],
+ plannedTasks: [],
+ archivedTasks: [],
+ };
+
+ const updatedProject = updateTaskLabelsInProject(
+ project,
+ "task-1",
+ (labels) => [
+ ...labels,
+ {
+ id: "label-bug",
+ name: "bug",
+ color: "red",
+ },
+ ],
+ );
+
+ expect(updatedProject.columns[0]?.tasks[0]?.labels).toEqual([
+ {
+ id: "label-bug",
+ name: "bug",
+ color: "red",
+ },
+ ]);
+ expect(updatedProject.columns[0]?.tasks[1]?.labels).toEqual([]);
+ });
+
+ it("removes a label from planned and archived task collections too", () => {
+ const project = {
+ id: "project-1",
+ name: "Project",
+ slug: "PROJ",
+ icon: null,
+ description: null,
+ isPublic: false,
+ createdAt: "2026-04-16T00:00:00.000Z",
+ updatedAt: "2026-04-16T00:00:00.000Z",
+ workspaceId: "workspace-1",
+ columns: [],
+ plannedTasks: [
+ {
+ id: "task-3",
+ title: "Planned task",
+ number: 3,
+ description: null,
+ status: "planned",
+ priority: null,
+ startDate: null,
+ dueDate: null,
+ position: 0,
+ createdAt: "2026-04-16T00:00:00.000Z",
+ updatedAt: "2026-04-16T00:00:00.000Z",
+ userId: null,
+ assigneeId: null,
+ assigneeName: null,
+ assigneeImage: null,
+ projectId: "project-1",
+ labels: [
+ {
+ id: "label-bug",
+ name: "bug",
+ color: "red",
+ },
+ ],
+ externalLinks: [],
+ },
+ ],
+ archivedTasks: [
+ {
+ id: "task-4",
+ title: "Archived task",
+ number: 4,
+ description: null,
+ status: "archived",
+ priority: null,
+ startDate: null,
+ dueDate: null,
+ position: 0,
+ createdAt: "2026-04-16T00:00:00.000Z",
+ updatedAt: "2026-04-16T00:00:00.000Z",
+ userId: null,
+ assigneeId: null,
+ assigneeName: null,
+ assigneeImage: null,
+ projectId: "project-1",
+ labels: [
+ {
+ id: "label-bug",
+ name: "bug",
+ color: "red",
+ },
+ ],
+ externalLinks: [],
+ },
+ ],
+ };
+
+ const updatedProject = updateTaskLabelsInProject(
+ project,
+ "task-3",
+ (labels) => labels.filter((label) => label.id !== "label-bug"),
+ );
+
+ expect(updatedProject.plannedTasks[0]?.labels).toEqual([]);
+ expect(updatedProject.archivedTasks[0]?.labels).toEqual([
+ {
+ id: "label-bug",
+ name: "bug",
+ color: "red",
+ },
+ ]);
+ });
+});
diff --git a/apps/web/src/hooks/mutations/label/sync-task-labels-cache.ts b/apps/web/src/hooks/mutations/label/sync-task-labels-cache.ts
new file mode 100644
index 000000000..4d88bb986
--- /dev/null
+++ b/apps/web/src/hooks/mutations/label/sync-task-labels-cache.ts
@@ -0,0 +1,83 @@
+import type { QueryClient } from "@tanstack/react-query";
+import type { ProjectWithTasks } from "@/types/project";
+import type Task from "@/types/task";
+
+type TaskLabel = NonNullable<Task["labels"]>[number];
+type TaskLabelsUpdater = (labels: TaskLabel[]) => TaskLabel[];
+
+function updateTaskLabels(
+ task: Task,
+ taskId: string,
+ updater: TaskLabelsUpdater,
+): Task {
+ if (task.id !== taskId) {
+ return task;
+ }
+
+ return {
+ ...task,
+ labels: updater(task.labels ?? []),
+ };
+}
+
+export function updateTaskLabelsInProject(
+ project: ProjectWithTasks,
+ taskId: string,
+ updater: TaskLabelsUpdater,
+): ProjectWithTasks {
+ return {
+ ...project,
+ columns: project.columns.map((column) => ({
+ ...column,
+ tasks: column.tasks.map((task) =>
+ updateTaskLabels(task, taskId, updater),
+ ),
+ })),
+ plannedTasks: project.plannedTasks.map((task) =>
+ updateTaskLabels(task, taskId, updater),
+ ),
+ archivedTasks: project.archivedTasks.map((task) =>
+ updateTaskLabels(task, taskId, updater),
+ ),
+ };
+}
+
+export function syncTaskLabelsInTasksCache(
+ queryClient: QueryClient,
+ taskId: string,
+ updater: TaskLabelsUpdater,
+) {
+ queryClient.setQueriesData<ProjectWithTasks | undefined>(
+ {
+ queryKey: ["tasks"],
+ },
+ (existingProject) =>
+ existingProject
+ ? updateTaskLabelsInProject(existingProject, taskId, updater)
+ : existingProject,
+ );
+}
+
+export function addLabelToTaskInTasksCache(
+ queryClient: QueryClient,
+ taskId: string,
+ label: TaskLabel,
+) {
+ syncTaskLabelsInTasksCache(queryClient, taskId, (existingLabels) => {
+ const alreadyExists = existingLabels.some(
+ (existingLabel) => existingLabel.id === label.id,
+ );
+
+ return alreadyExists ? existingLabels : [...existingLabels, label];
+ });
+}
+
+export function removeLabelFromTaskInTasksCache(
+ queryClient: QueryClient,
+ taskId: string,
+ labelId: string,
+) {
+ syncTaskLabelsInTasksCache(queryClient, taskId, (existingLabels) =>
+ existingLabels.filter((label) => label.id !== labelId),
+ );
+}
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 a43efca68..8849932c9 100644
--- a/apps/web/src/hooks/mutations/label/use-create-label.ts
+++ b/apps/web/src/hooks/mutations/label/use-create-label.ts
@@ -1,6 +1,7 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import type { CreateLabelRequest } from "@/fetchers/label/create-label";
import createLabel from "@/fetchers/label/create-label";
+import { addLabelToTaskInTasksCache } from "./sync-task-labels-cache";
function useCreateLabel() {
const queryClient = useQueryClient();
@@ -38,6 +39,12 @@ function useCreateLabel() {
: [...existingLabels, createdLabel];
},
);
+
+ addLabelToTaskInTasksCache(queryClient, createdLabel.taskId, {
+ id: createdLabel.id,
+ name: createdLabel.name,
+ color: createdLabel.color,
+ });
}
void queryClient.invalidateQueries({
diff --git a/apps/web/src/hooks/mutations/label/use-delete-label.ts b/apps/web/src/hooks/mutations/label/use-delete-label.ts
index 78424fed9..10259cd73 100644
--- a/apps/web/src/hooks/mutations/label/use-delete-label.ts
+++ b/apps/web/src/hooks/mutations/label/use-delete-label.ts
@@ -1,9 +1,44 @@
-import { useMutation } from "@tanstack/react-query";
+import { useMutation, useQueryClient } from "@tanstack/react-query";
import deleteLabel from "@/fetchers/label/delete-label";
+import { removeLabelFromTaskInTasksCache } from "./sync-task-labels-cache";
function useDeleteLabel() {
+ const queryClient = useQueryClient();
+
return useMutation({
mutationFn: deleteLabel,
+ onSuccess: (deletedLabel) => {
+ queryClient.setQueryData(
+ ["labels", deletedLabel.workspaceId],
+ (existingLabels: Array<typeof deletedLabel> | undefined) =>
+ existingLabels?.filter((label) => label.id !== deletedLabel.id) ?? [],
+ );
+
+ if (deletedLabel.taskId) {
+ queryClient.setQueryData(
+ ["labels", deletedLabel.taskId],
+ (existingLabels: Array<typeof deletedLabel> | undefined) =>
+ existingLabels?.filter((label) => label.id !== deletedLabel.id) ??
+ [],
+ );
+
+ removeLabelFromTaskInTasksCache(
+ queryClient,
+ deletedLabel.taskId,
+ deletedLabel.id,
+ );
+ }
+
+ void queryClient.invalidateQueries({
+ queryKey: ["labels", deletedLabel.workspaceId],
+ });
+
+ if (deletedLabel.taskId) {
+ void queryClient.invalidateQueries({
+ queryKey: ["labels", deletedLabel.taskId],
+ });
+ }
+ },
});
}