2024-01-24 13:51:59 +00:00
|
|
|
import React, { useState } from "react";
|
2024-04-29 06:42:33 +00:00
|
|
|
import { observer } from "mobx-react";
|
2024-01-10 14:39:45 +00:00
|
|
|
// hooks
|
2024-01-24 13:51:59 +00:00
|
|
|
// components
|
2024-03-19 14:38:35 +00:00
|
|
|
import { CycleDropdown } from "@/components/dropdowns";
|
2024-01-10 14:39:45 +00:00
|
|
|
// ui
|
2024-01-24 13:51:59 +00:00
|
|
|
// helpers
|
2024-03-19 14:38:35 +00:00
|
|
|
import { cn } from "@/helpers/common.helper";
|
|
|
|
import { useIssueDetail } from "@/hooks/store";
|
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-03-06 13:09:14 +00:00
|
|
|
<div className={cn("flex h-full items-center gap-1", className)}>
|
2024-01-24 13:51:59 +00:00
|
|
|
<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"
|
2024-03-06 13:09:14 +00:00
|
|
|
className="group w-full"
|
2024-01-24 13:51:59 +00:00
|
|
|
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
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|