Stabilize initial navigation effects

This commit is contained in:
2025-10-16 05:40:17 -06:00
parent 73537b28a2
commit 06842e9f40

View File

@@ -355,6 +355,14 @@ const LOCAL_STORAGE_KEY = "borealis_persistent_state";
[interpretPath, navigateTo] [interpretPath, navigateTo]
); );
const navigateToRef = useRef(navigateTo);
const navigateByPathRef = useRef(navigateByPath);
useEffect(() => {
navigateToRef.current = navigateTo;
navigateByPathRef.current = navigateByPath;
}, [navigateTo, navigateByPath]);
// Build breadcrumb items for current view // Build breadcrumb items for current view
const breadcrumbs = React.useMemo(() => { const breadcrumbs = React.useMemo(() => {
const items = []; const items = [];
@@ -500,6 +508,9 @@ const LOCAL_STORAGE_KEY = "borealis_persistent_state";
useEffect(() => { useEffect(() => {
if (!sessionResolved) return; if (!sessionResolved) return;
const navTo = navigateToRef.current;
const navByPath = navigateByPathRef.current;
if (user) { if (user) {
const stored = initialPathRef.current; const stored = initialPathRef.current;
const currentLocation = window.location.pathname + window.location.search; const currentLocation = window.location.pathname + window.location.search;
@@ -509,7 +520,7 @@ const LOCAL_STORAGE_KEY = "borealis_persistent_state";
: currentLocation === "/login" || currentLocation === "" : currentLocation === "/login" || currentLocation === ""
? "/devices" ? "/devices"
: currentLocation; : currentLocation;
navigateByPath(targetPath, { replace: true, allowUnauthenticated: true }); navByPath(targetPath, { replace: true, allowUnauthenticated: true });
initialPathRef.current = null; initialPathRef.current = null;
pendingPathRef.current = null; pendingPathRef.current = null;
} else { } else {
@@ -524,9 +535,9 @@ const LOCAL_STORAGE_KEY = "borealis_persistent_state";
if (rememberPath) { if (rememberPath) {
pendingPathRef.current = rememberPath; pendingPathRef.current = rememberPath;
} }
navigateTo("login", { replace: true, allowUnauthenticated: true, suppressPending: true }); navTo("login", { replace: true, allowUnauthenticated: true, suppressPending: true });
} }
}, [sessionResolved, user, navigateByPath, navigateTo]); }, [sessionResolved, user]);
useEffect(() => { useEffect(() => {
if (!sessionResolved) return; if (!sessionResolved) return;
@@ -537,15 +548,15 @@ const LOCAL_STORAGE_KEY = "borealis_persistent_state";
if (!path.startsWith("/login")) { if (!path.startsWith("/login")) {
pendingPathRef.current = path; pendingPathRef.current = path;
} }
navigateTo("login", { replace: true, allowUnauthenticated: true, suppressPending: true }); navigateToRef.current("login", { replace: true, allowUnauthenticated: true, suppressPending: true });
return; return;
} }
navigateByPath(path, { replace: true, allowUnauthenticated: true }); navigateByPathRef.current(path, { replace: true, allowUnauthenticated: true });
}; };
window.addEventListener("popstate", handlePopState); window.addEventListener("popstate", handlePopState);
return () => window.removeEventListener("popstate", handlePopState); return () => window.removeEventListener("popstate", handlePopState);
}, [sessionResolved, user, navigateByPath, navigateTo]); }, [sessionResolved, user]);
// Suggest fetcher with debounce // Suggest fetcher with debounce
const fetchSuggestions = useCallback((field, q) => { const fetchSuggestions = useCallback((field, q) => {