2024-01-24 13:51:59 +00:00
|
|
|
import React from "react";
|
2024-01-10 14:39:45 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
2024-03-06 13:09:14 +00:00
|
|
|
import Link from "next/link";
|
2024-01-24 13:51:59 +00:00
|
|
|
import { Pencil, X } from "lucide-react";
|
2024-01-10 14:39:45 +00:00
|
|
|
// hooks
|
|
|
|
// components
|
2024-04-03 12:34:35 +00:00
|
|
|
import { TOAST_TYPE, Tooltip, setToast } from "@plane/ui";
|
2024-03-19 14:38:35 +00:00
|
|
|
import { ParentIssuesListModal } from "@/components/issues";
|
2024-01-24 13:51:59 +00:00
|
|
|
// ui
|
|
|
|
// helpers
|
2024-03-19 14:38:35 +00:00
|
|
|
import { cn } from "@/helpers/common.helper";
|
|
|
|
import { useIssueDetail, useProject } from "@/hooks/store";
|
|
|
|
import { usePlatformOS } from "@/hooks/use-platform-os";
|
2024-01-24 13:51:59 +00:00
|
|
|
// types
|
2024-01-10 14:39:45 +00:00
|
|
|
import { TIssueOperations } from "./root";
|
|
|
|
|
|
|
|
type TIssueParentSelect = {
|
2024-01-24 13:51:59 +00:00
|
|
|
className?: string;
|
|
|
|
disabled?: boolean;
|
2024-01-10 14:39:45 +00:00
|
|
|
issueId: string;
|
|
|
|
issueOperations: TIssueOperations;
|
2024-01-24 13:51:59 +00:00
|
|
|
projectId: string;
|
|
|
|
workspaceSlug: string;
|
2024-01-10 14:39:45 +00:00
|
|
|
};
|
|
|
|
|
2024-01-24 13:51:59 +00:00
|
|
|
export const IssueParentSelect: React.FC<TIssueParentSelect> = observer((props) => {
|
|
|
|
const { className = "", disabled = false, issueId, issueOperations, projectId, workspaceSlug } = props;
|
|
|
|
// store hooks
|
|
|
|
const { getProjectById } = useProject();
|
|
|
|
const {
|
|
|
|
issue: { getIssueById },
|
|
|
|
} = useIssueDetail();
|
2024-04-03 12:34:35 +00:00
|
|
|
const {
|
|
|
|
isParentIssueModalOpen,
|
|
|
|
toggleParentIssueModal,
|
|
|
|
removeSubIssue,
|
|
|
|
subIssues: { setSubIssueHelpers },
|
|
|
|
} = useIssueDetail();
|
|
|
|
|
2024-01-24 13:51:59 +00:00
|
|
|
// derived values
|
|
|
|
const issue = getIssueById(issueId);
|
|
|
|
const parentIssue = issue?.parent_id ? getIssueById(issue.parent_id) : undefined;
|
|
|
|
const parentIssueProjectDetails =
|
|
|
|
parentIssue && parentIssue.project_id ? getProjectById(parentIssue.project_id) : undefined;
|
2024-03-12 15:09:36 +00:00
|
|
|
const { isMobile } = usePlatformOS();
|
2024-01-24 13:51:59 +00:00
|
|
|
const handleParentIssue = async (_issueId: string | null = null) => {
|
|
|
|
try {
|
|
|
|
await issueOperations.update(workspaceSlug, projectId, issueId, { parent_id: _issueId });
|
|
|
|
await issueOperations.fetch(workspaceSlug, projectId, issueId);
|
|
|
|
toggleParentIssueModal(false);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("something went wrong while fetching the issue");
|
|
|
|
}
|
|
|
|
};
|
2024-01-10 14:39:45 +00:00
|
|
|
|
2024-04-03 12:34:35 +00:00
|
|
|
const handleRemoveSubIssue = async (
|
|
|
|
workspaceSlug: string,
|
|
|
|
projectId: string,
|
|
|
|
parentIssueId: string,
|
|
|
|
issueId: string
|
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
setSubIssueHelpers(parentIssueId, "issue_loader", issueId);
|
|
|
|
await removeSubIssue(workspaceSlug, projectId, parentIssueId, issueId);
|
|
|
|
setSubIssueHelpers(parentIssueId, "issue_loader", issueId);
|
|
|
|
} catch (error) {
|
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
|
|
|
title: "Error",
|
|
|
|
message: "Something went wrong",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-24 13:51:59 +00:00
|
|
|
if (!issue) return <></>;
|
2024-01-10 14:39:45 +00:00
|
|
|
|
2024-01-24 13:51:59 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ParentIssuesListModal
|
|
|
|
projectId={projectId}
|
|
|
|
issueId={issueId}
|
|
|
|
isOpen={isParentIssueModalOpen}
|
|
|
|
handleClose={() => toggleParentIssueModal(false)}
|
|
|
|
onChange={(issue: any) => handleParentIssue(issue?.id)}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className={cn(
|
|
|
|
"group flex items-center justify-between gap-2 px-2 py-0.5 rounded outline-none",
|
|
|
|
{
|
|
|
|
"cursor-not-allowed": disabled,
|
|
|
|
"hover:bg-custom-background-80": !disabled,
|
2024-01-29 15:06:14 +00:00
|
|
|
"bg-custom-background-80": isParentIssueModalOpen,
|
2024-01-24 13:51:59 +00:00
|
|
|
},
|
|
|
|
className
|
|
|
|
)}
|
|
|
|
onClick={() => toggleParentIssueModal(true)}
|
|
|
|
disabled={disabled}
|
|
|
|
>
|
|
|
|
{issue.parent_id && parentIssue ? (
|
|
|
|
<div className="flex items-center gap-1 bg-green-500/20 text-green-700 rounded px-1.5 py-1">
|
2024-03-12 15:09:36 +00:00
|
|
|
<Tooltip tooltipHeading="Title" tooltipContent={parentIssue.name} isMobile={isMobile}>
|
2024-01-29 15:06:14 +00:00
|
|
|
<Link
|
|
|
|
href={`/${workspaceSlug}/projects/${projectId}/issues/${parentIssue?.id}`}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
2024-01-31 10:06:55 +00:00
|
|
|
className="text-xs font-medium"
|
2024-01-29 15:06:14 +00:00
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
>
|
|
|
|
{parentIssueProjectDetails?.identifier}-{parentIssue.sequence_id}
|
|
|
|
</Link>
|
|
|
|
</Tooltip>
|
2024-01-10 14:39:45 +00:00
|
|
|
|
2024-01-24 13:51:59 +00:00
|
|
|
{!disabled && (
|
2024-03-12 15:09:36 +00:00
|
|
|
<Tooltip tooltipContent="Remove" position="bottom" isMobile={isMobile}>
|
2024-01-24 13:51:59 +00:00
|
|
|
<span
|
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2024-04-03 12:34:35 +00:00
|
|
|
handleRemoveSubIssue(workspaceSlug, projectId, parentIssue.id, issueId);
|
2024-01-24 13:51:59 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<X className="h-2.5 w-2.5 text-custom-text-300 hover:text-red-500" />
|
|
|
|
</span>
|
|
|
|
</Tooltip>
|
2024-01-10 14:39:45 +00:00
|
|
|
)}
|
|
|
|
</div>
|
2024-01-24 13:51:59 +00:00
|
|
|
) : (
|
|
|
|
<span className="text-sm text-custom-text-400">Add parent issue</span>
|
|
|
|
)}
|
2024-01-29 15:06:14 +00:00
|
|
|
{!disabled && (
|
|
|
|
<span
|
|
|
|
className={cn("p-1 flex-shrink-0 opacity-0 group-hover:opacity-100", {
|
|
|
|
"text-custom-text-400": !issue.parent_id && !parentIssue,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<Pencil className="h-2.5 w-2.5 flex-shrink-0" />
|
|
|
|
</span>
|
|
|
|
)}
|
2024-01-24 13:51:59 +00:00
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|