import { useEffect, useRef } from "react"; import { useTranslation } from "react-i18next"; import { EyeOff, List } from "lucide-react"; export interface ChartContextMenuProps { x: number; y: number; categoryName: string; onHide: () => void; onViewDetails: () => void; onClose: () => void; } export default function ChartContextMenu({ x, y, categoryName, onHide, onViewDetails, onClose, }: ChartContextMenuProps) { const { t } = useTranslation(); const menuRef = useRef(null); useEffect(() => { function handleClickOutside(e: MouseEvent) { if (menuRef.current && !menuRef.current.contains(e.target as Node)) { onClose(); } } function handleEscape(e: KeyboardEvent) { if (e.key === "Escape") onClose(); } document.addEventListener("mousedown", handleClickOutside); document.addEventListener("keydown", handleEscape); return () => { document.removeEventListener("mousedown", handleClickOutside); document.removeEventListener("keydown", handleEscape); }; }, [onClose]); // Adjust position to stay within viewport useEffect(() => { if (!menuRef.current) return; const rect = menuRef.current.getBoundingClientRect(); if (rect.right > window.innerWidth) { menuRef.current.style.left = `${x - rect.width}px`; } if (rect.bottom > window.innerHeight) { menuRef.current.style.top = `${y - rect.height}px`; } }, [x, y]); return (
{categoryName}
); }