Fix auto-retry and time filter pool clearing

- Fixed auto-retry: now retries if 0 posts in first 15s after page load
- Fixed time filter: now clears post pool when timeHours changes
- Removed buggy initialLoadRef-based retry that never triggered

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Your Name 2026-02-01 19:00:26 +00:00
parent 3ee536f24f
commit 1eaa2f0ac9
1 changed files with 9 additions and 8 deletions

View File

@ -427,6 +427,7 @@ export function usePostsEngine({
const searchResultsRef = useRef(false);
const replacePoolRef = useRef(false);
const retryRef = useRef({ key: "", count: 0, ts: 0 });
const pageLoadTimeRef = useRef(Date.now());
const abortControllerRef = useRef(null);
const prevContentFilterRef = useRef(contentFilter);
const [mapReady, setMapReady] = useState(false);
@ -1250,8 +1251,8 @@ export function usePostsEngine({
if (distKm > Math.max(50, radiusKm * 0.5)) {
shouldClearPool = true;
}
} else if (forceFetch || last.filterKey !== filterKey) {
// Filter changed or force fetch - clear pool
} else if (forceFetch || last.filterKey !== filterKey || last.timeHours !== timeHours) {
// Filter changed (including time filter) or force fetch - clear pool
shouldClearPool = true;
}
@ -1388,19 +1389,19 @@ export function usePostsEngine({
setLoadingPosts(false);
// Auto-retry if we got very few posts (network timing issue)
const MIN_EXPECTED_POSTS = 5;
// Auto-retry if we got 0 posts in the first 15 seconds after page load
const fetchedCount = Array.isArray(newPosts) ? newPosts.length : 0;
if (fetchedCount < MIN_EXPECTED_POSTS && initialLoadRef.current) {
const retryKey = `retry:${Math.round(lat * 100)}|${Math.round(lng * 100)}`;
const timeSincePageLoad = Date.now() - pageLoadTimeRef.current;
if (fetchedCount === 0 && timeSincePageLoad < 15000) {
const retryKey = `empty:${Math.round(lat * 100)}|${Math.round(lng * 100)}|${filterKey}`;
const now = Date.now();
if (retryRef.current.key !== retryKey || now - retryRef.current.ts > 10000) {
if (retryRef.current.key !== retryKey || now - retryRef.current.ts > 8000) {
retryRef.current = { key: retryKey, count: 0, ts: now };
}
if (retryRef.current.count < 2) {
retryRef.current.count += 1;
retryRef.current.ts = now;
console.log(`[usePostsEngine] Low post count (${fetchedCount}), auto-retry #${retryRef.current.count}`);
console.log(`[usePostsEngine] Empty result, auto-retry #${retryRef.current.count} (page load ${Math.round(timeSincePageLoad/1000)}s ago)`);
if (delayedFetchRef.current) clearTimeout(delayedFetchRef.current);
delayedFetchRef.current = setTimeout(load, 800);
}