B: Fix potential memory leaks

This commit is contained in:
Ivan 2025-11-06 15:22:25 +03:00
parent 0dd8f850ba
commit 6b01d9afb2
2 changed files with 24 additions and 8 deletions

View File

@ -1,5 +1,7 @@
'use client';
import { useEffect } from 'react';
import { urls, useConceptNavigation } from '@/app';
import { useAuthSuspense } from '@/features/auth';
@ -9,12 +11,14 @@ export function HomePage() {
const router = useConceptNavigation();
const { isAnonymous } = useAuthSuspense();
if (isAnonymous) {
useEffect(() => {
// Note: Timeout is needed to let router initialize
setTimeout(() => router.replace({ path: urls.login }), PARAMETER.minimalTimeout);
} else {
setTimeout(() => router.replace({ path: urls.library }), PARAMETER.minimalTimeout);
}
const timeoutId = setTimeout(() => {
router.replace({ path: isAnonymous ? urls.login : urls.library });
}, PARAMETER.minimalTimeout);
return () => clearTimeout(timeoutId);
}, [router, isAnonymous]);
return null;
}

View File

@ -9,16 +9,28 @@ export function useBrowserNavigation() {
const end = useAppTransitionStore(state => state.endNavigation);
useEffect(() => {
let timeoutId: ReturnType<typeof setTimeout> | null = null;
const onPopState = () => {
start();
if (timeoutId) {
clearTimeout(timeoutId);
}
// Fallback to end the navigation in case route completes with cache
setTimeout(() => {
timeoutId = setTimeout(() => {
end();
}, DELAY_CACHE_CHECK); // or cancel after Suspense/loader finishes
timeoutId = null;
}, DELAY_CACHE_CHECK);
};
window.addEventListener('popstate', onPopState);
return () => window.removeEventListener('popstate', onPopState);
return () => {
window.removeEventListener('popstate', onPopState);
if (timeoutId) {
clearTimeout(timeoutId);
}
};
}, [start, end]);
}