Skip to content

Commit 354f6c8

Browse files
fix(mobile): pull request rows refresh their diff stats after a new push (#25713)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent ed44f25 commit 354f6c8

4 files changed

Lines changed: 128 additions & 16 deletions

File tree

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

Lines changed: 37 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,37 @@ 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 and refreshes its stats query when its updatedAt moves", () => {
452+
const query = {
453+
refs: [1, 2].map((number) => ({
454+
projectId: stat(number, 0).projectId,
455+
repository: "org/repo",
456+
number,
457+
})),
458+
};
459+
const listed = withListedPullRequests(EMPTY_PULL_REQUEST_STATS_CACHE, [entry(1), entry(2)]);
460+
expect(listed.refresh).toEqual([]);
461+
const answered = withAnsweredPullRequestStats(listed.cache, query, [stat(1, 5), stat(2, 7)]);
462+
expect(withListedPullRequests(answered, [entry(1), entry(2)])).toEqual({
463+
cache: answered,
464+
refresh: [],
465+
});
466+
467+
const pushed = withListedPullRequests(answered, [
468+
entry(1, { updatedAt: "2026-10-20T00:00:00Z" }),
469+
entry(2),
470+
]);
471+
expect([...pushed.cache.stats.values()]).toEqual([stat(2, 7)]);
472+
expect(pushed.refresh).toEqual([query]);
473+
});
474+
});

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type PullRequestListEntry,
88
type PullRequestListInput,
99
type PullRequestListResult,
10+
type PullRequestListStatsInput,
1011
} from "@t3tools/contracts";
1112
import type { PullRequestListControls } from "../../persistence/mobile-preferences";
1213

@@ -50,6 +51,71 @@ export const pullRequestStatKey = (
5051
row: Pick<PullRequestDiffStat, "projectId" | "repository" | "number">,
5152
) => `${row.projectId}:${row.repository}:${row.number}`;
5253

54+
/**
55+
* Diff stats an environment has answered, the stats query that answered each, and each row's
56+
* `updatedAt` as its list last answered it.
57+
*/
58+
export interface PullRequestStatsCache {
59+
readonly stats: ReadonlyMap<string, PullRequestDiffStat>;
60+
readonly answeredBy: ReadonlyMap<string, PullRequestListStatsInput>;
61+
readonly updatedAt: ReadonlyMap<string, string>;
62+
}
63+
64+
export const EMPTY_PULL_REQUEST_STATS_CACHE: PullRequestStatsCache = {
65+
stats: new Map(),
66+
answeredBy: new Map(),
67+
updatedAt: new Map(),
68+
};
69+
70+
/**
71+
* Records the rows a list answered. Stats carry no head, so a row whose `updatedAt` moved (a push,
72+
* among other changes) drops its cached stat, and the stats queries that answered it are returned
73+
* to refresh: one can still be fresh and would otherwise answer the row's next read with the old
74+
* counts.
75+
*/
76+
export function withListedPullRequests(
77+
cache: PullRequestStatsCache,
78+
entries: readonly Pick<
79+
PullRequestListEntry,
80+
"projectId" | "repository" | "number" | "updatedAt"
81+
>[],
82+
): {
83+
readonly cache: PullRequestStatsCache;
84+
readonly refresh: readonly PullRequestListStatsInput[];
85+
} {
86+
const moved = entries.filter(
87+
(entry) => cache.updatedAt.get(pullRequestStatKey(entry)) !== entry.updatedAt,
88+
);
89+
if (!moved.length) return { cache, refresh: [] };
90+
const stats = new Map(cache.stats);
91+
const answeredBy = new Map(cache.answeredBy);
92+
const updatedAt = new Map(cache.updatedAt);
93+
const refresh = new Set<PullRequestListStatsInput>();
94+
for (const entry of moved) {
95+
const key = pullRequestStatKey(entry);
96+
const query = answeredBy.get(key);
97+
if (query) refresh.add(query);
98+
stats.delete(key);
99+
answeredBy.delete(key);
100+
updatedAt.set(key, entry.updatedAt);
101+
}
102+
return { cache: { stats, answeredBy, updatedAt }, refresh: [...refresh] };
103+
}
104+
105+
export function withAnsweredPullRequestStats(
106+
cache: PullRequestStatsCache,
107+
input: PullRequestListStatsInput,
108+
answered: readonly PullRequestDiffStat[],
109+
): PullRequestStatsCache {
110+
const stats = new Map(cache.stats);
111+
const answeredBy = new Map(cache.answeredBy);
112+
for (const row of answered) {
113+
stats.set(pullRequestStatKey(row), row);
114+
answeredBy.set(pullRequestStatKey(row), input);
115+
}
116+
return { ...cache, stats, answeredBy };
117+
}
118+
53119
/**
54120
* Fills rows that have no diff stat yet from the stats answers on screen. Returns `entries`
55121
* 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: 24 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,24 @@ 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+
const statsAtom = pullRequestListStatsFamily(target.environmentId);
48+
const { cache, refresh } = withListedPullRequests(registry.get(statsAtom), value.entries);
49+
registry.set(statsAtom, cache);
50+
for (const input of refresh)
51+
registry.refresh(
52+
composerPullRequests.listStats({ environmentId: target.environmentId, input }),
53+
);
54+
if (target.input.cursors === undefined)
55+
registry.update(pullRequestListPagesFamily(pullRequestListPagesKey(target)), (previous) =>
56+
pullRequestListHead(previous, value),
57+
);
58+
}),
4859
listStats: (target, value, registry) =>
4960
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-
}),
61+
registry.update(pullRequestListStatsFamily(target.environmentId), (previous) =>
62+
withAnsweredPullRequestStats(previous, target.input, value.stats),
63+
),
5564
),
5665
});
5766
export const pullRequestStackAtom = createPullRequestStackAtomFamily(

0 commit comments

Comments
 (0)