DashboardPrompt: Implement beforeunload handler (#37490) (#37524)

(cherry picked from commit 81cf09af9e)

Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
This commit is contained in:
Grot (@grafanabot)
2021-08-04 11:41:54 +02:00
committed by GitHub
co-authored by Ashley Harrison
parent 2549519180
commit dd42424a01
@@ -50,6 +50,22 @@ export const DashboardPrompt = React.memo(({ dashboard }: Props) => {
};
}, [dashboard]);
useEffect(() => {
const handleUnload = (event: BeforeUnloadEvent) => {
if (ignoreChanges(dashboard, original)) {
return;
}
if (hasChanges(dashboard, original)) {
event.preventDefault();
// No browser actually displays this message anymore.
// But Chrome requires it to be defined else the popup won't show.
event.returnValue = '';
}
};
window.addEventListener('beforeunload', handleUnload);
return () => window.removeEventListener('beforeunload', handleUnload);
}, [dashboard, original]);
// Handle saved events
useEffect(() => {
const savedEventUnsub = appEvents.subscribe(DashboardSavedEvent, () => {