import { useEffect, useRef, useState } from "react"; import { addDays } from "date-fns"; import { Plus } from "lucide-react"; // ui import { Tooltip } from "@plane/ui"; // helpers import { renderFormattedDate, renderFormattedPayloadDate } from "helpers/date-time.helper"; // types import { IBlockUpdateData, IGanttBlock } from "../types"; import { useGanttChart } from "../hooks/use-gantt-chart"; import { observer } from "mobx-react"; type Props = { block: IGanttBlock; blockUpdateHandler: (block: any, payload: IBlockUpdateData) => void; }; export const ChartAddBlock: React.FC = observer((props) => { const { block, blockUpdateHandler } = props; // states const [isButtonVisible, setIsButtonVisible] = useState(false); const [buttonXPosition, setButtonXPosition] = useState(0); const [buttonStartDate, setButtonStartDate] = useState(null); // refs const containerRef = useRef(null); // chart hook const { currentViewData } = useGanttChart(); const handleButtonClick = () => { if (!currentViewData) return; const { startDate: chartStartDate, width } = currentViewData.data; const columnNumber = buttonXPosition / width; const startDate = addDays(chartStartDate, columnNumber); const endDate = addDays(startDate, 1); blockUpdateHandler(block.data, { start_date: renderFormattedPayloadDate(startDate) ?? undefined, target_date: renderFormattedPayloadDate(endDate) ?? undefined, }); }; useEffect(() => { const container = containerRef.current; if (!container) return; const handleMouseMove = (e: MouseEvent) => { if (!currentViewData) return; setButtonXPosition(e.offsetX); const { startDate: chartStartDate, width } = currentViewData.data; const columnNumber = buttonXPosition / width; const startDate = addDays(chartStartDate, columnNumber); setButtonStartDate(startDate); }; container.addEventListener("mousemove", handleMouseMove); return () => { container?.removeEventListener("mousemove", handleMouseMove); }; }, [buttonXPosition, currentViewData]); return (
setIsButtonVisible(true)} onMouseLeave={() => setIsButtonVisible(false)} >
{isButtonVisible && ( )}
); });