make settings esc button navigate back to app rather than back page (#1607)
This commit is contained in:
committed by
GitHub
parent
12829c34c6
commit
377941a90c
28
frontend/src/hooks/usePreviousPath.ts
Normal file
28
frontend/src/hooks/usePreviousPath.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { useCallback, useEffect } from 'react';
|
||||
import { useLocation, useNavigate } from 'react-router-dom';
|
||||
|
||||
const globalVisited: string[] = [];
|
||||
|
||||
export function usePreviousPath() {
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
|
||||
// Track pathnames as user navigates
|
||||
useEffect(() => {
|
||||
if (globalVisited[globalVisited.length - 1] !== location.pathname) {
|
||||
globalVisited.push(location.pathname);
|
||||
// Keep only last 50 entries to prevent memory bloat
|
||||
if (globalVisited.length > 50) {
|
||||
globalVisited.splice(0, globalVisited.length - 50);
|
||||
}
|
||||
}
|
||||
}, [location]);
|
||||
|
||||
return useCallback(() => {
|
||||
// Find last non-settings route in history
|
||||
const lastNonSettingsPath = [...globalVisited]
|
||||
.reverse()
|
||||
.find((p) => !p.startsWith('/settings'));
|
||||
navigate(lastNonSettingsPath || '/');
|
||||
}, [navigate]);
|
||||
}
|
||||
Reference in New Issue
Block a user