UNPKG

@axiomhq/react

Version:
37 lines (36 loc) 1.13 kB
"use client"; import { useState, useEffect } from "react"; function createUseLogger(logger) { if (!logger) { throw new Error("A logger must be provided to create useLogger"); } const useLogger = () => { const [path, setPath] = useState(typeof window !== "undefined" ? window.location.pathname : ""); useEffect(() => { const handleLocationChange = () => { setPath(window.location.pathname); }; window.addEventListener("popstate", handleLocationChange); window.addEventListener("pushState", handleLocationChange); window.addEventListener("replaceState", handleLocationChange); return () => { window.removeEventListener("popstate", handleLocationChange); window.removeEventListener("pushState", handleLocationChange); window.removeEventListener("replaceState", handleLocationChange); }; }, []); useEffect(() => { return () => { if (logger) { logger.flush(); } }; }, [path]); return logger; }; return useLogger; } export { createUseLogger }; //# sourceMappingURL=use-logger.js.map