2024-01-24 13:51:59 +00:00
|
|
|
import React, { useState } from "react";
|
2024-01-10 14:39:45 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
// hooks
|
2024-01-24 13:51:59 +00:00
|
|
|
import { useIssueDetail } from "hooks/store";
|
|
|
|
// components
|
|
|
|
import { CycleDropdown } from "components/dropdowns";
|
2024-01-10 14:39:45 +00:00
|
|
|
// ui
|
2024-01-24 13:51:59 +00:00
|
|
|
import { Spinner } from "@plane/ui";
|
|
|
|
// helpers
|
|
|
|
import { cn } from "helpers/common.helper";
|
2024-01-10 14:39:45 +00:00
|
|
|
// types
|
|
|
|
import type { TIssueOperations } from "./root";
|
|
|
|
|
|
|
|
type TIssueCycleSelect = {
|
2024-01-24 13:51:59 +00:00
|
|
|
className?: string;
|
2024-01-10 14:39:45 +00:00
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
issueId: string;
|
|
|
|
issueOperations: TIssueOperations;
|
|
|
|
disabled?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const IssueCycleSelect: React.FC<TIssueCycleSelect> = observer((props) => {
|
2024-01-24 13:51:59 +00:00
|
|
|
const { className = "", workspaceSlug, projectId, issueId, issueOperations, disabled = false } = props;
|
|
|
|
// states
|
|
|
|
const [isUpdating, setIsUpdating] = useState(false);
|
|
|
|
// store hooks
|
2024-01-10 14:39:45 +00:00
|
|
|
const {
|
|
|
|
issue: { getIssueById },
|
|
|
|
} = useIssueDetail();
|
2024-01-24 13:51:59 +00:00
|
|
|
// derived values
|
2024-01-10 14:39:45 +00:00
|
|
|
const issue = getIssueById(issueId);
|
|
|
|
const disableSelect = disabled || isUpdating;
|
|
|
|
|
2024-01-24 13:51:59 +00:00
|
|
|
const handleIssueCycleChange = async (cycleId: string | null) => {
|
|
|
|
if (!issue || issue.cycle_id === cycleId) return;
|
2024-01-10 14:39:45 +00:00
|
|
|
setIsUpdating(true);
|
2024-01-24 15:03:54 +00:00
|
|
|
if (cycleId) await issueOperations.addIssueToCycle?.(workspaceSlug, projectId, cycleId, [issueId]);
|
|
|
|
else await issueOperations.removeIssueFromCycle?.(workspaceSlug, projectId, issue.cycle_id ?? "", issueId);
|
2024-01-10 14:39:45 +00:00
|
|
|
setIsUpdating(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-01-24 13:51:59 +00:00
|
|
|
<div className={cn("flex items-center gap-1 h-full", className)}>
|
|
|
|
<CycleDropdown
|
|
|
|
value={issue?.cycle_id ?? null}
|
|
|
|
onChange={handleIssueCycleChange}
|
|
|
|
projectId={projectId}
|
2024-01-10 14:39:45 +00:00
|
|
|
disabled={disableSelect}
|
2024-01-24 13:51:59 +00:00
|
|
|
buttonVariant="transparent-with-text"
|
|
|
|
className="w-full group"
|
|
|
|
buttonContainerClassName="w-full text-left"
|
2024-02-29 14:10:46 +00:00
|
|
|
buttonClassName={`text-sm justify-between ${issue?.cycle_id ? "" : "text-custom-text-400"}`}
|
2024-01-24 13:51:59 +00:00
|
|
|
placeholder="No cycle"
|
|
|
|
hideIcon
|
|
|
|
dropdownArrow
|
|
|
|
dropdownArrowClassName="h-3.5 w-3.5 hidden group-hover:inline"
|
2024-01-10 14:39:45 +00:00
|
|
|
/>
|
|
|
|
{isUpdating && <Spinner className="h-4 w-4" />}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|