Spaces:
Running
Running
Commit
·
a810633
1
Parent(s):
5b74493
Prevent accidental back button click in visualize page (#10)
Browse files- src/hooks/useBackButton.ts +50 -0
- src/views/example/Example.tsx +3 -0
src/hooks/useBackButton.ts
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
*
|
3 |
+
* Copyright 2023-2024 InspectorRAGet Team
|
4 |
+
*
|
5 |
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
6 |
+
* you may not use this file except in compliance with the License.
|
7 |
+
* You may obtain a copy of the License at
|
8 |
+
*
|
9 |
+
* http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
*
|
11 |
+
* Unless required by applicable law or agreed to in writing, software
|
12 |
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
13 |
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 |
+
* See the License for the specific language governing permissions and
|
15 |
+
* limitations under the License.
|
16 |
+
*
|
17 |
+
**/
|
18 |
+
|
19 |
+
import { useEffect } from 'react';
|
20 |
+
|
21 |
+
/**
|
22 |
+
* Detects when user clicks the back button and asks the user to reconfirm
|
23 |
+
*
|
24 |
+
*
|
25 |
+
*/
|
26 |
+
export function useBackButton(warningMessage?: string) {
|
27 |
+
const BACK_BUTTON_MESSAGE = 'Going back will make you lose the current progress. Are you sure you want to go back?';
|
28 |
+
|
29 |
+
const onBackButtonEvent = (e) => {
|
30 |
+
const leaveThisPage = window.confirm(warningMessage? warningMessage: BACK_BUTTON_MESSAGE);
|
31 |
+
if (leaveThisPage) {
|
32 |
+
// Let user go back
|
33 |
+
window.history.back();
|
34 |
+
}
|
35 |
+
};
|
36 |
+
|
37 |
+
useEffect(() => {
|
38 |
+
//@ts-ignore
|
39 |
+
window.history.pushState(null, null, window.location.pathname); // Prevent going back
|
40 |
+
window.addEventListener('popstate', onBackButtonEvent);
|
41 |
+
|
42 |
+
return () => {
|
43 |
+
window.removeEventListener('popstate', onBackButtonEvent);
|
44 |
+
};
|
45 |
+
}, []);
|
46 |
+
|
47 |
+
return {
|
48 |
+
|
49 |
+
};
|
50 |
+
}
|
src/views/example/Example.tsx
CHANGED
@@ -35,6 +35,7 @@ import {
|
|
35 |
import { Data, TaskEvaluation } from '@/src/types';
|
36 |
import { calculateAggregateValue } from '@/src/utilities/metrics';
|
37 |
import { useDataStore } from '@/src/store';
|
|
|
38 |
|
39 |
import Task from '@/src/views/task/Task';
|
40 |
import ExperimentTile from '@/src/components/example-tile/ExampleTile';
|
@@ -229,6 +230,8 @@ export default memo(function Example({ data }: { data: Data }) {
|
|
229 |
];
|
230 |
}, [data.evaluations, data.tasks, data.models, eligibleMetricsMap]);
|
231 |
|
|
|
|
|
232 |
// Step 3: Return
|
233 |
return (
|
234 |
<div className={classes.page}>
|
|
|
35 |
import { Data, TaskEvaluation } from '@/src/types';
|
36 |
import { calculateAggregateValue } from '@/src/utilities/metrics';
|
37 |
import { useDataStore } from '@/src/store';
|
38 |
+
import { useBackButton } from '@/src/hooks/useBackButton';
|
39 |
|
40 |
import Task from '@/src/views/task/Task';
|
41 |
import ExperimentTile from '@/src/components/example-tile/ExampleTile';
|
|
|
230 |
];
|
231 |
}, [data.evaluations, data.tasks, data.models, eligibleMetricsMap]);
|
232 |
|
233 |
+
const { } = useBackButton();
|
234 |
+
|
235 |
// Step 3: Return
|
236 |
return (
|
237 |
<div className={classes.page}>
|