Fix DoomScrollView going blank after 100 items

When history hit the 100-entry cap, setHistory sliced the array back to
indices 0–99 but setHistoryIndex still returned idx + 1 = 100, making
current = history[100] = undefined. Nothing rendered and no API calls
were made until the user went back (decrementing to index 99, which
held the newly-picked item).

Fix: cap the returned historyIndex at HISTORY_CAP - 1 so it always
points to a valid entry in the sliced array. Extract HISTORY_CAP = 100
as a named constant so the slice and the index cap stay in sync.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Garret Patti
2026-04-10 21:13:06 -04:00
parent 6f86750a99
commit 5d4d11512d

View File

@@ -16,6 +16,8 @@ interface Props {
onClose: () => void onClose: () => void
} }
const HISTORY_CAP = 100
function pickRandom(items: DoomScrollItem[], excludeRecent: DoomScrollItem[]): DoomScrollItem { function pickRandom(items: DoomScrollItem[], excludeRecent: DoomScrollItem[]): DoomScrollItem {
const excludeCount = Math.min(excludeRecent.length, items.length - 1) const excludeCount = Math.min(excludeRecent.length, items.length - 1)
const recentUrls = new Set(excludeRecent.slice(-excludeCount).map((i) => i.url)) const recentUrls = new Set(excludeRecent.slice(-excludeCount).map((i) => i.url))
@@ -55,9 +57,9 @@ export default function DoomScrollView({ items, videoContext = 'mixed', onClose
const next = pickRandom(items, history) const next = pickRandom(items, history)
setHistory((h) => { setHistory((h) => {
const updated = [...h, next] const updated = [...h, next]
return updated.length > 100 ? updated.slice(-100) : updated return updated.length > HISTORY_CAP ? updated.slice(-HISTORY_CAP) : updated
}) })
return idx + 1 return Math.min(idx + 1, HISTORY_CAP - 1)
}) })
}, [items, history]) }, [items, history])