Skip to content

Commit 0b8f7f5

Browse files
fix(mobile): pull request rows refresh their diff stats after a new push
The mobile PR list cached each row's +/- counts by project, repository and number. A list answer for a PR with new commits kept the old counts on the row, and since the row already had counts, no stats request went out for the new head. The per-environment stats cache now also records each row's updatedAt as its list answered it. When a list answers a row with a different updatedAt, its cached stat is dropped, so the row asks for stats again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent becf853 commit 0b8f7f5

4 files changed

Lines changed: 95 additions & 16 deletions

File tree

‎apps/mobile/src/features/pull-requests/pullRequestList.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import {
99
appendPullRequestListPage,
1010
DEFAULT_PR_LIST_CONTROLS,
11+
EMPTY_PULL_REQUEST_STATS_CACHE,
1112
filterPullRequestList,
1213
mergePullRequestListEntries,
1314
mergeEnvironmentPullRequestPages,
@@ -21,6 +22,8 @@ import {
2122
scopedPullRequestListKey,
2223
shownPullRequestListPages,
2324
pullRequestListRowsAreEqual,
25+
withAnsweredPullRequestStats,
26+
withListedPullRequests,
2427
withShownPullRequestStats,
2528
withoutScratchProjectScope,
2629
} from "./pullRequestList";
@@ -435,3 +438,30 @@ describe("shown PR diff stats", () => {
435438
expect(filled[1]).toBe(rows[1]);
436439
});
437440
});
441+
442+
describe("cached PR diff stats", () => {
443+
const stat = (number: number, additions: number) => ({
444+
projectId: "project" as PullRequestListEntry["projectId"],
445+
repository: "org/repo",
446+
number,
447+
additions,
448+
deletions: 0,
449+
});
450+
451+
it("drops a row's stat when a list answers it with a new updatedAt", () => {
452+
const answered = withAnsweredPullRequestStats(
453+
withListedPullRequests(EMPTY_PULL_REQUEST_STATS_CACHE, [entry(1), entry(2)]),
454+
[stat(1, 5), stat(2, 7)],
455+
);
456+
expect(withListedPullRequests(answered, [entry(1), entry(2)])).toBe(answered);
457+
458+
const pushed = withListedPullRequests(answered, [
459+
entry(1, { updatedAt: "2026-10-20T00:00:00Z" }),
460+
entry(2),
461+
]);
462+
expect([...pushed.stats.values()]).toEqual([stat(2, 7)]);
463+
expect(
464+
withAnsweredPullRequestStats(pushed, [stat(1, 9)]).stats.get("project:org/repo:1"),
465+
).toEqual(stat(1, 9));
466+
});
467+
});

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,50 @@ export const pullRequestStatKey = (
5050
row: Pick<PullRequestDiffStat, "projectId" | "repository" | "number">,
5151
) => `${row.projectId}:${row.repository}:${row.number}`;
5252

