Skip to content

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

Go
2.1 KB
Raw Blame
"use client";

import { useQuery } from "@tanstack/react-query";
import { toast } from "sonner";
import { useWorkspaceId } from "@multica/core/hooks";
import {
  issueDetailOptions,
  childIssuesOptions,
} from "@multica/core/issues/queries";
import { useUpdateIssue } from "@multica/core/issues/mutations";
import { IssuePickerModal } from "./issue-picker-modal";
import { useT } from "../i18n";

export function AddChildIssueModal({
  open = true,
  onClose,
  onOpenChangeComplete,
  data,
}: {
  open?: boolean;
  onClose: () => void;
  onOpenChangeComplete?: (open: boolean) => void;
  data: Record<string, unknown> | null;
}) {
  const { t } = useT("modals");
  const issueId = (data?.issueId as string) || "";
  const wsId = useWorkspaceId();
  const { mutateAsync: updateIssue } = useUpdateIssue();

  const { data: issue = null } = useQuery({
    ...issueDetailOptions(wsId, issueId),
    enabled: !!issueId,
  });
  const { data: children = [] } = useQuery({
    ...childIssuesOptions(wsId, issueId),
    enabled: !!issueId,
  });

  const excludeIds = [
    issueId,
    ...(issue?.parent_issue_id ? [issue.parent_issue_id] : []),
    ...children.map((c) => c.id),
  ];

  // The picker closes as soon as an issue is picked, unmounting this modal, so
  // the toasts follow the request's promise rather than per-call callbacks.
  return (
    <IssuePickerModal
      open={open}
      onOpenChange={(v) => {
        if (!v) onClose();
      }}
      onOpenChangeComplete={onOpenChangeComplete}
      title={t(($) => $.add_child.title)}
      description={t(($) => $.add_child.description)}
      excludeIds={excludeIds}
      onSelect={(selected) => {
        updateIssue({
          id: selected.id,
          parent_issue_id: issueId,
        }).then(
          () =>
            toast.success(
              t(($) => $.add_child.toast_success, {
                identifier: selected.identifier,
              }),
            ),
          (err) =>
            toast.error(
              err instanceof Error && err.message
                ? err.message
                : t(($) => $.add_child.toast_failed),
            ),
        );
      }}
    />
  );
}