"use client";

import { useEffect, useState } from "react";

/**
 * The viewport width, for the handful of places that size an *icon* rather than
 * a layout.
 *
 * Starts at 0 and is set on mount rather than read at module scope: `window`
 * does not exist during the server render, and reading it during the first
 * client render would produce a hydration mismatch.
 */
export function useWindowWidth(): number {
  const [windowWidth, setWindowWidth] = useState(0);

  useEffect(() => {
    const handleResize = () => setWindowWidth(window.innerWidth);
    handleResize();
    window.addEventListener("resize", handleResize);
    return () => window.removeEventListener("resize", handleResize);
  }, []);

  return windowWidth;
}
