import { FC, useState } from "react"; import { observer } from "mobx-react"; import { Plus } from "lucide-react"; import { TEstimatePointsObject } from "@plane/types"; import { Button, Draggable, Sortable } from "@plane/ui"; // components import { EstimatePointItemPreview, EstimatePointCreate } from "@/components/estimates/points"; // constants import { EEstimateUpdateStages, maxEstimatesCount } from "@/constants/estimates"; // hooks import { useEstimate } from "@/hooks/store"; type TEstimatePointEditRoot = { workspaceSlug: string; projectId: string; estimateId: string; mode?: EEstimateUpdateStages; }; export const EstimatePointEditRoot: FC = observer((props) => { // props const { workspaceSlug, projectId, estimateId } = props; // hooks const { asJson: estimate, estimatePointIds, estimatePointById, updateEstimateSortOrder } = useEstimate(estimateId); // states const [estimatePointCreate, setEstimatePointCreate] = useState(undefined); const estimatePoints: TEstimatePointsObject[] = estimatePointIds && estimatePointIds.length > 0 ? (estimatePointIds.map((estimatePointId: string) => { const estimatePoint = estimatePointById(estimatePointId); if (estimatePoint) return { id: estimatePointId, key: estimatePoint.key, value: estimatePoint.value }; }) as TEstimatePointsObject[]) : ([] as TEstimatePointsObject[]); const handleEstimatePointCreate = (mode: "add" | "remove", value: TEstimatePointsObject) => { switch (mode) { case "add": setEstimatePointCreate((prevValue) => { prevValue = prevValue ? [...prevValue] : []; return [...prevValue, value]; }); break; case "remove": setEstimatePointCreate((prevValue) => { prevValue = prevValue ? [...prevValue] : []; return prevValue.filter((item) => item.key !== value.key); }); break; default: break; } }; const handleDragEstimatePoints = (updatedEstimatedOrder: TEstimatePointsObject[]) => { const updatedEstimateKeysOrder = updatedEstimatedOrder.map((item, index) => ({ ...item, key: index + 1 })); updateEstimateSortOrder(workspaceSlug, projectId, updatedEstimateKeysOrder); }; if (!workspaceSlug || !projectId || !estimateId) return <>; return (
{estimate?.type}
( {value?.id && ( )} )} onChange={(data: TEstimatePointsObject[]) => handleDragEstimatePoints(data)} keyExtractor={(item: TEstimatePointsObject) => item?.id?.toString() || item.value.toString()} /> {estimatePointCreate && estimatePointCreate.map((estimatePoint) => ( handleEstimatePointCreate("remove", estimatePoint)} /> ))} {estimatePoints && estimatePoints.length + (estimatePointCreate?.length || 0) <= maxEstimatesCount && ( )}
); });