84 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-08-05 08:14:22 +03:00
import React, { useRef, useEffect, useState } from "react";
import { Task } from "../../types/public-types";
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;
svgHeight: number;
rowHeight: number;
task: BarTask;
2020-07-22 20:50:43 +03:00
fontSize: string;
fontFamily: string;
TooltipContent: React.FC<{
task: Task;
fontSize: string;
fontFamily: string;
}>;
2020-07-22 20:50:43 +03:00
};
export const Tooltip: React.FC<TooltipProps> = ({
x,
rowHeight,
svgHeight,
2020-07-22 20:50:43 +03:00
task,
fontSize,
fontFamily,
TooltipContent,
2020-07-22 20:50:43 +03:00
}) => {
const tooltipRef = useRef<HTMLDivElement | null>(null);
const [toolWidth, setToolWidth] = useState(1000);
const [relatedY, setRelatedY] = useState((task.index - 1) * rowHeight);
2020-07-22 20:50:43 +03:00
useEffect(() => {
if (tooltipRef.current) {
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);
}
}, [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}>
<TooltipContent
task={task}
fontSize={fontSize}
fontFamily={fontFamily}
/>
2020-07-22 20:50:43 +03:00
</div>
</foreignObject>
);
};
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>
<p className={styles.tooltipDefaultContainerParagraph}>
{!!task.progress && `Progress: ${task.progress} %`}
</p>
2020-07-22 20:50:43 +03:00
</div>
);
};