// ScrollToHashElement.tsx
import { useEffect } from "react";
import { useLocation } from "react-router-dom";

const ScrollToHashElement: React.FC<{ offset?: number }> = ({ offset = 80 }) => {
  const { hash } = useLocation();

  console.log('ScrollToHashElement');
  useEffect(() => {
    if (!hash) return;
    const el = document.querySelector(hash);
    if (!el) return;

    const y = el.getBoundingClientRect().top + window.scrollY - offset;
    window.scrollTo({ top: y, behavior: "smooth" });
  }, [hash, offset]);

  return null;
};

export default ScrollToHashElement;
