2020-08-05 08:14:22 +03:00
|
|
|
import React, { useRef, useEffect, useState } from "react";
|
|
|
|
|
import { Task } from "../../types/public-types";
|
2020-08-23 22:33:25 +03:00
|
|
|
import { BarTask } from "../../types/bar-task";
|
2020-08-05 08:14:22 +03:00
|
|
|
import styles from "./tooltip.module.css";
|
|
|
|
|
|
2020-07-22 20:50:43 +03:00
|
|
|
export type TooltipProps = {
|
|
|
|
|
x: number;
|
2020-08-23 22:33:25 +03:00
|
|
|
svgHeight: number;
|
|
|
|
|
rowHeight: number;
|
|
|
|
|
task: BarTask;
|
2020-07-22 20:50:43 +03:00
|
|
|
fontSize: string;
|
|
|
|
|
fontFamily: string;
|
2020-08-23 22:33:25 +03:00
|
|
|
TooltipContent: React.FC<{
|
|
|
|
|
task: Task;
|
|
|
|
|
fontSize: string;
|
|
|
|
|
fontFamily: string;
|
|
|
|
|
}>;
|
2020-07-22 20:50:43 +03:00
|
|
|
};
|
|
|
|
|
export const Tooltip: React.FC<TooltipProps> = ({
|
|
|
|
|
x,
|
2020-08-23 22:33:25 +03:00
|
|
|
rowHeight,
|
|
|
|
|
svgHeight,
|
2020-07-22 20:50:43 +03:00
|
|
|
task,
|
|
|
|
|
fontSize,
|
|
|
|
|
fontFamily,
|
2020-08-23 22:33:25 +03:00
|
|
|
TooltipContent,
|
2020-07-22 20:50:43 +03:00
|
|
|
}) => {
|
|
|
|
|
const tooltipRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
const [toolWidth, setToolWidth] = useState(1000);
|
2020-08-23 22:33:25 +03:00
|
|
|
const [relatedY, setRelatedY] = useState((task.index - 1) * rowHeight);
|
2020-07-22 20:50:43 +03:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (tooltipRef.current) {
|
2020-08-23 22:33:25 +03:00
|
|
|
const tooltipHeight = tooltipRef.current.offsetHeight;
|
|
|
|
|
const tooltipY = task.index * rowHeight + rowHeight;
|
|
|
|
|
if (tooltipHeight > tooltipY) {
|
|
|
|
|
setRelatedY(tooltipHeight * 0.5);
|
|
|
|
|
} else if (tooltipY + tooltipHeight > svgHeight) {
|
|
|
|
|
setRelatedY(svgHeight - tooltipHeight * 1.05);
|
|
|
|
|
}
|
2020-07-22 20:50:43 +03:00
|
|
|
setToolWidth(tooltipRef.current.scrollWidth * 1.1);
|
|
|
|
|
}
|
2020-08-23 22:33:25 +03:00
|
|
|
}, [tooltipRef, task]);
|
2020-07-22 20:50:43 +03:00
|
|
|
return (
|
|
|
|
|
<foreignObject x={x} y={relatedY} width={toolWidth} height={1000}>
|
2020-08-05 08:14:22 +03:00
|
|
|
<div ref={tooltipRef} className={styles.tooltipDetailsContainer}>
|
2020-08-23 22:33:25 +03:00
|
|
|
<TooltipContent
|
|
|
|
|
task={task}
|
|
|
|
|
fontSize={fontSize}
|
|
|
|
|
fontFamily={fontFamily}
|
|
|
|
|
/>
|
2020-07-22 20:50:43 +03:00
|
|
|
</div>
|
|
|
|
|
</foreignObject>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-23 22:33:25 +03:00
|
|
|
export const StandardTooltipContent: React.FC<{
|
|
|
|
|
task: Task;
|
|
|
|
|
fontSize: string;
|
|
|
|
|
fontFamily: string;
|
|
|
|
|
}> = ({ task, fontSize, fontFamily }) => {
|
2020-07-22 20:50:43 +03:00
|
|
|
const style = {
|
|
|
|
|
fontSize,
|
|
|
|
|
fontFamily,
|
|
|
|
|
};
|
|
|
|
|
return (
|
2020-08-05 08:14:22 +03:00
|
|
|
<div className={styles.tooltipDefaultContainer} style={style}>
|
2020-07-22 20:50:43 +03:00
|
|
|
<b style={{ fontSize: fontSize + 6 }}>{`${
|
|
|
|
|
task.name
|
2020-08-05 08:14:22 +03:00
|
|
|
}: ${task.start.getDate()}-${
|
|
|
|
|
task.start.getMonth() + 1
|
|
|
|
|
}-${task.start.getFullYear()} - ${task.end.getDate()}-${
|
|
|
|
|
task.end.getMonth() + 1
|
|
|
|
|
}-${task.end.getFullYear()}`}</b>
|
|
|
|
|
<p className={styles.tooltipDefaultContainerParagraph}>{`Duration: ${~~(
|
2020-07-22 20:50:43 +03:00
|
|
|
(task.end.getTime() - task.start.getTime()) /
|
|
|
|
|
(1000 * 60 * 60 * 24)
|
|
|
|
|
)} day(s)`}</p>
|
2020-08-23 22:33:25 +03:00
|
|
|
<p className={styles.tooltipDefaultContainerParagraph}>
|
|
|
|
|
{!!task.progress && `Progress: ${task.progress} %`}
|
|
|
|
|
</p>
|
2020-07-22 20:50:43 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|