fix: Batch story list updates and limit length

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2025-11-19 17:57:13 +00:00
parent 02e86efb4f
commit e18aaad741

View File

@@ -20,7 +20,8 @@ function Feed({ updateCache }) {
if (!updated) return;
const existingStoryIds = new Set(stories ? stories.map(s => s.id) : []);
let currentStories = Array.isArray(stories) ? [...stories] : [];
const existingStoryIds = new Set(currentStories.map(s => s.id));
for (const newStory of [...newApiStories].reverse()) {
if (existingStoryIds.has(newStory.id)) {
@@ -37,22 +38,20 @@ function Feed({ updateCache }) {
console.log('preloaded', fullStory.id, fullStory.title);
updateCache(fullStory.id, fullStory);
setStories(prevStories => {
const currentStories = Array.isArray(prevStories) ? prevStories : [];
const updatedStories = [newStory, ...currentStories];
const poppedStory = updatedStories.pop();
if (poppedStory) {
localForage.removeItem(poppedStory.id);
}
localStorage.setItem('stories', JSON.stringify(updatedStories));
return updatedStories;
});
currentStories.unshift(newStory);
} catch (error) {
console.log('Skipping story due to fetch error', newStory.id, error);
}
}
const finalStories = currentStories.slice(0, newApiStories.length);
const removedStories = currentStories.slice(newApiStories.length);
for (const story of removedStories) {
localForage.removeItem(story.id);
}
localStorage.setItem('stories', JSON.stringify(finalStories));
setStories(finalStories);
},
(error) => {
setError(true);