Add support for changes dialog when leaving without saving config editor

This commit is contained in:
Nicolas Mowen 2024-08-21 07:48:14 -06:00
parent f6d1f610cb
commit c7d75bf318

View File

@ -124,12 +124,49 @@ function ConfigEditor() {
};
});
// monitoring state
const [hasChanges, setHasChanges] = useState(false);
useEffect(() => {
if (!config || !modelRef.current) {
return;
}
modelRef.current.onDidChangeContent(() => {
if (modelRef.current?.getValue() != config) {
setHasChanges(true);
} else {
setHasChanges(false);
}
});
}, [config]);
useEffect(() => {
if (config && modelRef.current) {
modelRef.current.setValue(config);
setHasChanges(false);
}
}, [config]);
useEffect(() => {
let listener: ((e: BeforeUnloadEvent) => void) | undefined;
if (hasChanges) {
listener = (e) => {
e.preventDefault();
e.returnValue = true;
return "Exit without saving?";
};
window.addEventListener("beforeunload", listener);
}
return () => {
if (listener) {
window.removeEventListener("beforeunload", listener);
}
};
}, [hasChanges]);
if (!config) {
return <ActivityIndicator />;
}