From b3f5ba4260e9c7fa511e0fafe8c644eb516cd87f Mon Sep 17 00:00:00 2001 From: tapframe Date: Wed, 17 Dec 2025 23:27:26 +0530 Subject: [PATCH] continue watching crash fix --- .../home/ContinueWatchingSection.tsx | 67 +++++++++---------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/src/components/home/ContinueWatchingSection.tsx b/src/components/home/ContinueWatchingSection.tsx index 1dbe89d7..7cbfa49d 100644 --- a/src/components/home/ContinueWatchingSection.tsx +++ b/src/components/home/ContinueWatchingSection.tsx @@ -293,51 +293,50 @@ const ContinueWatchingSection = React.forwardRef((props, re const mergeBatchIntoState = async (batch: ContinueWatchingItem[]) => { if (!batch || batch.length === 0) return; + // 1. Filter items first (async checks) - do this BEFORE any state updates + const validItems: ContinueWatchingItem[] = []; + for (const it of batch) { + const key = `${it.type}:${it.id}`; + + // Skip recently removed items + if (recentlyRemovedRef.current.has(key)) { + continue; + } + + // Skip persistently removed items + const isRemoved = await storageService.isContinueWatchingRemoved(it.id, it.type); + if (isRemoved) { + continue; + } + + validItems.push(it); + } + + if (validItems.length === 0) return; + + // 2. Single state update for the entire batch setContinueWatchingItems((prev) => { const map = new Map(); + // Add existing items for (const it of prev) { map.set(`${it.type}:${it.id}`, it); } + // Merge new valid items + for (const it of validItems) { + const key = `${it.type}:${it.id}`; + const existing = map.get(key); + // Only update if newer or doesn't exist + if (!existing || (it.lastUpdated ?? 0) > (existing.lastUpdated ?? 0)) { + map.set(key, it); + } + } + const merged = Array.from(map.values()); merged.sort((a, b) => (b.lastUpdated ?? 0) - (a.lastUpdated ?? 0)); return merged; }); - - // Process batch items asynchronously to check removal status - for (const it of batch) { - const key = `${it.type}:${it.id}`; - - // Skip recently removed items to prevent immediate re-addition - if (recentlyRemovedRef.current.has(key)) { - continue; - } - - // Skip items that have been persistently marked as removed - const isRemoved = await storageService.isContinueWatchingRemoved(it.id, it.type); - if (isRemoved) { - continue; - } - - // Add the item to state - setContinueWatchingItems((prev) => { - const map = new Map(); - for (const existing of prev) { - map.set(`${existing.type}:${existing.id}`, existing); - } - - const existing = map.get(key); - if (!existing || (it.lastUpdated ?? 0) > (existing.lastUpdated ?? 0)) { - map.set(key, it); - const merged = Array.from(map.values()); - merged.sort((a, b) => (b.lastUpdated ?? 0) - (a.lastUpdated ?? 0)); - return merged; - } - - return prev; - }); - } }; try {