mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-28 17:50:28 +08:00
Restore list scroll position on back/forward
Navigating away from a list and back via history (the sidebar back/forward buttons) returns to the same scroll position with the previously loaded pages intact, instead of refetching only the first page at the top.
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
resetIssueCounts,
|
||||
updateIssueCounts,
|
||||
} from "@app/lib/issueCounts.svelte";
|
||||
import type { ListCacheSnapshot } from "@app/lib/listState";
|
||||
import { readListState, saveListState } from "@app/lib/listState";
|
||||
import { show } from "@app/lib/modal";
|
||||
import * as mutexExecutor from "@app/lib/mutexExecutor";
|
||||
import * as router from "@app/lib/router";
|
||||
@@ -41,12 +43,32 @@
|
||||
|
||||
const { repo, issues, status }: Props = $props();
|
||||
|
||||
function listKey(filter: IssueStatus): string {
|
||||
return `repo.issues:${repo.rid}:${filter}`;
|
||||
}
|
||||
|
||||
// Restore the prior list and scroll position only when arriving via
|
||||
// back/forward; a fresh navigation (sidebar, links) starts from the top.
|
||||
// svelte-ignore state_referenced_locally
|
||||
const restored = router.isHistoryNavigation()
|
||||
? readListState<Issue>(listKey(status))
|
||||
: undefined;
|
||||
|
||||
// Parent reuses this component across status filter changes; a sibling
|
||||
// $effect resets pagination state when the issues prop changes.
|
||||
// $effect resets pagination state when the filter changes.
|
||||
// svelte-ignore state_referenced_locally
|
||||
let items = $state(issues.content);
|
||||
let items = $state(restored?.items ?? issues.content);
|
||||
// Rows fetched so far — used as the pagination offset. Tracked separately
|
||||
// from items.length because appends are deduped (overlapping pages, e.g. when
|
||||
// the list grows underneath us, must still advance the offset).
|
||||
// svelte-ignore state_referenced_locally
|
||||
let more = $state(issues.more);
|
||||
let cursor = $state((restored?.items ?? issues.content).length);
|
||||
// svelte-ignore state_referenced_locally
|
||||
let more = $state(restored?.more ?? issues.more);
|
||||
const initialScrollOffset = restored?.scrollOffset;
|
||||
const initialCache = restored?.cache;
|
||||
// svelte-ignore state_referenced_locally
|
||||
let activeKey = listKey(status);
|
||||
let loadingMore = $state(false);
|
||||
let loading = $state(false);
|
||||
let searchInput = $state("");
|
||||
@@ -63,13 +85,36 @@
|
||||
const abort = async (): Promise<undefined> => undefined;
|
||||
|
||||
$effect(() => {
|
||||
items = issues.content;
|
||||
more = issues.more;
|
||||
const key = listKey(status);
|
||||
const fresh = issues;
|
||||
// Skip the initial mount (state is seeded above, possibly restored); only
|
||||
// reset when the filter actually changes.
|
||||
if (key === activeKey) return;
|
||||
activeKey = key;
|
||||
items = fresh.content;
|
||||
cursor = fresh.content.length;
|
||||
more = fresh.more;
|
||||
// Abort any in-flight loadMoreContent so it cannot append a page
|
||||
// from the previous filter onto the just-reset items.
|
||||
void loader.run(abort);
|
||||
});
|
||||
|
||||
// Persist the loaded list + scroll position so back/forward can restore it.
|
||||
// Only the unfiltered list is cached, so its length matches virtua's size
|
||||
// snapshot.
|
||||
function persistScroll(state: {
|
||||
scrollOffset: number;
|
||||
cache: ListCacheSnapshot;
|
||||
}) {
|
||||
if (searchInput !== "") return;
|
||||
saveListState(listKey(status), {
|
||||
items: [...items],
|
||||
more,
|
||||
scrollOffset: state.scrollOffset,
|
||||
cache: state.cache,
|
||||
});
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (more === false) {
|
||||
updateIssueCounts(
|
||||
@@ -121,6 +166,7 @@
|
||||
|
||||
if (page !== undefined) {
|
||||
items = page.content;
|
||||
cursor = page.content.length;
|
||||
more = page.more;
|
||||
}
|
||||
|
||||
@@ -141,7 +187,7 @@
|
||||
return await invoke<PaginatedQuery<Issue[]>>("list_issues", {
|
||||
rid: repo.rid,
|
||||
status,
|
||||
skip: all ? 0 : items.length,
|
||||
skip: all ? 0 : cursor,
|
||||
take: all ? undefined : DEFAULT_TAKE,
|
||||
});
|
||||
});
|
||||
@@ -156,7 +202,17 @@
|
||||
}
|
||||
|
||||
more = page.more;
|
||||
items = all ? page.content : [...items, ...page.content];
|
||||
if (all) {
|
||||
items = page.content;
|
||||
cursor = page.content.length;
|
||||
} else {
|
||||
// Drop ids already shown so duplicate keys never reach the virtual
|
||||
// list (which would leave blank, persistent gaps); still advance the
|
||||
// offset by the rows fetched so paging keeps moving forward.
|
||||
const seen = new Set(items.map(i => i.id));
|
||||
items = [...items, ...page.content.filter(i => !seen.has(i.id))];
|
||||
cursor += page.content.length;
|
||||
}
|
||||
if (page.content.length === 0) more = false;
|
||||
} finally {
|
||||
if (!superseded) {
|
||||
@@ -315,33 +371,38 @@
|
||||
</div>
|
||||
</Topbar>
|
||||
|
||||
<ScrollArea style="height: 100%; min-width: 0;">
|
||||
<div bind:clientHeight={chromeHeight}>
|
||||
{#if issueCountMismatch(status)}
|
||||
<CobCacheWarning
|
||||
noun="issues"
|
||||
{cacheState}
|
||||
onRebuild={rebuildIssueCache} />
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if searchResults.length === 0}
|
||||
{#if searchResults.length === 0}
|
||||
{#if issueCountMismatch(status)}
|
||||
<CobCacheWarning
|
||||
noun="issues"
|
||||
{cacheState}
|
||||
onRebuild={rebuildIssueCache} />
|
||||
{/if}
|
||||
<div
|
||||
class="global-flex"
|
||||
style:flex="1"
|
||||
style:justify-content="center"
|
||||
style:align-items="center">
|
||||
<div
|
||||
class="global-flex"
|
||||
style:flex="1"
|
||||
style:justify-content="center"
|
||||
style:align-items="center">
|
||||
<div
|
||||
class="txt-missing txt-body-m-regular global-flex"
|
||||
style:gap="0.25rem">
|
||||
{#if items.length > 0}
|
||||
No matching issues
|
||||
{:else}
|
||||
No {status === "all" ? "" : status} issues
|
||||
{/if}
|
||||
</div>
|
||||
class="txt-missing txt-body-m-regular global-flex"
|
||||
style:gap="0.25rem">
|
||||
{#if items.length > 0}
|
||||
No matching issues
|
||||
{:else}
|
||||
No {status === "all" ? "" : status} issues
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<ScrollArea style="height: 100%; min-width: 0;">
|
||||
<div bind:clientHeight={chromeHeight}>
|
||||
{#if issueCountMismatch(status)}
|
||||
<CobCacheWarning
|
||||
noun="issues"
|
||||
{cacheState}
|
||||
onRebuild={rebuildIssueCache} />
|
||||
{/if}
|
||||
</div>
|
||||
{:else}
|
||||
<VirtualList
|
||||
items={searchResults}
|
||||
hasMore={more}
|
||||
@@ -349,7 +410,10 @@
|
||||
onLoadMore={() => loadMoreContent()}
|
||||
startMargin={chromeHeight}
|
||||
estimatedItemSize={80}
|
||||
getKey={result => result.obj.issue.id}>
|
||||
getKey={result => result.obj.issue.id}
|
||||
{initialCache}
|
||||
{initialScrollOffset}
|
||||
onState={persistScroll}>
|
||||
{#snippet row(result)}
|
||||
<div class="row">
|
||||
<IssueTeaser
|
||||
@@ -360,7 +424,7 @@
|
||||
</div>
|
||||
{/snippet}
|
||||
</VirtualList>
|
||||
{/if}
|
||||
</ScrollArea>
|
||||
</ScrollArea>
|
||||
{/if}
|
||||
</div>
|
||||
</Layout>
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
import delay from "lodash/delay";
|
||||
|
||||
import { invoke } from "@app/lib/invoke";
|
||||
import type { ListCacheSnapshot } from "@app/lib/listState";
|
||||
import { readListState, saveListState } from "@app/lib/listState";
|
||||
import * as mutexExecutor from "@app/lib/mutexExecutor";
|
||||
import {
|
||||
patchCountMismatch,
|
||||
@@ -39,12 +41,32 @@
|
||||
|
||||
const { repo, patches, status }: Props = $props();
|
||||
|
||||
function listKey(filter: PatchStatus | undefined): string {
|
||||
return `repo.patches:${repo.rid}:${filter ?? "all"}`;
|
||||
}
|
||||
|
||||
// Restore the prior list and scroll position only when arriving via
|
||||
// back/forward; a fresh navigation (sidebar, links) starts from the top.
|
||||
// svelte-ignore state_referenced_locally
|
||||
const restored = router.isHistoryNavigation()
|
||||
? readListState<Patch>(listKey(status))
|
||||
: undefined;
|
||||
|
||||
// Parent reuses this component across status filter changes; a sibling
|
||||
// $effect resets pagination state when the patches prop changes.
|
||||
// $effect resets pagination state when the filter changes.
|
||||
// svelte-ignore state_referenced_locally
|
||||
let items = $state(patches.content);
|
||||
let items = $state(restored?.items ?? patches.content);
|
||||
// Rows fetched so far — used as the pagination offset. Tracked separately
|
||||
// from items.length because appends are deduped (overlapping pages, e.g. when
|
||||
// the list grows underneath us, must still advance the offset).
|
||||
// svelte-ignore state_referenced_locally
|
||||
let more = $state(patches.more);
|
||||
let cursor = $state((restored?.items ?? patches.content).length);
|
||||
// svelte-ignore state_referenced_locally
|
||||
let more = $state(restored?.more ?? patches.more);
|
||||
const initialScrollOffset = restored?.scrollOffset;
|
||||
const initialCache = restored?.cache;
|
||||
// svelte-ignore state_referenced_locally
|
||||
let activeKey = listKey(status);
|
||||
let loadingMore = $state(false);
|
||||
let loading = $state(false);
|
||||
let searchInput = $state("");
|
||||
@@ -61,13 +83,36 @@
|
||||
const abort = async (): Promise<undefined> => undefined;
|
||||
|
||||
$effect(() => {
|
||||
items = patches.content;
|
||||
more = patches.more;
|
||||
const key = listKey(status);
|
||||
const fresh = patches;
|
||||
// Skip the initial mount (state is seeded above, possibly restored); only
|
||||
// reset when the filter actually changes.
|
||||
if (key === activeKey) return;
|
||||
activeKey = key;
|
||||
items = fresh.content;
|
||||
cursor = fresh.content.length;
|
||||
more = fresh.more;
|
||||
// Abort any in-flight loadMoreContent so it cannot append a page
|
||||
// from the previous filter onto the just-reset items.
|
||||
void loader.run(abort);
|
||||
});
|
||||
|
||||
// Persist the loaded list + scroll position so back/forward can restore it.
|
||||
// Only the unfiltered list is cached, so its length matches virtua's size
|
||||
// snapshot.
|
||||
function persistScroll(state: {
|
||||
scrollOffset: number;
|
||||
cache: ListCacheSnapshot;
|
||||
}) {
|
||||
if (searchInput !== "") return;
|
||||
saveListState(listKey(status), {
|
||||
items: [...items],
|
||||
more,
|
||||
scrollOffset: state.scrollOffset,
|
||||
cache: state.cache,
|
||||
});
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (more === false) {
|
||||
updatePatchCounts(items.length, project.meta.patches, status);
|
||||
@@ -112,6 +157,7 @@
|
||||
|
||||
if (page !== undefined) {
|
||||
items = page.content;
|
||||
cursor = page.content.length;
|
||||
more = page.more;
|
||||
}
|
||||
|
||||
@@ -132,7 +178,7 @@
|
||||
return await invoke<PaginatedQuery<Patch[]>>("list_patches", {
|
||||
rid: repo.rid,
|
||||
status,
|
||||
skip: all ? 0 : items.length,
|
||||
skip: all ? 0 : cursor,
|
||||
take: all ? undefined : DEFAULT_TAKE,
|
||||
});
|
||||
});
|
||||
@@ -147,7 +193,17 @@
|
||||
}
|
||||
|
||||
more = page.more;
|
||||
items = all ? page.content : [...items, ...page.content];
|
||||
if (all) {
|
||||
items = page.content;
|
||||
cursor = page.content.length;
|
||||
} else {
|
||||
// Drop ids already shown so duplicate keys never reach the virtual
|
||||
// list (which would leave blank, persistent gaps); still advance the
|
||||
// offset by the rows fetched so paging keeps moving forward.
|
||||
const seen = new Set(items.map(i => i.id));
|
||||
items = [...items, ...page.content.filter(i => !seen.has(i.id))];
|
||||
cursor += page.content.length;
|
||||
}
|
||||
if (page.content.length === 0) more = false;
|
||||
} finally {
|
||||
if (!superseded) {
|
||||
@@ -343,33 +399,38 @@
|
||||
</div>
|
||||
</Topbar>
|
||||
|
||||
<ScrollArea style="height: 100%; min-width: 0;">
|
||||
<div bind:clientHeight={chromeHeight}>
|
||||
{#if patchCountMismatch(status)}
|
||||
<CobCacheWarning
|
||||
noun="patches"
|
||||
{cacheState}
|
||||
onRebuild={rebuildPatchCache} />
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if searchResults.length === 0}
|
||||
{#if searchResults.length === 0}
|
||||
{#if patchCountMismatch(status)}
|
||||
<CobCacheWarning
|
||||
noun="patches"
|
||||
{cacheState}
|
||||
onRebuild={rebuildPatchCache} />
|
||||
{/if}
|
||||
<div
|
||||
class="global-flex"
|
||||
style:flex="1"
|
||||
style:justify-content="center"
|
||||
style:align-items="center">
|
||||
<div
|
||||
class="global-flex"
|
||||
style:flex="1"
|
||||
style:justify-content="center"
|
||||
style:align-items="center">
|
||||
<div
|
||||
class="txt-missing txt-body-m-regular global-flex"
|
||||
style:gap="0.25rem">
|
||||
{#if items.length > 0}
|
||||
No matching patches
|
||||
{:else}
|
||||
No {status === undefined ? "" : status} patches
|
||||
{/if}
|
||||
</div>
|
||||
class="txt-missing txt-body-m-regular global-flex"
|
||||
style:gap="0.25rem">
|
||||
{#if items.length > 0}
|
||||
No matching patches
|
||||
{:else}
|
||||
No {status === undefined ? "" : status} patches
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<ScrollArea style="height: 100%; min-width: 0;">
|
||||
<div bind:clientHeight={chromeHeight}>
|
||||
{#if patchCountMismatch(status)}
|
||||
<CobCacheWarning
|
||||
noun="patches"
|
||||
{cacheState}
|
||||
onRebuild={rebuildPatchCache} />
|
||||
{/if}
|
||||
</div>
|
||||
{:else}
|
||||
<VirtualList
|
||||
items={searchResults}
|
||||
hasMore={more}
|
||||
@@ -377,7 +438,10 @@
|
||||
onLoadMore={() => loadMoreContent()}
|
||||
startMargin={chromeHeight}
|
||||
estimatedItemSize={80}
|
||||
getKey={result => result.obj.patch.id}>
|
||||
getKey={result => result.obj.patch.id}
|
||||
{initialCache}
|
||||
{initialScrollOffset}
|
||||
onState={persistScroll}>
|
||||
{#snippet row(result)}
|
||||
<div class="row">
|
||||
<PatchTeaser
|
||||
@@ -388,7 +452,7 @@
|
||||
</div>
|
||||
{/snippet}
|
||||
</VirtualList>
|
||||
{/if}
|
||||
</ScrollArea>
|
||||
</ScrollArea>
|
||||
{/if}
|
||||
</div>
|
||||
</Layout>
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
import fuzzysort from "fuzzysort";
|
||||
|
||||
import { invoke } from "@app/lib/invoke";
|
||||
import type { ListCacheSnapshot } from "@app/lib/listState";
|
||||
import { readListState, saveListState } from "@app/lib/listState";
|
||||
import * as mutexExecutor from "@app/lib/mutexExecutor";
|
||||
import * as router from "@app/lib/router";
|
||||
import { modifierKey } from "@app/lib/utils";
|
||||
@@ -44,12 +46,25 @@
|
||||
commits: Commit[];
|
||||
};
|
||||
|
||||
function listKey(): string {
|
||||
return `repo.commits:${repo.rid}:${peer ?? ""}:${revision ?? ""}`;
|
||||
}
|
||||
|
||||
// Restore the prior list and scroll position only when arriving via
|
||||
// back/forward; a fresh navigation (sidebar, links) starts from the top.
|
||||
const restored = router.isHistoryNavigation()
|
||||
? readListState<Commit>(listKey())
|
||||
: undefined;
|
||||
|
||||
// Parent reuses this component across navigations; a sibling $effect resets
|
||||
// pagination state when the commits prop changes.
|
||||
// pagination state when the peer/revision changes.
|
||||
// svelte-ignore state_referenced_locally
|
||||
let items = $state(commits.content);
|
||||
let items = $state(restored?.items ?? commits.content);
|
||||
// svelte-ignore state_referenced_locally
|
||||
let more = $state(commits.more);
|
||||
let more = $state(restored?.more ?? commits.more);
|
||||
const initialScrollOffset = restored?.scrollOffset;
|
||||
const initialCache = restored?.cache;
|
||||
let activeKey = listKey();
|
||||
let loadingMore = $state(false);
|
||||
let loading = $state(false);
|
||||
let searchInput = $state("");
|
||||
@@ -60,13 +75,35 @@
|
||||
const abort = async (): Promise<undefined> => undefined;
|
||||
|
||||
$effect(() => {
|
||||
items = commits.content;
|
||||
more = commits.more;
|
||||
const key = listKey();
|
||||
const fresh = commits;
|
||||
// Skip the initial mount (state is seeded above, possibly restored); only
|
||||
// reset when the peer/revision changes.
|
||||
if (key === activeKey) return;
|
||||
activeKey = key;
|
||||
items = fresh.content;
|
||||
more = fresh.more;
|
||||
// Abort any in-flight loadMoreContent so it cannot append a page
|
||||
// from the previous navigation onto the just-reset items.
|
||||
void loader.run(abort);
|
||||
});
|
||||
|
||||
// Persist the loaded list + scroll position so back/forward can restore it.
|
||||
// Only the unfiltered list is cached, so its length matches virtua's size
|
||||
// snapshot.
|
||||
function persistScroll(state: {
|
||||
scrollOffset: number;
|
||||
cache: ListCacheSnapshot;
|
||||
}) {
|
||||
if (searchInput !== "") return;
|
||||
saveListState(listKey(), {
|
||||
items: [...items],
|
||||
more,
|
||||
scrollOffset: state.scrollOffset,
|
||||
cache: state.cache,
|
||||
});
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
const value = searchInput;
|
||||
const timer = setTimeout(() => {
|
||||
@@ -295,7 +332,10 @@
|
||||
{loadingMore}
|
||||
onLoadMore={() => loadMoreContent()}
|
||||
getKey={r =>
|
||||
r.type === "header" ? `h:${r.key}` : `c:${r.commit.id}`}>
|
||||
r.type === "header" ? `h:${r.key}` : `c:${r.commit.id}`}
|
||||
{initialCache}
|
||||
{initialScrollOffset}
|
||||
onState={persistScroll}>
|
||||
{#snippet row(item)}
|
||||
{#if item.type === "header"}
|
||||
<div class="group-title">{item.label}</div>
|
||||
|
||||
Reference in New Issue
Block a user