2024-02-12 09:38:17 +00:00
|
|
|
import { FC, useEffect, useState } from "react";
|
2024-02-23 13:40:45 +00:00
|
|
|
import { observer } from "mobx-react";
|
|
|
|
// hooks
|
2024-02-12 09:38:17 +00:00
|
|
|
// components
|
2024-02-23 13:40:45 +00:00
|
|
|
import { GanttChartHeader, GanttChartMainContent } from "components/gantt-chart";
|
2024-02-12 09:38:17 +00:00
|
|
|
// views
|
|
|
|
// helpers
|
|
|
|
import { cn } from "helpers/common.helper";
|
|
|
|
// types
|
|
|
|
// data
|
2024-03-06 13:09:14 +00:00
|
|
|
import { SIDEBAR_WIDTH } from "../constants";
|
2024-02-12 09:38:17 +00:00
|
|
|
import { currentViewDataWithView } from "../data";
|
|
|
|
// constants
|
2024-03-06 13:09:14 +00:00
|
|
|
import { useGanttChart } from "../hooks/use-gantt-chart";
|
|
|
|
import { ChartDataType, IBlockUpdateData, IGanttBlock, TGanttViews } from "../types";
|
|
|
|
import {
|
|
|
|
generateMonthChart,
|
|
|
|
getNumberOfDaysBetweenTwoDatesInMonth,
|
|
|
|
getMonthChartItemPositionWidthInMonth,
|
|
|
|
} from "../views";
|
2024-02-12 09:38:17 +00:00
|
|
|
|
|
|
|
type ChartViewRootProps = {
|
|
|
|
border: boolean;
|
|
|
|
title: string;
|
|
|
|
loaderTitle: string;
|
|
|
|
blocks: IGanttBlock[] | null;
|
|
|
|
blockUpdateHandler: (block: any, payload: IBlockUpdateData) => void;
|
|
|
|
blockToRender: (data: any) => React.ReactNode;
|
|
|
|
sidebarToRender: (props: any) => React.ReactNode;
|
|
|
|
enableBlockLeftResize: boolean;
|
|
|
|
enableBlockRightResize: boolean;
|
|
|
|
enableBlockMove: boolean;
|
|
|
|
enableReorder: boolean;
|
2024-02-12 13:40:07 +00:00
|
|
|
enableAddBlock: boolean;
|
2024-02-12 09:38:17 +00:00
|
|
|
bottomSpacing: boolean;
|
|
|
|
showAllBlocks: boolean;
|
2024-02-14 12:45:13 +00:00
|
|
|
quickAdd?: React.JSX.Element | undefined;
|
2024-02-12 09:38:17 +00:00
|
|
|
};
|
|
|
|
|
2024-02-23 13:40:45 +00:00
|
|
|
export const ChartViewRoot: FC<ChartViewRootProps> = observer((props) => {
|
2024-02-12 09:38:17 +00:00
|
|
|
const {
|
|
|
|
border,
|
|
|
|
title,
|
|
|
|
blocks = null,
|
|
|
|
loaderTitle,
|
|
|
|
blockUpdateHandler,
|
|
|
|
sidebarToRender,
|
|
|
|
blockToRender,
|
|
|
|
enableBlockLeftResize,
|
|
|
|
enableBlockRightResize,
|
|
|
|
enableBlockMove,
|
|
|
|
enableReorder,
|
2024-02-12 13:40:07 +00:00
|
|
|
enableAddBlock,
|
2024-02-12 09:38:17 +00:00
|
|
|
bottomSpacing,
|
|
|
|
showAllBlocks,
|
2024-02-14 12:45:13 +00:00
|
|
|
quickAdd,
|
2024-02-12 09:38:17 +00:00
|
|
|
} = props;
|
|
|
|
// states
|
|
|
|
const [itemsContainerWidth, setItemsContainerWidth] = useState(0);
|
|
|
|
const [fullScreenMode, setFullScreenMode] = useState(false);
|
|
|
|
const [chartBlocks, setChartBlocks] = useState<IGanttBlock[] | null>(null);
|
|
|
|
// hooks
|
2024-02-23 13:40:45 +00:00
|
|
|
const { currentView, currentViewData, renderView, updateCurrentView, updateCurrentViewData, updateRenderView } =
|
|
|
|
useGanttChart();
|
2024-02-12 09:38:17 +00:00
|
|
|
|
|
|
|
// rendering the block structure
|
|
|
|
const renderBlockStructure = (view: any, blocks: IGanttBlock[] | null) =>
|
|
|
|
blocks
|
|
|
|
? blocks.map((block: any) => ({
|
|
|
|
...block,
|
|
|
|
position: getMonthChartItemPositionWidthInMonth(view, block),
|
|
|
|
}))
|
|
|
|
: [];
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!currentViewData || !blocks) return;
|
|
|
|
setChartBlocks(() => renderBlockStructure(currentViewData, blocks));
|
|
|
|
}, [currentViewData, blocks]);
|
|
|
|
|
|
|
|
const updateCurrentViewRenderPayload = (side: null | "left" | "right", view: TGanttViews) => {
|
|
|
|
const selectedCurrentView: TGanttViews = view;
|
|
|
|
const selectedCurrentViewData: ChartDataType | undefined =
|
|
|
|
selectedCurrentView && selectedCurrentView === currentViewData?.key
|
|
|
|
? currentViewData
|
|
|
|
: currentViewDataWithView(view);
|
|
|
|
|
|
|
|
if (selectedCurrentViewData === undefined) return;
|
|
|
|
|
|
|
|
let currentRender: any;
|
|
|
|
if (selectedCurrentView === "month") currentRender = generateMonthChart(selectedCurrentViewData, side);
|
|
|
|
|
|
|
|
// updating the prevData, currentData and nextData
|
|
|
|
if (currentRender.payload.length > 0) {
|
2024-02-23 13:40:45 +00:00
|
|
|
updateCurrentViewData(currentRender.state);
|
|
|
|
|
2024-02-12 09:38:17 +00:00
|
|
|
if (side === "left") {
|
2024-02-23 13:40:45 +00:00
|
|
|
updateCurrentView(selectedCurrentView);
|
|
|
|
updateRenderView([...currentRender.payload, ...renderView]);
|
2024-02-12 09:38:17 +00:00
|
|
|
updatingCurrentLeftScrollPosition(currentRender.scrollWidth);
|
|
|
|
setItemsContainerWidth(itemsContainerWidth + currentRender.scrollWidth);
|
|
|
|
} else if (side === "right") {
|
2024-02-23 13:40:45 +00:00
|
|
|
updateCurrentView(view);
|
|
|
|
updateRenderView([...renderView, ...currentRender.payload]);
|
2024-02-12 09:38:17 +00:00
|
|
|
setItemsContainerWidth(itemsContainerWidth + currentRender.scrollWidth);
|
|
|
|
} else {
|
2024-02-23 13:40:45 +00:00
|
|
|
updateCurrentView(view);
|
|
|
|
updateRenderView(currentRender.payload);
|
2024-02-12 09:38:17 +00:00
|
|
|
setItemsContainerWidth(currentRender.scrollWidth);
|
|
|
|
setTimeout(() => {
|
|
|
|
handleScrollToCurrentSelectedDate(currentRender.state, currentRender.state.data.currentDate);
|
|
|
|
}, 50);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleToday = () => updateCurrentViewRenderPayload(null, currentView);
|
|
|
|
|
|
|
|
// handling the scroll positioning from left and right
|
|
|
|
useEffect(() => {
|
|
|
|
handleToday();
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const updatingCurrentLeftScrollPosition = (width: number) => {
|
|
|
|
const scrollContainer = document.querySelector("#gantt-container") as HTMLDivElement;
|
|
|
|
if (!scrollContainer) return;
|
|
|
|
|
|
|
|
scrollContainer.scrollLeft = width + scrollContainer?.scrollLeft;
|
|
|
|
setItemsContainerWidth(width + scrollContainer?.scrollLeft);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleScrollToCurrentSelectedDate = (currentState: ChartDataType, date: Date) => {
|
|
|
|
const scrollContainer = document.querySelector("#gantt-container") as HTMLDivElement;
|
|
|
|
if (!scrollContainer) return;
|
|
|
|
|
|
|
|
const clientVisibleWidth: number = scrollContainer?.clientWidth;
|
|
|
|
let scrollWidth: number = 0;
|
|
|
|
let daysDifference: number = 0;
|
|
|
|
|
|
|
|
// if (currentView === "hours")
|
|
|
|
// daysDifference = getNumberOfDaysBetweenTwoDatesInMonth(currentState.data.startDate, date);
|
|
|
|
// if (currentView === "day")
|
|
|
|
// daysDifference = getNumberOfDaysBetweenTwoDatesInMonth(currentState.data.startDate, date);
|
|
|
|
// if (currentView === "week")
|
|
|
|
// daysDifference = getNumberOfDaysBetweenTwoDatesInMonth(currentState.data.startDate, date);
|
|
|
|
// if (currentView === "bi_week")
|
|
|
|
// daysDifference = getNumberOfDaysBetweenTwoDatesInMonth(currentState.data.startDate, date);
|
|
|
|
if (currentView === "month")
|
|
|
|
daysDifference = getNumberOfDaysBetweenTwoDatesInMonth(currentState.data.startDate, date);
|
|
|
|
// if (currentView === "quarter")
|
|
|
|
// daysDifference = getNumberOfDaysBetweenTwoDatesInQuarter(currentState.data.startDate, date);
|
|
|
|
// if (currentView === "year")
|
|
|
|
// daysDifference = getNumberOfDaysBetweenTwoDatesInYear(currentState.data.startDate, date);
|
|
|
|
|
|
|
|
scrollWidth =
|
|
|
|
daysDifference * currentState.data.width - (clientVisibleWidth / 2 - currentState.data.width) + SIDEBAR_WIDTH / 2;
|
|
|
|
|
|
|
|
scrollContainer.scrollLeft = scrollWidth;
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={cn("relative flex flex-col h-full select-none rounded-sm bg-custom-background-100 shadow", {
|
|
|
|
"fixed inset-0 z-[999999] bg-custom-background-100": fullScreenMode,
|
|
|
|
"border-[0.5px] border-custom-border-200": border,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<GanttChartHeader
|
|
|
|
blocks={blocks}
|
|
|
|
fullScreenMode={fullScreenMode}
|
|
|
|
toggleFullScreenMode={() => setFullScreenMode((prevData) => !prevData)}
|
|
|
|
handleChartView={(key) => updateCurrentViewRenderPayload(null, key)}
|
|
|
|
handleToday={handleToday}
|
|
|
|
loaderTitle={loaderTitle}
|
|
|
|
title={title}
|
|
|
|
/>
|
|
|
|
<GanttChartMainContent
|
|
|
|
blocks={blocks}
|
|
|
|
blockToRender={blockToRender}
|
|
|
|
blockUpdateHandler={blockUpdateHandler}
|
|
|
|
bottomSpacing={bottomSpacing}
|
|
|
|
chartBlocks={chartBlocks}
|
|
|
|
enableBlockLeftResize={enableBlockLeftResize}
|
|
|
|
enableBlockMove={enableBlockMove}
|
|
|
|
enableBlockRightResize={enableBlockRightResize}
|
|
|
|
enableReorder={enableReorder}
|
2024-02-12 13:40:07 +00:00
|
|
|
enableAddBlock={enableAddBlock}
|
2024-02-12 09:38:17 +00:00
|
|
|
itemsContainerWidth={itemsContainerWidth}
|
|
|
|
showAllBlocks={showAllBlocks}
|
|
|
|
sidebarToRender={sidebarToRender}
|
|
|
|
title={title}
|
|
|
|
updateCurrentViewRenderPayload={updateCurrentViewRenderPayload}
|
2024-02-14 12:45:13 +00:00
|
|
|
quickAdd={quickAdd}
|
2024-02-12 09:38:17 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2024-02-23 13:40:45 +00:00
|
|
|
});
|