import { useEffect } from 'react';

/**
 * When `error` is set (truthy), scrolls the window to the top so the user
 * sees the error message that is typically shown at the top of the form.
 * Use together with ErrorPopup for best UX: popup shows immediately,
 * and after closing the page is already scrolled to the error.
 * @param {string|null|undefined} error - Current error message state
 */
export function useScrollToError(error) {
  useEffect(() => {
    if (error) {
      window.scrollTo({ top: 0, behavior: 'smooth' });
    }
  }, [error]);
}
