diff --git a/packages/app/e2e/support/helpers/import-session.ts b/packages/app/e2e/support/helpers/import-session.ts index 9caabc5edd..757ab9e4ff 100644 --- a/packages/app/e2e/support/helpers/import-session.ts +++ b/packages/app/e2e/support/helpers/import-session.ts @@ -97,18 +97,15 @@ export class ImportSessionFlow { await target.scrollIntoViewIfNeeded(); await expect(target).toBeInViewport(); } + /** + * Scrolls the virtualized list, the only one that leaves rows unmounted. A row handle is no way + * in: the list unmounts rows as it scrolls, and a detached row has no scroller above it. + */ private async scrollList(forward: boolean) { - await this.page - .locator('[data-testid^="import-session-session-"]') - .first() - .evaluate((row, advance) => { - let element = row.parentElement; - while (element && !["auto", "scroll"].includes(getComputedStyle(element).overflowY)) - element = element.parentElement; - if (!element) throw new Error("Import list has no scroll owner"); - element.scrollTop = advance ? element.scrollTop + element.clientHeight * 0.8 : 0; - element.dispatchEvent(new Event("scroll")); - }, forward); + await this.page.getByTestId("import-session-list").evaluate((list, advance) => { + list.scrollTop = advance ? list.scrollTop + list.clientHeight * 0.8 : 0; + list.dispatchEvent(new Event("scroll")); + }, forward); } async search(query: string) { await this.page.getByTestId("import-session-search").fill(query); @@ -120,11 +117,18 @@ export class ImportSessionFlow { await this.page.getByTestId("import-session-search").fill(""); await expect(this.page.getByTestId("import-session-load-more")).toBeVisible(); } + /** Load more keeps the reader down the list instead of sending them back to the top. */ async loadMore() { - await this.page.getByTestId("import-session-load-more").click(); + const loadMore = this.page.getByTestId("import-session-load-more"); + const topRow = this.page.getByTestId((await this.rowIds())[0]!); + await expect(topRow).toBeInViewport(); + await loadMore.click(); + // The next page is the last and short, so the button leaves once it lands. + await expect(loadMore).toHaveCount(0); + await expect(this.page.getByText("Loading sessions...", { exact: true })).toHaveCount(0); + await expect(topRow).not.toBeInViewport(); await this.revealSession("fixture-root-20"); await expect(this.page.getByText("Root session 20", { exact: true })).toBeInViewport(); - await expect(this.page.getByTestId("import-session-load-more")).toHaveCount(0); } async retryProvider(provider: string, label: string) { await this.expectProviderError(label); diff --git a/packages/app/src/components/import-session-sheet-view-model.ts b/packages/app/src/components/import-session-sheet-view-model.ts index d9f31944c1..188442a449 100644 --- a/packages/app/src/components/import-session-sheet-view-model.ts +++ b/packages/app/src/components/import-session-sheet-view-model.ts @@ -17,6 +17,11 @@ export function nextPageLimit(limit: number): number { return PAGE_LIMITS.find((candidate) => candidate > limit) ?? limit; } +/** The page Load more grew this limit from, or null for the first page. */ +export function previousPageLimit(limit: number): number | null { + return PAGE_LIMITS.findLast((candidate) => candidate < limit) ?? null; +} + export function requiresImportSessionsHostUpgrade(input: { supportsSnapshot: boolean; workspaceId?: string | null; diff --git a/packages/app/src/components/import-session-sheet.test.tsx b/packages/app/src/components/import-session-sheet.test.tsx index 167de2a0ed..0024ba7f20 100644 --- a/packages/app/src/components/import-session-sheet.test.tsx +++ b/packages/app/src/components/import-session-sheet.test.tsx @@ -1172,6 +1172,44 @@ describe("ImportSessionSheet", () => { }); }); + it("keeps the loaded list in place while Load more fetches the next page", async () => { + let releaseNextPage: () => void = () => undefined; + const nextPage = new Promise((resolve) => { + releaseNextPage = resolve; + }); + const fetchRecentProviderSessions = vi.fn(async (options: { limit?: number } | undefined) => { + if (options?.limit !== 15) await nextPage; + return { + requestId: "recent-provider-sessions", + entries: createPageOfEntries(options?.limit === 15 ? 15 : 20), + }; + }); + + renderSheet(createRecentSessionsClient(fetchRecentProviderSessions, vi.fn()), { + cwd: null, + snapshot: { supportsSnapshot: true, entries: [createSnapshotEntry("claude")] }, + }); + + fireEvent.click(await screen.findByTestId("import-session-load-more")); + // The list the user scrolled to Load more is the one they keep: replacing it would drop + // them back at the top, behind a loading state, until the larger page lands. + const list = screen.getByTestId("import-session-list"); + await waitFor(() => { + expect(fetchRecentProviderSessions).toHaveBeenCalledWith({ + providers: ["claude"], + limit: 45, + }); + }); + expect(screen.getByTestId("import-session-list")).toBe(list); + expect(screen.queryByText("Loading sessions...")).toBeNull(); + + releaseNextPage(); + await waitFor(() => { + expect(screen.queryByTestId("import-session-load-more")).toBeNull(); + }); + expect(screen.getByTestId("import-session-list")).toBe(list); + }); + it("stops the refresh spinner and retries only the failed provider", async () => { const fetchRecentProviderSessions = vi.fn( async (options: { providers?: string[] } | undefined) => { diff --git a/packages/app/src/components/import-session-sheet.tsx b/packages/app/src/components/import-session-sheet.tsx index f97ae79cc6..5432b445b4 100644 --- a/packages/app/src/components/import-session-sheet.tsx +++ b/packages/app/src/components/import-session-sheet.tsx @@ -1,6 +1,6 @@ import React, { type ReactNode, useCallback, useEffect, useMemo, useRef, useState } from "react"; import { Pressable, type PressableStateCallbackType, Text, View } from "react-native"; -import { keepPreviousData, useMutation, useQueries, useQueryClient } from "@tanstack/react-query"; +import { useMutation, useQueries, useQueryClient, type QueryClient } from "@tanstack/react-query"; import { useTranslation } from "react-i18next"; import type { DaemonClient, @@ -40,6 +40,7 @@ import { resolveDirectoryLabel, nextPageLimit, PER_PROVIDER_LIMIT, + previousPageLimit, type ProviderErrorRow, resolveImportTarget, resolveProvidersToFetch, @@ -100,11 +101,12 @@ interface SessionsQueryConfig { // Query's default retry would keep the header spinner turning and pile up // in-flight daemon requests behind a dead provider (#2512). retry: false; - placeholderData: typeof keepPreviousData; + placeholderData: () => RecentSessionsResponse | undefined; queryFn: () => Promise; } function buildSessionsQueriesConfig(args: { + queryClient: QueryClient; providersToFetch: AgentProvider[] | null; visible: boolean; client: RecentProviderSessionsClient | null; @@ -113,14 +115,24 @@ function buildSessionsQueriesConfig(args: { limit: number; hostDisconnectedMessage?: string; }): SessionsQueryConfig[] { - const { providersToFetch, visible, client, cwd, query, limit, hostDisconnectedMessage } = args; + const { queryClient, providersToFetch, visible, client, cwd, query, limit } = args; + const { hostDisconnectedMessage } = args; if (providersToFetch === null) return []; const enabled = visible && Boolean(client); + const previousLimit = previousPageLimit(limit); return providersToFetch.map((provider) => ({ queryKey: buildSessionsQueryKey({ cwd, query, limit, provider }), enabled, retry: false as const, - placeholderData: keepPreviousData, + // While Load more fetches the next page, keep showing the page it grew from. React Query's + // keepPreviousData cannot do this here: useQueries gives a changed key a new observer, which + // has no previous data, so the rows would vanish and the list would remount at the top. + placeholderData: () => + previousLimit === null + ? undefined + : queryClient.getQueryData( + buildSessionsQueryKey({ cwd, query, limit: previousLimit, provider }), + ), queryFn: async () => { if (!client) { throw new Error(hostDisconnectedMessage ?? i18n.t("workspace.terminal.hostDisconnected")); @@ -522,6 +534,7 @@ export function ImportSessionSheet({ const queriesConfig = useMemo( () => buildSessionsQueriesConfig({ + queryClient, providersToFetch, visible, client, @@ -530,7 +543,7 @@ export function ImportSessionSheet({ limit: pageLimit, hostDisconnectedMessage: t("workspace.terminal.hostDisconnected"), }), - [providersToFetch, visible, client, scopeCwd, query, pageLimit, t], + [queryClient, providersToFetch, visible, client, scopeCwd, query, pageLimit, t], ); const queries = useQueries({ queries: queriesConfig });