Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
13.6 KB
Raw Blame
"use client";

import { subIssueCreateDefaults } from "@multica/core/issues";
import { useCallback } from "react";
import { useQuery } from "@tanstack/react-query";
import { toast } from "sonner";
import type { Issue, UpdateIssueRequest } from "@multica/core/types";
import { useAuthStore } from "@multica/core/auth";
import { useWorkspaceId } from "@multica/core/hooks";
import { useWorkspacePaths, issueRouteRef } from "@multica/core/paths";
import { useModalStore } from "@multica/core/modals";
import { useUpdateIssue } from "@multica/core/issues/mutations";
import { useIssueStatuses } from "@multica/core/issue-statuses/hooks";
import { errorCode } from "@multica/core/api";
import { pinListOptions, useCreatePin, useDeletePin } from "@multica/core/pins";
import { copyText, copyTextAndHtml } from "@multica/ui/lib/clipboard";
import { useNavigation } from "../../navigation";
import { useT } from "../../i18n";
import { titleLinkPayload } from "../../common/title-link";
import { runConfirmIntent } from "./run-confirm-gate";
import { useIssueSurfaceActionsOptional } from "../surface/actions-context";
import type { IssueSurfaceMutationOptions } from "../surface/actions-context";

export interface UseIssueActionsResult {
  isPinned: boolean;
  updateField: (
    updates: Partial<UpdateIssueRequest>,
    options?: IssueSurfaceMutationOptions,
  ) => void;
  openInNewTab: () => void;
  openInDesktop?: () => void;
  openInNewWindow?: () => void;
  togglePin: () => void;
  copyLink: () => Promise<void>;
  copyTitleAsLink: () => Promise<void>;
  copyCommentLink: (commentId: string) => Promise<void>;
  openCreateSubIssue: () => void;
  openRename: () => void;
  openSetParent: () => void;
  removeParent: () => void;
  openAddChild: () => void;
  openMarkDuplicate: () => void;
  openAddBlocker: () => void;
  openAddBlocking: () => void;
  openDeleteConfirm: (opts?: { onDeletedFallbackPath?: string }) => void;
}

/**
 * Accepts a nullable issue so callers can invoke the hook before they've
 * early-returned on a missing issue. Returned handlers are safe no-ops when
 * `issue` is null.
 */
