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.

1.9 KB
Raw Blame
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { FolderInputIcon } from "lucide-react";
import { invalidateMyWork } from "@/lib/invalidate-my-work";
import { useTranslation } from "react-i18next";
import moveTask from "@/fetchers/task/move-task";
import { toast } from "@/lib/toast";

export function useMoveTask() {
  const { t } = useTranslation();
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: moveTask,
    onSuccess: (result, variables) => {
      invalidateMyWork(queryClient);
      toast.success(t("tasks:move.success"), { icon: FolderInputIcon });
      queryClient.invalidateQueries({
        queryKey: ["external-links", variables.taskId],
      });
      queryClient.invalidateQueries({
        queryKey: ["task", variables.taskId],
      });
      queryClient.invalidateQueries({
        queryKey: ["tasks", result.sourceProjectId],
      });
      queryClient.invalidateQueries({
        queryKey: ["tasks", result.destinationProjectId],
      });
      // The list view reads relations per project, and moving a task changes
      // which edges are internal to each side. The websocket broadcast skips
      // the window that started the move, so this is the only path that
      // refreshes it for whoever performed it.
      queryClient.invalidateQueries({
        queryKey: ["task-relations", "project", result.sourceProjectId],
      });
      queryClient.invalidateQueries({
        queryKey: ["task-relations", "project", result.destinationProjectId],
      });
      queryClient.invalidateQueries({
        queryKey: ["projects"],
      });
      queryClient.invalidateQueries({
        queryKey: ["activities", variables.taskId],
      });
      queryClient.invalidateQueries({
        queryKey: ["notifications"],
      });
    },
    onError: (error) => {
      toast.error(
        error instanceof Error ? error.message : t("tasks:move.error"),
      );
    },
  });
}