fix: spurious unsaved-changes warning when navigating between test cases in a test run (#406)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: kimatata <117462761+kimatata@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,19 @@
|
||||
import { useEffect } from 'react';
|
||||
|
||||
export const useFormGuard = (isDirty: boolean, confirmText: string, ignorePaths?: string[]) => {
|
||||
const isIgnoredPath = (href: string, compiledPatterns: RegExp[]): boolean => {
|
||||
return compiledPatterns.some((regex) => regex.test(href));
|
||||
};
|
||||
|
||||
export const useFormGuard = (isDirty: boolean, confirmText: string, ignorePathPatterns?: string[]) => {
|
||||
useEffect(() => {
|
||||
const compiledPatterns: RegExp[] = (ignorePathPatterns ?? []).flatMap((pattern) => {
|
||||
try {
|
||||
return [new RegExp(pattern)];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
|
||||
const handleClick = (event: MouseEvent) => {
|
||||
if (!isDirty) return;
|
||||
|
||||
@@ -13,7 +25,7 @@ export const useFormGuard = (isDirty: boolean, confirmText: string, ignorePaths?
|
||||
if (!href) return;
|
||||
|
||||
// do not show confirm for ignored paths
|
||||
if (ignorePaths && ignorePaths.some((path) => href.includes(path))) {
|
||||
if (isIgnoredPath(href, compiledPatterns)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -38,5 +50,5 @@ export const useFormGuard = (isDirty: boolean, confirmText: string, ignorePaths?
|
||||
window.removeEventListener('beforeunload', handleBeforeUnload);
|
||||
window.removeEventListener('click', handleClick, true);
|
||||
};
|
||||
}, [confirmText, isDirty, ignorePaths]);
|
||||
}, [confirmText, isDirty, ignorePathPatterns]);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user