export function useIssueActions(issue: Issue | null): UseIssueActionsResult {
  const { t } = useT("issues");
  const wsId = useWorkspaceId();
  const paths = useWorkspacePaths();
  const navigation = useNavigation();
  const user = useAuthStore((s) => s.user);
  const userId = user?.id;

  const { data: pinnedItems = [] } = useQuery({
    ...pinListOptions(wsId, userId ?? ""),
    enabled: !!userId,
  });

  const isPinned =
    !!issue &&
    pinnedItems.some(
      (p) => p.item_type === "issue" && p.item_id === issue.id,
    );

  const updateIssue = useUpdateIssue();
  const surfaceActions = useIssueSurfaceActionsOptional();
  const createPin = useCreatePin();
  const deletePin = useDeletePin();
  const openModal = useModalStore((s) => s.open);

  const issueId = issue?.id ?? null;
  const issueIdentifier = issue?.identifier ?? null;
  const issueTitle = issue?.title ?? "";
  const issueProjectId = issue?.project_id ?? null;
  const issueAssigneeType = issue?.assignee_type ?? null;
  const issueAssigneeId = issue?.assignee_id ?? null;
  const { entryOf } = useIssueStatuses(wsId);
  const updateField = useCallback(
    (
      updates: Partial<UpdateIssueRequest>,
      options?: IssueSurfaceMutationOptions,
    ) => {
      if (!issueId) return;
      // The two writes that can hand work to an agent — giving it an owner, and
      // promoting it out of the parking lot — confirm first, through the shared
      // gate every single-issue entry point routes on (runConfirmIntent). The
      // modal applies the change itself; everything else applies directly.
      //
      // Not wired into drag-and-drop or the batch toolbar, which keep applying
      // directly. That is the existing split, not a new one: a drop is direct
      // manipulation whose card has already moved, and batch status was made
      // deliberately dialog-free in MUL-4155.
      const intent = issue && runConfirmIntent(issue, updates, { entryOf });
      if (intent) {
        openModal("issue-run-confirm", intent);
        return;
      }
      if (surfaceActions) {
        surfaceActions.updateIssue(issueId, updates, {
          errorMessage: t(($) => $.detail.update_failed),
          ...options,
        });
      } else {
        // Keep UI callbacks observer-scoped; error feedback belongs to the request.
        void updateIssue
          .mutateAsync(
            { id: issueId, ...updates },
            {
              onSuccess: (issue) => options?.onSuccess?.(issue),
              onError: (err) => options?.onError?.(err),
              onSettled: () => options?.onSettled?.(),
            },
          )
          .catch((err: unknown) => {
            toast.error(
              errorCode(err) === "revision_conflict"
                ? t(($) => $.revision.conflict)
                : err instanceof Error && err.message
                ? err.message
                : t(($) => $.detail.update_failed),
            );
          });
      }
    },
    [issue, issueId, entryOf, surfaceActions, updateIssue, openModal, t],
  );

  // Explicit "open it somewhere else" CTA, so the new tab takes focus
  // (`activate: true`) — the user is asking to move into the new context, not
  // to stash it for later the way modifier-click does. Same contract as the
  // table row open and the attachment preview's "Open in new tab".
  //
  // Only desktop implements `openInNewTab`; on web it is undefined and we fall
  // back to a real browser tab via the shareable URL.
  const openInNewTab = useCallback(() => {
    if (!issueId) return;
    // Identifier form, same as copyLink: on web this becomes a real browser
    // tab at the shareable URL, so it is a link the user sees and may copy out
    // of the address bar. Opening on the UUID would also make the route
    // immediately rewrite the fresh tab's URL.
    const path = paths.issueDetail(issueRouteRef({ id: issueId, identifier: issueIdentifier }));
    if (navigation.openInNewTab) {
      navigation.openInNewTab(path, issueIdentifier ?? undefined, {
        activate: true,
      });
      return;
    }
    window.open(
      navigation.getShareableUrl(path),
      "_blank",
      "noopener,noreferrer",
    );
  }, [issueId, issueIdentifier, navigation, paths]);

  const openInNewWindow = useCallback(() => {
    if (!issueId) return;
    navigation.openIssueInNewWindow?.(
      paths.issueDetail(issueRouteRef({ id: issueId, identifier: issueIdentifier })),
      issueIdentifier ? `${issueIdentifier} ${issueTitle}` : issueTitle,
    );
  }, [issueId, issueIdentifier, issueTitle, navigation, paths]);

  const openInDesktop = useCallback(() => {
    if (!issueId) return;
    const path = paths.issueDetail(issueRouteRef({ id: issueId, identifier: issueIdentifier }));
    const isCurrentIssue =
      navigation.pathname === path ||
      navigation.pathname === paths.issueDetail(issueRouteRef({ id: issueId, identifier: null }));
    navigation.openInDesktop?.(path + (isCurrentIssue ? navigation.hash : ""));
  }, [issueId, issueIdentifier, navigation, paths]);

  const togglePin = useCallback(() => {
    if (!issueId) return;
    if (isPinned) {
      deletePin.mutate({ itemType: "issue", itemId: issueId });
    } else {
      createPin.mutate({ item_type: "issue", item_id: issueId });
    }
  }, [isPinned, issueId, createPin, deletePin]);

  const copyLink = useCallback(async () => {
    if (!issueId) return;
    // Share the identifier form (`/{ws}/issues/MUL-123`): a pasted link should
    // say which issue it points at. The UUID form stays valid, so links copied
    // before this still resolve.
    const url = navigation.getShareableUrl(paths.issueDetail(issueRouteRef({ id: issueId, identifier: issueIdentifier })));
    if (await copyText(url)) {
      toast.success(t(($) => $.detail.link_copied));
    } else {
      toast.error(t(($) => $.detail.link_copy_failed));
    }
  }, [paths, issueId, issueIdentifier, navigation, t]);

  const copyTitleAsLink = useCallback(async () => {
    if (!issueId) return;
    const url = navigation.getShareableUrl(paths.issueDetail(issueRouteRef({ id: issueId, identifier: issueIdentifier })));
    // Keep the identifier in front of the title, matching how the issue reads
    // in lists and activity feeds, so the pasted link stays recognizable.
    const label = [issueIdentifier, issueTitle].filter(Boolean).join(" ");
    const { text, html } = titleLinkPayload(label, url);
    if (await copyTextAndHtml(text, html)) {
      toast.success(t(($) => $.detail.title_link_copied));
    } else {
      toast.error(t(($) => $.detail.link_copy_failed));
    }
  }, [paths, issueId, issueIdentifier, issueTitle, navigation, t]);

  // Built during render so `copyCommentLink` depends on this string alone:
  // `paths` is rebuilt on every render, and the handler is passed to every
  // memoized comment card, so depending on it would re-render all of them on
  // any unrelated page update. Identifier form for the same reason as `copyLink`.
  const issueShareUrl = issueId
    ? navigation.getShareableUrl(paths.issueDetail(issueRouteRef({ id: issueId, identifier: issueIdentifier })))
    : null;
  const copyCommentLink = useCallback(async (commentId: string) => {
    if (!issueShareUrl) return;
    // The `#comment-…` fragment is the deep-link anchor `IssueDetailRoute`
    // resolves via `parseCommentHighlightHash`; dropping it would downgrade the
    // link to the whole issue.
    const url = `${issueShareUrl}#comment-${commentId}`;
    if (await copyText(url)) {
      toast.success(t(($) => $.comment.link_copied));
    } else {
      toast.error(t(($) => $.comment.link_copy_failed));
    }
  }, [issueShareUrl, t]);

  const openCreateSubIssue = useCallback(() => {
    if (!issueId) return;
    openModal("create-issue", subIssueCreateDefaults({
      id: issueId,
      identifier: issueIdentifier,
      project_id: issueProjectId,
      assignee_type: issueAssigneeType,
      assignee_id: issueAssigneeId,
    }));
  }, [
    openModal,
    issueId,
    issueIdentifier,
    issueProjectId,
    issueAssigneeType,
    issueAssigneeId,
  ]);

  const openSetParent = useCallback(() => {
    if (!issueId) return;
    openModal("issue-set-parent", { issueId });
  }, [openModal, issueId]);

  // Rename entry point for every surface that renders the actions menu. The
  // issue detail page can also activate its inline title editor by clicking the
  // title, but list, board, gantt and sub-issue rows have no such affordance —
  // one shared modal keeps the write path identical everywhere.
  const openRename = useCallback(() => {
    if (!issueId) return;
    openModal("issue-rename", {
      issueId,
      identifier: issueIdentifier,
      title: issueTitle,
    });
  }, [openModal, issueId, issueIdentifier, issueTitle]);

  const openMarkDuplicate = useCallback(() => {
    if (!issueId) return;
    openModal("issue-mark-duplicate", { issueId });
  }, [openModal, issueId]);

  // Detach from the parent and promote to a standalone issue. Reversible
  // (Set parent re-links it), non-destructive, and mirrors the clear-date
  // actions — so it applies directly instead of a confirm modal. `stage`
  // only orders sub-issues under a parent, so clear it in the same write to
  // avoid an orphaned value on a standalone issue. The success toast fires
  // from onSuccess, not eagerly after mutate() — otherwise a request that
  // fails on permission/network/validation would flash "removed" before the
  // error toast and the optimistic rollback (false confirmation).
  const removeParent = useCallback(() => {
    if (!issueId) return;
    if (surfaceActions) {
      surfaceActions.updateIssue(
        issueId,
        {
          parent_issue_id: null,
          stage: null,
        },
        {
          onSuccess: () =>
            toast.success(t(($) => $.actions.remove_parent_issue_success)),
          errorMessage: t(($) => $.detail.update_failed),
        },
      );
    } else {
      updateIssue.mutate(
        {
          id: issueId,
          parent_issue_id: null,
          stage: null,
        },
        {
          onSuccess: () =>
            toast.success(t(($) => $.actions.remove_parent_issue_success)),
          onError: (err) =>
            toast.error(
              err instanceof Error && err.message
                ? err.message
                : t(($) => $.detail.update_failed),
            ),
        },
      );
    }
  }, [issueId, surfaceActions, updateIssue, t]);

  const openAddChild = useCallback(() => {
    if (!issueId) return;
    openModal("issue-add-child", { issueId });
  }, [openModal, issueId]);

  const openAddBlocker = useCallback(() => {
    if (!issueId) return;
    openModal("issue-add-blocker", { issueId });
  }, [openModal, issueId]);

  const openAddBlocking = useCallback(() => {
    if (!issueId) return;
    openModal("issue-add-blocking", { issueId });
  }, [openModal, issueId]);

  const openDeleteConfirm = useCallback(
    (opts?: { onDeletedFallbackPath?: string }) => {
      if (!issueId) return;
      openModal("issue-delete-confirm", {
        issueId,
        identifier: issueIdentifier,
        onDeletedFallbackPath: opts?.onDeletedFallbackPath,
      });
    },
    [openModal, issueId, issueIdentifier],
  );

  return {
    isPinned,
    updateField,
    openInNewTab,
    openInDesktop: navigation.openInDesktop ? openInDesktop : undefined,
    openInNewWindow: navigation.openIssueInNewWindow ? openInNewWindow : undefined,
    togglePin,
    copyLink,
    copyTitleAsLink,
    copyCommentLink,
    openCreateSubIssue,
    openRename,
    openSetParent,
    removeParent,
    openAddChild,
    openMarkDuplicate,
    openAddBlocker,
    openAddBlocking,
    openDeleteConfirm,
  };
}