Skip to content

Commit

Permalink
Merge pull request #1174 from Agenta-AI/issue-1015/-refreshing-playgr…
Browse files Browse the repository at this point in the history
…ound-and-testset-result-in-an-alert

Refreshing playground and testset results in an alert
  • Loading branch information
aakrem authored Jan 14, 2024
2 parents a4e275f + 034925c commit 3b58a19
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions agenta-web/src/hooks/useBlockNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,23 @@ const useBlockNavigation = (
const props = useRef(_props)
const shouldAlert = useRef(_shouldAlert)

const beforeUnloadHandler = (event: BeforeUnloadEvent) => {
if (blocking.current) {
const message = "You have unsaved changes. Are you sure you want to leave?"
event.returnValue = message // Standard for most browsers
return message // For some older browsers
}
}

useEffect(() => {
blocking.current = _blocking

// prevent from reload or closing tab with unsaved changes
window.addEventListener("beforeunload", beforeUnloadHandler)

return () => {
window.removeEventListener("beforeunload", beforeUnloadHandler)
}
}, [_blocking])

useEffect(() => {
Expand All @@ -26,9 +41,6 @@ const useBlockNavigation = (
}, [_shouldAlert])

useEffect(() => {
// prevent from reload or closing tab
window.onbeforeunload = () => true

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

Expand Down Expand Up @@ -76,7 +88,7 @@ const useBlockNavigation = (

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

0 comments on commit 3b58a19

Please sign in to comment.