Skip to content

Commit 9663385

Browse files
fix(mobile): pull request rows keep cached diff stats while scrolling
Scrolling changes the visible rows, which makes a new stats query. That query has no answer until its cached rows load, so rows shown from the previous cached answer dropped their stats and got them back a moment later. Each environment now keeps the answer it showed until the new one lands, and the shown answers keep their identity when nothing changed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e7567b8 commit 9663385

1 file changed

Lines changed: 36 additions & 10 deletions

File tree

‎apps/mobile/src/features/pull-requests/usePullRequestList.ts‎

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RegistryContext, useAtomValue } from "@effect/atom-react";
22
import { executeAtomQuery, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
3-
import type { EnvironmentId, PullRequestListInput } from "@t3tools/contracts";
3+
import type { EnvironmentId, PullRequestDiffStat, PullRequestListInput } from "@t3tools/contracts";
44
import * as Cause from "effect/Cause";
55
import * as Effect from "effect/Effect";
66
import * as Option from "effect/Option";
@@ -252,16 +252,13 @@ export function usePullRequestList(targets: readonly ListTarget[]) {
252252
}, [statsKey]);
253253
const counts = useAtomValue(statsAtom);
254254
// Server answers land in pullRequestListStatsFamily; these cover a cached answer, which is
255-
// all an offline environment has.
255+
// all an offline environment has. Scrolling makes a new stats query, which has no answer until
256+
// its cached rows load, so each environment keeps the answer it showed until the new one lands.
257+
const [shown, setShown] = useState({ counts, answers: answeredStats(counts, []) });
258+
if (shown.counts !== counts) setShown({ counts, answers: answeredStats(counts, shown.answers) });
256259
const entries = useMemo(
257-
() =>
258-
withShownPullRequestStats(
259-
data.entries,
260-
counts.flatMap(({ environmentId, result }) =>
261-
AsyncResult.isSuccess(result) ? [{ environmentId, stats: result.value.stats }] : [],
262-
),
263-
),
264-
[data.entries, counts],
260+
() => withShownPullRequestStats(data.entries, shown.answers),
261+
[data.entries, shown.answers],
265262
);
266263

267264
const updateEntry = useCallback(
@@ -335,3 +332,32 @@ function requestErrors(results: readonly PromiseSettledResult<unknown>[]) {
335332
.join("\n") || null
336333
);
337334
}
335+
336+
type StatsAnswer = {
337+
readonly environmentId: EnvironmentId;
338+
readonly stats: readonly PullRequestDiffStat[];
339+
};
340+
341+
/** Each environment's newest stats answer, or the one it had while its query has none. */
342+
function answeredStats(
343+
counts: readonly {
344+
readonly environmentId: EnvironmentId;
345+
readonly result: AsyncResult.AsyncResult<
346+
{ readonly stats: readonly PullRequestDiffStat[] },
347+
unknown
348+
>;
349+
}[],
350+
previous: readonly StatsAnswer[],
351+
): readonly StatsAnswer[] {
352+
const next = counts.flatMap(({ environmentId, result }) => {
353+
const kept = previous.find((answer) => answer.environmentId === environmentId);
354+
if (!AsyncResult.isSuccess(result)) return kept ? [kept] : [];
355+
return kept?.stats === result.value.stats
356+
? [kept]
357+
: [{ environmentId, stats: result.value.stats }];
358+
});
359+
return next.length === previous.length &&
360+
next.every((answer, index) => answer === previous[index])
361+
? previous
362+
: next;
363+
}

0 commit comments

Comments
 (0)