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:
@@ -20,7 +20,8 @@ function Feed({ updateCache }) {
|
|||||||
|
|
||||||
if (!updated) return;
|
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()) {
|
for (const newStory of [...newApiStories].reverse()) {
|
||||||
if (existingStoryIds.has(newStory.id)) {
|
if (existingStoryIds.has(newStory.id)) {
|
||||||
@@ -37,22 +38,20 @@ function Feed({ updateCache }) {
|
|||||||
console.log('preloaded', fullStory.id, fullStory.title);
|
console.log('preloaded', fullStory.id, fullStory.title);
|
||||||
updateCache(fullStory.id, fullStory);
|
updateCache(fullStory.id, fullStory);
|
||||||
|
|
||||||
setStories(prevStories => {
|
currentStories.unshift(newStory);
|
||||||
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;
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('Skipping story due to fetch error', newStory.id, 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) => {
|
(error) => {
|
||||||
setError(true);
|
setError(true);
|
||||||
|
|||||||
Reference in New Issue
Block a user