make settings esc button navigate back to app rather than back page (#1607)

This commit is contained in:
Gabriel Gordon-Hall
2025-12-18 18:00:28 +00:00
committed by GitHub
parent 12829c34c6
commit 377941a90c
3 changed files with 37 additions and 8 deletions

View 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]);
}