Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion web/oss/src/hooks/useBlockNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ const useBlockNavigation = (
}, [_shouldAlert])

useEffect(() => {
// Handle unhandled promise rejections for our intentional route cancellation
const handleUnhandledRejection = (event: PromiseRejectionEvent) => {
if (event.reason === "cancelRouteChange") {
event.preventDefault()
}
}

window.addEventListener("unhandledrejection", handleUnhandledRejection)

const handler = (newRoute: string) => {
if (opened.current || !blocking.current) return

Expand Down Expand Up @@ -84,13 +93,15 @@ const useBlockNavigation = (
cancellable: false,
})

//block NextJS navigation until user confirms or cancels
// Block NextJS navigation until user confirms or cancels
Router.events.emit("routeChangeError")
throw "cancelRouteChange"
}

Router.events.on("routeChangeStart", handler)
return () => {
window.removeEventListener("beforeunload", beforeUnloadHandler)
window.removeEventListener("unhandledrejection", handleUnhandledRejection)
Router.events.off("routeChangeStart", handler)
}
}, [])
Expand Down