fix: keep folders left open on home from flickering on reload (#12222)

* fix: keep folders left open on home from flickering on reload

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: keep open owners' first pages in one cache entry per scope

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: replace an owner's cached rows on the first page that lands after a failure

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: cache only the rows an opened owner first renders

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-11-04 22:09:01 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 37db9acaa4
commit f6c4b260d5
@@ -596,6 +596,19 @@
// handing back a cursor can't spin forever; hitting it leaves `hasMore` set, so the
// footer stays and another click resumes where this one stopped.
const OWNER_LOAD_ALL_PAGES = 100
// The first rows of each owner open now, by prefix, as last fetched. One entry per listing
// scope rather than one per owner: the cache is shared and bounded, and an entry per owner,
// sort and filter would evict the other pages' entries. Only what an opened node first
// renders is kept: the entry reaches the first frame only while it fits the localStorage
// snapshot's budget, which drops an oversized entry whole.
const openOwnerPagesCache = persistedValue<Record<string, { items: RunnableItem[] }>>(
'home_open_owner_pages',
1
)
const OWNER_CACHED_ROWS = 50
// Prefixes whose rows on screen are still the cached page: until a first page of their own
// lands, whichever load that is (one after a failed request included) replaces them.
const ownersShowingCache = new Set<string>()
let ownerLoad = $state<Record<string, OwnerLoadState>>({})
let treeOwnerItems = $state<ItemType[]>([])
let treeGen = 0
@@ -645,11 +658,30 @@
gen
}
const prefix = `${owner}/`
// Only a forced refresh replaces this prefix's rows, so a reload swaps them without
// blanking the tree. Everything else merges: a nested prefix inherits rows from an
// ancestor's pages, and one page of its own can cover fewer of them than are shown —
const scopeArgs = {
...sortToParams(sortOrder),
showArchived: archived ? true : undefined,
includeWithoutMain: includeWithoutMain ? true : undefined,
kinds: runnableKinds,
includeDraftOnly: true,
perPage: OWNER_PAGE_SIZE
}
const params = { ...scopeArgs, pathStart: prefix }
const cacheScope = { workspace: ws, args: scopeArgs }
const sentWith = readSessionHint()
// A first load with nothing of this prefix on screen (a reload reopening the owners left
// open) paints the page last fetched, so the frame that mounts the tree has the rows the
// Home snapshot painted before the app booted.
const cached =
!more && !force && !treeOwnerItems.some((x) => effectivePath(x).startsWith(prefix))
? openOwnerPagesCache.peek(cacheScope)?.[owner]
: undefined
if (cached) ownersShowingCache.add(owner)
// A forced refresh, or the fresh page replacing a cached one, swaps this prefix's rows
// without blanking the tree. Everything else merges: a nested prefix inherits rows from
// an ancestor's pages, and one page of its own can cover fewer of them than are shown —
// replacing would delete rows on the click meant to add them.
const replacing = !more && force
const replacing = !more && (force || ownersShowingCache.has(owner))
// Merges a page and answers how many rows it actually added. Reads the live store
// each time rather than a snapshot, so a page landing while another prefix loads
// doesn't drop that prefix's rows.
@@ -674,6 +706,10 @@
treeOwnerItems = merged
return added
}
if (cached) {
mergePage(cached.items)
firstMerge = true
}
let cursor = more ? st?.cursor : undefined
let nextCursor: string | undefined
// A nested prefix's own stream restarts at its first row, which an ancestor's pages
@@ -685,17 +721,7 @@
for (let page = 0; page < (all ? OWNER_LOAD_ALL_PAGES : OWNER_CATCH_UP_PAGES); page++) {
let res: { items: RunnableItem[]; next_cursor?: string }
try {
res = await ScriptService.listRunnables({
workspace: ws,
...sortToParams(sortOrder),
showArchived: archived ? true : undefined,
includeWithoutMain: includeWithoutMain ? true : undefined,
kinds: runnableKinds,
pathStart: prefix,
includeDraftOnly: true,
perPage: OWNER_PAGE_SIZE,
cursor
})
res = await ScriptService.listRunnables({ workspace: ws, ...params, cursor })
} catch (e: any) {
if (gen !== treeGen) return
// Keep the cursor the pages that did land reached, so the next click resumes
@@ -718,7 +744,22 @@
// workspace changed and reset the tree); drop the response so stale rows from
// another scope can't appear under the current one.
if (gen !== treeGen) return
if (!more && page === 0) {
// Owners closed since are dropped: a reload reopens only the ones open now.
const kept = Object.entries(openOwnerPagesCache.peek(cacheScope) ?? {}).filter(([p]) =>
openOwners.has(p)
)
openOwnerPagesCache.save(
cacheScope,
{
...Object.fromEntries(kept),
[owner]: { items: res.items.slice(0, OWNER_CACHED_ROWS) }
},
sentWith
)
}
const added = mergePage(res.items)
if (replacing) ownersShowingCache.delete(owner)
nextCursor = res.next_cursor
cursor = nextCursor
// Collapsing the node is the only way to stop a run that spans many pages;