import { useState, useEffect } from "react"; import { isPathBanned, getCurrentPath } from "@/hooks/banned-path/path-utils"; import { withErrorHandling } from "@/utils/sentry"; export const useBannedPaths = (bannedPaths: string[]) => { const [currentPath, setCurrentPath] = useState(""); useEffect(() => { const updateCurrentPath = () => { setCurrentPath(getCurrentPath()); }; updateCurrentPath(); const handlePopState = withErrorHandling("handlePopState", () => { updateCurrentPath(); }); window.addEventListener("popstate", handlePopState); const originalPushState = history.pushState; const originalReplaceState = history.replaceState; history.pushState = withErrorHandling( "history.pushState", function (...args: unknown[]) { originalPushState.apply( history, args as [data: any, unused: string, url?: string | URL | null] ); updateCurrentPath(); } ) as typeof history.pushState; history.replaceState = withErrorHandling( "history.replaceState", function (...args: unknown[]) { originalReplaceState.apply( history, args as [data: any, unused: string, url?: string | URL | null] ); updateCurrentPath(); } ) as typeof history.replaceState; return () => { window.removeEventListener("popstate", handlePopState); history.pushState = originalPushState; history.replaceState = originalReplaceState; }; }, []); const isCurrentPathBanned = (): boolean => { return isPathBanned(currentPath, bannedPaths); }; return { isCurrentPathBanned, }; };