import { useEffect, useRef, useState } from "react"; import { addDays } from "date-fns"; import { observer } from "mobx-react"; import { Plus } from "lucide-react"; // ui import { Tooltip } from "@plane/ui"; // helpers import { renderFormattedDate, renderFormattedPayloadDate } from "helpers/date-time.helper"; // types import { useGanttChart } from "../hooks/use-gantt-chart"; import { IBlockUpdateData, IGanttBlock } from "../types"; // hooks import { usePlatformOS } from "hooks/use-platform-os"; 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); // hooks const { isMobile } = usePlatformOS(); // 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 && ( )}
); });