53+
/** Diff stats an environment has answered, and each row's `updatedAt` as its list last answered it. */
54+
export interface PullRequestStatsCache {
55+
readonly stats: ReadonlyMap<string, PullRequestDiffStat>;
56+
readonly updatedAt: ReadonlyMap<string, string>;
57+
}
58+
59+
export const EMPTY_PULL_REQUEST_STATS_CACHE: PullRequestStatsCache = {
60+
stats: new Map(),
61+
updatedAt: new Map(),
62+
};
63+
64+
/**
65+
* Records the rows a list answered. Stats carry no head, so a row whose `updatedAt` moved (a push,
66+
* among other changes) drops its cached stat and the list asks for it again.
67+
*/
68+
export function withListedPullRequests(
69+
cache: PullRequestStatsCache,
70+
entries: readonly Pick<
71+
PullRequestListEntry,
72+
"projectId" | "repository" | "number" | "updatedAt"
73+
>[],
74+
): PullRequestStatsCache {
75+
const moved = entries.filter(
76+
(entry) => cache.updatedAt.get(pullRequestStatKey(entry)) !== entry.updatedAt,
77+
);
78+
if (!moved.length) return cache;
79+
const stats = new Map(cache.stats);
80+
const updatedAt = new Map(cache.updatedAt);
81+
for (const entry of moved) {
82+
stats.delete(pullRequestStatKey(entry));
83+
updatedAt.set(pullRequestStatKey(entry), entry.updatedAt);
84+
}
85+
return { stats, updatedAt };
86+
}
87+
88+
export function withAnsweredPullRequestStats(
89+
cache: PullRequestStatsCache,
90+
answered: readonly PullRequestDiffStat[],
91+
): PullRequestStatsCache {
92+
const stats = new Map(cache.stats);
93+
for (const row of answered) stats.set(pullRequestStatKey(row), row);
94+
return { ...cache, stats };
95+
}
96+
5397
/**
5498
* Fills rows that have no diff stat yet from the stats answers on screen. Returns `entries`
5599
* itself when no row changes, so the list keeps its identity across renders.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export function usePullRequestList(targets: readonly ListTarget[]) {
185185
pages: {
186186
...pages,
187187
entries: pages.entries.map((entry) => {
188-
const stat = stats.get(statKey(entry));
188+
const stat = stats.stats.get(statKey(entry));
189189
return stat
190190
? { ...entry, additions: stat.additions, deletions: stat.deletions }
191191
: entry;

‎apps/mobile/src/state/pull-requests.ts‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import {
22
createPullRequestEnvironmentAtoms,
33
createPullRequestStackAtomFamily,
44
} from "@t3tools/client-runtime/state/pull-requests";
5-
import type { EnvironmentId, PullRequestDiffStat, PullRequestListInput } from "@t3tools/contracts";
5+
import type { EnvironmentId, PullRequestListInput } from "@t3tools/contracts";
66
import * as Effect from "effect/Effect";
77
import * as Option from "effect/Option";
88
import { AsyncResult, Atom } from "effect/reactivity";
99
import { connectionAtomRuntime } from "../connection/runtime";
1010
import {
11+
EMPTY_PULL_REQUEST_STATS_CACHE,
1112
pullRequestListHead,
12-
pullRequestStatKey,
13+
withAnsweredPullRequestStats,
14+
withListedPullRequests,
1315
type PullRequestListPages,
16+
type PullRequestStatsCache,
1417
} from "../features/pull-requests/pullRequestList";
1518

1619
export const pullRequestListPagesKey = (target: {
@@ -25,7 +28,9 @@ export const pullRequestListPagesFamily = Atom.family((_key: string) =>
2528

2629
/** Diff stats each environment has answered so far, so rows keep them when they scroll away. */
2730
export const pullRequestListStatsFamily = Atom.family((_environmentId: string) =>
28-
Atom.make<ReadonlyMap<string, PullRequestDiffStat>>(new Map()).pipe(Atom.setIdleTTL(5 * 60_000)),
31+
Atom.make<PullRequestStatsCache>(EMPTY_PULL_REQUEST_STATS_CACHE).pipe(
32+
Atom.setIdleTTL(5 * 60_000),
33+
),
2934
);
3035

3136
export const composerPullRequests = createPullRequestEnvironmentAtoms(connectionAtomRuntime, {
@@ -38,20 +43,20 @@ export const composerPullRequests = createPullRequestEnvironmentAtoms(connection
3843
registry.refresh(composerPullRequests.filesViewed(target));
3944
}),
4045
list: (target, value, registry) =>
41-
target.input.cursors === undefined
42-
? Effect.sync(() =>
43-
registry.update(pullRequestListPagesFamily(pullRequestListPagesKey(target)), (previous) =>
44-
pullRequestListHead(previous, value),
45-
),
46-
)
47-
: Effect.void,
46+
Effect.sync(() => {
47+
registry.update(pullRequestListStatsFamily(target.environmentId), (previous) =>
48+
withListedPullRequests(previous, value.entries),
49+
);
50+
if (target.input.cursors === undefined)
51+
registry.update(pullRequestListPagesFamily(pullRequestListPagesKey(target)), (previous) =>
52+
pullRequestListHead(previous, value),
53+
);
54+
}),
4855
listStats: (target, value, registry) =>
4956
Effect.sync(() =>
50-
registry.update(pullRequestListStatsFamily(target.environmentId), (previous) => {
51-
const next = new Map(previous);
52-
for (const row of value.stats) next.set(pullRequestStatKey(row), row);
53-
return next;
54-
}),
57+
registry.update(pullRequestListStatsFamily(target.environmentId), (previous) =>
58+
withAnsweredPullRequestStats(previous, value.stats),
59+
),
5560
),
5661
});
5762
export const pullRequestStackAtom = createPullRequestStackAtomFamily(

0 commit comments

Comments
 (0)