Skip to content

Commit 0ea3c02

Browse files
Bil0000claudejuliusmarminge
authored
perf(pr): pull requests paint from data already held, on web, desktop and mobile (pingdotgg#27798)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 2d0efd3 commit 0ea3c02

28 files changed

Lines changed: 958 additions & 96 deletions

‎apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import type { ThreadListFilters } from "../../persistence/mobile-preferences";
5858
import { appAtomRegistry } from "../../state/atom-registry";
5959
import { useProjects } from "../../state/entities";
6060
import { environmentThreadShells } from "../../state/threads";
61+
import { withKnownPullRequestSummary } from "../../state/use-thread-pr";
6162
import { mobilePreferencesAtom } from "../../state/preferences";
6263
import {
6364
DEFAULT_MOBILE_PROJECT_GROUPING_SETTINGS,
@@ -589,6 +590,7 @@ function AdaptiveWorkspaceLayoutContent(
589590
columns: workspaceTabColumns(Platform.OS, tab, options?.columns ?? layout.usesSplitView),
590591
check: workspaceSelectionCheck,
591592
overlays,
593+
seedPullRequest: withKnownPullRequestSummary,
592594
},
593595
);
594596
if (layout.variant === "medium") {

‎apps/mobile/src/features/pull-requests/PullRequestOverviewLoading.tsx‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Chip } from "../../components/ui/Chip";
22
import { PullRequestSectionHeading } from "./PullRequestSectionHeading";
3-
import type { PullRequestListEntry } from "@t3tools/contracts";
3+
import type { PullRequestListEntry, PullRequestSummary } from "@t3tools/contracts";
44
import { View } from "react-native";
55
import { PullRequestCardGroup, PullRequestCardRow } from "./PullRequestControls";
66
import { PullRequestStackCard } from "./PullRequestStackCard";
@@ -14,11 +14,14 @@ export function PullRequestOverviewLoading({
1414
params,
1515
stack,
1616
}: {
17-
readonly summary: PullRequestListEntry;
17+
readonly summary: PullRequestListEntry | PullRequestSummary;
1818
readonly params: PullRequestParams;
1919
readonly stack: PullRequestStackState;
2020
}) {
21-
const review = pullRequestListReviewStatus(summary);
21+
const review = pullRequestListReviewStatus({
22+
reviewDecision: summary.reviewDecision ?? undefined,
23+
viewerReviewRequested: "viewerReviewRequested" in summary && summary.viewerReviewRequested,
24+
});
2225
const conflicting = summary.mergeability === "conflicting";
2326
const clean = summary.mergeability === "mergeable";
2427
return (
@@ -89,7 +92,7 @@ export function PullRequestOverviewLoading({
8992
<View className="h-11 rounded-xl bg-subtle-strong" />
9093
</View>
9194
</PullRequestCardGroup>
92-
{summary.labels.length ? (
95+
{"labels" in summary && summary.labels.length ? (
9396
<View className="flex-row flex-wrap gap-2">
9497
{summary.labels.map((label) => (
9598
<Chip key={label.name} label={label.name} size="small" />

‎apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ import { useFocusEffect, useNavigation, type StaticScreenProps } from "@react-na
4747
import type { NativeStackNavigationProp } from "@react-navigation/native-stack";
4848
import { RegistryContext, useAtomRefresh, useAtomValue } from "@effect/atom-react";
4949
import { pullRequestReviewKey } from "./pullRequestReviewDrafts";
50-
import { type PullRequestDetail, type PullRequestListEntry } from "@t3tools/contracts";
50+
import {
51+
type PullRequestDetail,
52+
type PullRequestListEntry,
53+
type PullRequestSummary,
54+
} from "@t3tools/contracts";
5155
import { PullRequestOverviewLoading } from "./PullRequestOverviewLoading";
5256
import { copyPullRequestLink, sharePullRequestLink } from "./pullRequestLink";
5357
import { pullRequestStatus } from "./pullRequestStatus";
@@ -180,11 +184,11 @@ function usePullRequestActivityRows(params: PullRequestParams, filter: ActivityF
180184
}
181185

182186
function PullRequestIdentity(props: {
183-
readonly pr: PullRequestDetail | PullRequestListEntry;
187+
readonly pr: PullRequestDetail | PullRequestListEntry | PullRequestSummary;
184188
readonly params: PullRequestParams;
185189
}) {
186190
const { pr } = props;
187-
const status = pullRequestStatus(pr);
191+
const status = pullRequestStatus({ state: pr.state, isDraft: pr.isDraft === true });
188192
return (
189193
<View className="gap-3">
190194
<View className="flex-row flex-wrap items-center gap-2">
@@ -196,8 +200,8 @@ function PullRequestIdentity(props: {
196200
<Text className={`text-xs font-t3-medium ${status.text}`}>{status.label}</Text>
197201
</View>
198202
)}
199-
<PullRequestDiffStats additions={pr.additions} deletions={pr.deletions} compact />
200-
{"changedFiles" in pr ? (
203+
<PullRequestDiffStats additions={pr.additions ?? 0} deletions={pr.deletions ?? 0} compact />
204+
{"changedFiles" in pr && pr.changedFiles !== undefined ? (
201205
<Text className="text-2xs text-foreground-muted">
202206
{pr.changedFiles} {pr.changedFiles === 1 ? "file" : "files"}
203207
</Text>
@@ -209,7 +213,7 @@ function PullRequestIdentity(props: {
209213
{pr.title}
210214
</Text>
211215
<View className="flex-row items-center gap-2">
212-
<PullRequestAvatar actor={pr.author} size={20} />
216+
<PullRequestAvatar actor={pr.author ?? null} size={20} />
213217
<Text className="min-w-0 flex-1 text-xs font-t3-medium" numberOfLines={1}>
214218
{pr.author?.login ?? "Unknown author"}
215219
</Text>

‎apps/mobile/src/features/pull-requests/PullRequestStackCard.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { pullRequestStatus } from "./pullRequestStatus";
99
import type { PullRequestParams } from "./usePullRequestDiff";
1010
import type { PullRequestStackState } from "./usePullRequestStack";
1111
import { PullRequestGlyph } from "./pullRequestGlyphs";
12+
import { withKnownPullRequestSummary } from "../../state/use-thread-pr";
1213

1314
export function PullRequestStackCard(props: {
1415
readonly params: PullRequestParams;
@@ -67,7 +68,9 @@ export function PullRequestStackCard(props: {
6768
selected
6869
? undefined
6970
: () => {
70-
const params = pullRequestLayerParams(props.params, layer.number);
71+
const params = withKnownPullRequestSummary(
72+
pullRequestLayerParams(props.params, layer.number),
73+
);
7174
if (Platform.OS === "android")
7275
selectDetail({ name: "PullRequest", params });
7376
else navigation.dispatch(StackActions.push("PullRequest", params));

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients";
2-
import { EnvironmentId, ProjectId, type PullRequestListEntry } from "@t3tools/contracts";
2+
import {
3+
EnvironmentId,
4+
ProjectId,
5+
type PullRequestListEntry,
6+
type PullRequestSummary,
7+
} from "@t3tools/contracts";
38
import { useAtomRefresh } from "@effect/atom-react";
49
import { useLayoutEffect, useMemo, useState } from "react";
510

@@ -18,7 +23,7 @@ export type PullRequestParams = {
1823
readonly repository: string;
1924
readonly number: number;
2025
readonly host?: string;
21-
readonly initialSummary?: PullRequestListEntry;
26+
readonly initialSummary?: PullRequestListEntry | PullRequestSummary;
2227
};
2328

2429
export type PullRequestDiffParams = PullRequestParams & { readonly commit?: string };

‎apps/mobile/src/features/workspace/workspace-tab-navigation.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ function detailRoute(
150150
tab: "threads" | "pullRequests",
151151
selections: WorkspaceSelections,
152152
check: WorkspaceSelectionCheck,
153+
seedPullRequest?: (identity: Omit<PullRequestSelection, "key">) => object,
153154
): RouteLike | undefined {
154155
if (tab === "threads") {
155156
const draft = selections.threadsDraft;
@@ -184,7 +185,11 @@ function detailRoute(
184185
(route) =>
185186
route.name === "PullRequest" &&
186187
samePullRequest(pullRequestSelection(route.params), pullRequest),
187-
) ?? { key: pullRequest.key, name: "PullRequest", params: identityOf(pullRequest) }
188+
) ?? {
189+
key: pullRequest.key,
190+
name: "PullRequest",
191+
params: seedPullRequest?.(identityOf(pullRequest)) ?? identityOf(pullRequest),
192+
}
188193
);
189194
}
190195

@@ -204,6 +209,7 @@ export function workspaceTabNavigationState<
204209
readonly columns: boolean;
205210
readonly check: WorkspaceSelectionCheck;
206211
readonly overlays?: readonly RouteLike[];
212+
readonly seedPullRequest?: (identity: Omit<PullRequestSelection, "key">) => object;
207213
},
208214
) {
209215
const overlays = options.overlays ?? [];
@@ -221,7 +227,7 @@ export function workspaceTabNavigationState<
221227
const home = state.routes.find((route) => route.name === "Home") ?? { name: "Home" };
222228
const detail =
223229
options.columns && tab !== "usage"
224-
? detailRoute(state, tab, selections, options.check)
230+
? detailRoute(state, tab, selections, options.check, options.seedPullRequest)
225231
: undefined;
226232
const routes = [...[home, ...(detail ? [detail] : [])].map(keyed), ...overlays];
227233
return { ...state, index: routes.length - 1, routes };

‎apps/mobile/src/native/useOpenPullRequest.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useProjects, useThreadShell } from "../state/entities";
77
import { tryOpenExternalUrl } from "../lib/openExternalUrl";
88
import { appAtomRegistry } from "../state/atom-registry";
99
import { environmentServerConfigsAtom } from "../state/server";
10+
import { withKnownPullRequestSummary } from "../state/use-thread-pr";
1011

1112
type PullRequestContext = { readonly environmentId: string; readonly projectId: string };
1213

@@ -19,10 +20,11 @@ export function useNavigateToPullRequest(options?: { readonly replace?: boolean
1920
return async (url: string, context: PullRequestContext | null) => {
2021
// Read at press time so a config push does not hand every link a new handler.
2122
const serverConfigs = appAtomRegistry.get(environmentServerConfigsAtom);
22-
const params = nativePullRequestDestination(url, context, projects, (environmentId) =>
23+
const destination = nativePullRequestDestination(url, context, projects, (environmentId) =>
2324
serverConfigs.has(environmentId),
2425
);
25-
if (!params) return tryOpenExternalUrl(url, "pull-request");
26+
if (!destination) return tryOpenExternalUrl(url, "pull-request");
27+
const params = withKnownPullRequestSummary(destination);
2628
// Android shows one page at a time; iOS opens it over the current page, so Back returns.
2729
if (Platform.OS === "android") selectDetail({ name: "PullRequest", params });
2830
else if (replace) navigation.dispatch(StackActions.replace("PullRequest", params));

‎apps/mobile/src/state/thread-pr-presentation.ts‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2-
import type { ThreadPullRequestLink, VcsStatusResult } from "@t3tools/contracts";
2+
import type {
3+
PullRequestSummary,
4+
ThreadPullRequestKey,
5+
ThreadPullRequestLink,
6+
VcsStatusResult,
7+
} from "@t3tools/contracts";
38
import {
49
sourceControlClients,
510
type ChangeRequestTerminology,
611
} from "@t3tools/client-runtime/source-control-clients";
712

813
import {
14+
legacyThreadPullRequestKey,
915
resolveThreadCurrentPullRequestLink,
1016
resolveThreadPullRequestBadge,
17+
threadPullRequestKeyOf,
1118
} from "@t3tools/client-runtime/thread-pull-requests";
1219

1320
export type ThreadPr = NonNullable<VcsStatusResult["pr"]>;
@@ -142,3 +149,53 @@ export function rememberBounded<V>(
142149
}
143150
return next;
144151
}
152+
153+
/** The newest summary held for `pullRequest`, matched strictly by host, repository and number. */
154+
export function knownPullRequestSummary(
155+
pullRequest: ThreadPullRequestKey,
156+
threads: Iterable<Pick<EnvironmentThreadShell, "projectId" | "pullRequests">>,
157+
summaries: Iterable<PullRequestSummary>,
158+
): PullRequestSummary | undefined {
159+
const key = threadPullRequestKeyOf(pullRequest);
160+
let newest:
161+
| { readonly summary: PullRequestSummary; readonly updatedAt: number; readonly readAt: number }
162+
| undefined;
163+
// Checks and reviews change without moving `updatedAt`, so equal versions prefer the later read.
164+
const consider = (summary: PullRequestSummary, readAt = Number.NEGATIVE_INFINITY) => {
165+
const updatedAt = Date.parse(summary.updatedAt);
166+
if (
167+
newest === undefined ||
168+
updatedAt > newest.updatedAt ||
169+
(updatedAt === newest.updatedAt && readAt > newest.readAt)
170+
) {
171+
newest = { summary, updatedAt, readAt };
172+
}
173+
};
174+
for (const summary of summaries) {
175+
if (threadPullRequestKeyOf(legacyThreadPullRequestKey(summary)) === key) {
176+
consider(summary, summary.observedAt);
177+
}
178+
}
179+
for (const thread of threads) {
180+
for (const link of thread.pullRequests) {
181+
const snapshot = link.snapshot;
182+
if (snapshot?.updatedAt == null || threadPullRequestKeyOf(link) !== key) continue;
183+
consider(
184+
{
185+
...snapshot,
186+
updatedAt: snapshot.updatedAt,
187+
provider: (
188+
sourceControlClients.findByChangeRequestUrl(link.url) ??
189+
sourceControlClients.get(undefined)
190+
).kind,
191+
projectId: thread.projectId,
192+
repository: link.repository,
193+
number: link.number,
194+
url: link.url,
195+
},
196+
Date.parse(snapshot.syncedAt),
197+
);
198+
}
199+
}
200+
return newest?.summary;
201+
}

‎apps/mobile/src/state/use-thread-pr.test.ts‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { ProjectId, type ThreadPullRequestLink } from "@t3tools/contracts";
1+
import {
2+
ProjectId,
3+
type PullRequestSummary,
4+
SourceControlProviderKind,
5+
type ThreadPullRequestLink,
6+
} from "@t3tools/contracts";
27
import { describe, expect, it } from "vite-plus/test";
38

49
import {
10+
knownPullRequestSummary,
511
presentThreadLinkedPullRequests,
612
rememberBounded,
713
resolveThreadPrSource,
@@ -113,6 +119,49 @@ describe("resolveThreadPrSource compatibility", () => {
113119
});
114120
});
115121

122+
describe("knownPullRequestSummary", () => {
123+
it("seeds from the newest summary of the same pull request, never another's", () => {
124+
const projectId = ProjectId.make("project");
125+
const link = linkedPr(7);
126+
const threads = [
127+
{
128+
projectId,
129+
pullRequests: [
130+
{ ...link, snapshot: { ...link.snapshot!, updatedAt: "2026-09-08T00:00:00.000Z" } },
131+
],
132+
},
133+
];
134+
const listed: PullRequestSummary = {
135+
provider: SourceControlProviderKind.make("github"),
136+
projectId,
137+
repository: "t3tools/t3code",
138+
number: 7,
139+
title: "Listed title",
140+
url: "https://github.com/t3tools/t3code/pull/7",
141+
state: "open",
142+
headBranch: "change-7",
143+
baseBranch: "main",
144+
updatedAt: "2026-09-09T00:00:00.000Z",
145+
};
146+
const lookup = (host: string, number: number, summaries = [listed]) =>
147+
knownPullRequestSummary({ host, repository: "t3tools/t3code", number }, threads, summaries);
148+
149+
expect(lookup("github.com", 7, [])?.title).toBe("Change 7");
150+
expect(lookup("github.com", 7)?.title).toBe("Listed title");
151+
expect(lookup("github.com", 8)).toBeUndefined();
152+
expect(lookup("gitlab.com", 7)).toBeUndefined();
153+
154+
const sameVersion = { ...listed, updatedAt: "2026-09-08T00:00:00.000Z" };
155+
const syncedAt = Date.parse(link.snapshot!.syncedAt);
156+
expect(lookup("github.com", 7, [{ ...sameVersion, observedAt: syncedAt - 1 }])?.title).toBe(
157+
"Change 7",
158+
);
159+
expect(lookup("github.com", 7, [{ ...sameVersion, observedAt: syncedAt + 1 }])?.title).toBe(
160+
"Listed title",
161+
);
162+
});
163+
});
164+
116165
describe("rememberBounded", () => {
117166
it("moves a re-answered entry to the newest slot and evicts the oldest past the limit", () => {
118167
let snapshots: ReadonlyMap<string, number> = new Map();

‎apps/mobile/src/state/use-thread-pr.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import { Atom } from "effect/reactivity";
1010
import { use, useMemo } from "react";
1111

1212
import { connectionAtomRuntime } from "../connection/runtime";
13+
import { appAtomRegistry } from "./atom-registry";
1314
import { serverEnvironment } from "./server";
15+
import { environmentThreadShells } from "./threads";
1416
import { useEnvironmentQuery } from "./query";
1517
import { ThreadPrDemandContext } from "./thread-pr-demand";
1618
import {
19+
knownPullRequestSummary,
1720
rememberBounded,
1821
resolveThreadPrSource,
1922
presentThreadPr,
@@ -59,6 +62,21 @@ const pullRequestSummaryAtom = createLinkedPullRequestSummaryAtomFamily(
5962

6063
export { type ThreadPr, type ThreadPrPresentation } from "./thread-pr-presentation";
6164

65+
/** Seeds a pull request page with a summary already in memory, never another pull request's. */
66+
export function withKnownPullRequestSummary<
67+
Params extends { readonly host?: string; readonly repository: string; readonly number: number },
68+
>(params: Params) {
69+
const initialSummary =
70+
params.host === undefined
71+
? undefined
72+
: knownPullRequestSummary(
73+
{ host: params.host, repository: params.repository, number: params.number },
74+
appAtomRegistry.get(environmentThreadShells.threadShellsAtom),
75+
appAtomRegistry.get(pullRequestSnapshotsAtom).values(),
76+
);
77+
return initialSummary === undefined ? params : { ...params, initialSummary };
78+
}
79+
6280
export function useThreadPr(thread: EnvironmentThreadShell): ThreadPrPresentation | null {
6381
const demand = use(ThreadPrDemandContext);
6482
const supportsLinks = useAtomValue(

0 commit comments

Comments
 (0)