Backfill smart feed with posts when sparse

This commit is contained in:
Your Name 2025-12-26 01:10:59 -05:00
parent 5685668d18
commit e15c78e950
1 changed files with 27 additions and 4 deletions

View File

@ -663,8 +663,10 @@ export function usePostsEngine({
username: username || undefined,
});
const timeParam = timeFilter && timeFilter !== "PAST" ? timeFilter : undefined;
if (!hasPostsInRadius(newPosts, lat, lng, radiusKm)) {
newPosts = await fetchPosts({
const smartCount = Array.isArray(newPosts) ? newPosts.length : 0;
const needFallback = smartCount < 12 || !hasPostsInRadius(newPosts, lat, lng, radiusKm);
if (needFallback) {
const fallbackPosts = await fetchPosts({
category: catCode || undefined,
subCategory: subCatParam || undefined,
time: timeParam,
@ -673,8 +675,8 @@ export function usePostsEngine({
radiusKm,
limit: 120,
});
if ((!Array.isArray(newPosts) || newPosts.length === 0) && timeFilter !== "PAST") {
newPosts = await fetchPosts({
if ((!Array.isArray(fallbackPosts) || fallbackPosts.length === 0) && timeFilter !== "PAST") {
const widenedPosts = await fetchPosts({
category: catCode || undefined,
subCategory: subCatParam || undefined,
time: "PAST",
@ -683,6 +685,27 @@ export function usePostsEngine({
radiusKm,
limit: 120,
});
const merged = new Map();
for (const p of (newPosts || [])) {
const key = getPostKey(p);
if (key) merged.set(key, p);
}
for (const p of (widenedPosts || [])) {
const key = getPostKey(p);
if (key && !merged.has(key)) merged.set(key, p);
}
newPosts = Array.from(merged.values());
} else {
const merged = new Map();
for (const p of (newPosts || [])) {
const key = getPostKey(p);
if (key) merged.set(key, p);
}
for (const p of (fallbackPosts || [])) {
const key = getPostKey(p);
if (key && !merged.has(key)) merged.set(key, p);
}
newPosts = Array.from(merged.values());
}
}