The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
feat(mobile): pull request files follow the same review order as web (#26760)
Julius Marminge committed
Nov 19, 2026 at 14:09 UTC
210996207d3c9327c1dcbb12cd958abdfa72e5ae
11 files changed
+198
-142
apps/mobile/src/features/pull-requests/PullRequestFiles.tsx
+1
-38
@@ -32,6 +32,7 @@ import {
32
import { usePullRequestFileFilter } from "./usePullRequestFileFilter";
33
import { usePullRequestFilesViewed } from "./usePullRequestFilesViewed";
34
import { requestPullRequestFileJump } from "./pullRequestFileJump";
35
+import { fileDirectory, groupByDirectory } from "./pullRequestFileSections";
36
import type { MessageResolver } from "../../i18n/messages";
37
38
const BREAKDOWN_LABELS = {
@@ -42,11 +43,6 @@ const BREAKDOWN_LABELS = {
43
docs: msg("Docs", { $context: "kind of changed file" }),
44
} satisfies Record<keyof NonNullable<PullRequestChangeBreakdown>, string>;
45
45
-function fileDirectory(path: string) {
46
- const separator = path.lastIndexOf("/");
47
- return separator < 0 ? "" : path.slice(0, separator);
48
-}
49
-
46
function useChangedFiles(params: PullRequestDiffParams) {
47
const detail = useEnvironmentQuery(composerPullRequests.detail(pullRequestTarget(params)));
48
// A commit's diff is part of the pull request, so the PR's file counts and viewed state don't apply.
@@ -375,39 +371,6 @@ export function usePullRequestFilesSection(params: PullRequestParams) {
371
};
372
}
373
378
-type SheetItem =
379
- | {
380
- readonly kind: "directory";
381
- readonly key: string;
382
- readonly directory: string;
383
- readonly collapsed: boolean;
384
- }
385
- | { readonly kind: "file"; readonly key: string; readonly file: ReviewRenderableFile };
386
-
387
-function groupByDirectory(
388
- files: ReadonlyArray<ReviewRenderableFile>,
389
- collapsed: ReadonlySet<string>,
390
-) {
391
- const sorted = files
392
- .map((file) => ({ file, directory: fileDirectory(file.path) }))
393
- .sort(
394
- (a, b) => a.directory.localeCompare(b.directory) || a.file.path.localeCompare(b.file.path),
395
- );
396
- const items: SheetItem[] = [];
397
- sorted.forEach(({ file, directory }, index) => {
398
- if (directory !== sorted[index - 1]?.directory) {
399
- items.push({
400
- kind: "directory",
401
- key: `directory:${directory}`,
402
- directory,
403
- collapsed: collapsed.has(directory),
404
- });
405
- }
406
- if (!collapsed.has(directory)) items.push({ kind: "file", key: file.id, file });
407
- });
408
- return items;
409
-}
410
-
374
function DirectoryRow(props: {
375
readonly directory: string;
376
readonly collapsed: boolean;
apps/mobile/src/features/pull-requests/pullRequestDiffPages.ts
+5
-1
@@ -1,3 +1,4 @@
1
+import { orderDiffFilesForReview } from "@t3tools/client-runtime/diff-file-order";
2
import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients";
3
import { changeRequestTermKind } from "../../lib/changeRequestTerm";
4
import type { PullRequestDiffResult } from "@t3tools/contracts";
@@ -118,7 +119,10 @@ export function combinePullRequestDiffPages(
119
cache.set(page.result, value);
120
return value;
121
});
121
- const files = parsed.flatMap((page) => (page.kind === "files" ? page.files : []));
122
+ const files = orderDiffFilesForReview(
123
+ parsed.flatMap((page) => (page.kind === "files" ? page.files : [])),
124
+ (file) => file.path,
125
+ );
126
if (files.length === 0) return parsed[0] ?? { kind: "empty" };
127
return {
128
kind: "files",
apps/mobile/src/features/pull-requests/pullRequestFileSections.test.ts
new
+22
@@ -0,0 +1,22 @@
1
+import { describe, expect, it } from "vite-plus/test";
2
+import { buildReviewParsedDiff } from "../review/reviewModel";
3
+import { groupByDirectory } from "./pullRequestFileSections";
4
+
5
+function patch(path: string) {
6
+ return `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n@@ -1 +1 @@\n-old\n+new\n`;
7
+}
8
+
9
+describe("groupByDirectory", () => {
10
+ it("keeps review order when a parent directory resumes after a subdirectory", () => {
11
+ const parsed = buildReviewParsedDiff(
12
+ ["src/a.ts", "src/lib/b.ts", "src/z.ts"].map(patch).join("\n"),
13
+ "pr",
14
+ );
15
+ if (parsed.kind !== "files") throw new Error("Expected files");
16
+ const items = groupByDirectory(parsed.files, new Set());
17
+ expect(
18
+ items.map((item) => (item.kind === "directory" ? `${item.directory}/` : item.file.path)),
19
+ ).toEqual(["src/", "src/a.ts", "src/lib/", "src/lib/b.ts", "src/", "src/z.ts"]);
20
+ expect(new Set(items.map((item) => item.key)).size).toBe(items.length);
21
+ });
22
+});
apps/mobile/src/features/pull-requests/pullRequestFileSections.ts
new
+37
@@ -0,0 +1,37 @@
1
+import type { ReviewRenderableFile } from "../review/reviewModel";
2
+
3
+export function fileDirectory(path: string) {
4
+ const separator = path.lastIndexOf("/");
5
+ return separator < 0 ? "" : path.slice(0, separator);
6
+}
7
+
8
+type SheetItem =
9
+ | {
10
+ readonly kind: "directory";
11
+ readonly key: string;
12
+ readonly directory: string;
13
+ readonly collapsed: boolean;
14
+ }
15
+ | { readonly kind: "file"; readonly key: string; readonly file: ReviewRenderableFile };
16
+
17
+export function groupByDirectory(
18
+ files: ReadonlyArray<ReviewRenderableFile>,
19
+ collapsed: ReadonlySet<string>,
20
+) {
21
+ // Keep the shared review order: a directory gets a new header each time it resumes after a subdirectory.
22
+ const items: SheetItem[] = [];
23
+ files.forEach((file, index) => {
24
+ const directory = fileDirectory(file.path);
25
+ const previous = files[index - 1];
26
+ if (!previous || directory !== fileDirectory(previous.path)) {
27
+ items.push({
28
+ kind: "directory",
29
+ key: `directory:${directory}:${index}`,
30
+ directory,
31
+ collapsed: collapsed.has(directory),
32
+ });
33
+ }
34
+ if (!collapsed.has(directory)) items.push({ kind: "file", key: file.id, file });
35
+ });
36
+ return items;
37
+}
apps/web/src/components/diffs/DiffFileTree.tsx
+1
-1
@@ -21,9 +21,9 @@ import {
21
compareDiffFileTreeEntries,
22
collectDirectoryPaths,
23
diffFileTreeModel,
24
- diffFileTreePositions,
24
type DiffFileTreeEntry,
25
} from "./diffFileTree.logic";
26
+import { diffFileTreePositions } from "@t3tools/client-runtime/diff-file-order";
27
28
export type { DiffFileTreeEntry } from "./diffFileTree.logic";
29
apps/web/src/components/diffs/diffFileTree.logic.test.ts
+1
-25
@@ -1,11 +1,7 @@
1
import type { FileDiffMetadata } from "@pierre/diffs";
2
import { describe, expect, it } from "vite-plus/test";
3
4
-import {
5
- buildDiffFileTreeUpdates,
6
- diffFileTreeEntries,
7
- orderDiffFilesLikeTree,
8
-} from "./diffFileTree.logic";
4
+import { buildDiffFileTreeUpdates, diffFileTreeEntries } from "./diffFileTree.logic";
5
6
function file(type: FileDiffMetadata["type"], name: string, prevName = name): FileDiffMetadata {
7
return { type, name, prevName } as FileDiffMetadata;
@@ -28,26 +24,6 @@ describe("diffFileTreeEntries", () => {
24
});
25
});
26
31
-describe("orderDiffFilesLikeTree", () => {
32
- it("pulls a test listed last into its directory, keeping the first-seen order", () => {
33
- expect(
34
- orderDiffFilesLikeTree([
35
- file("change", "server/device/Host.ts"),
36
- file("change", "web/settings.ts"),
37
- file("change", "server/ws.ts"),
38
- file("change", "server/device/Host.test.ts"),
39
- file("change", "web/settings.test.ts"),
40
- ]).map((entry) => entry.name),
41
- ).toEqual([
42
- "server/device/Host.ts",
43
- "server/device/Host.test.ts",
44
- "server/ws.ts",
45
- "web/settings.ts",
46
- "web/settings.test.ts",
47
- ]);
48
- });
49
-});
50
-
27
describe("buildDiffFileTreeUpdates", () => {
28
it("adds a new file's directories before the file", () => {
29
expect(buildDiffFileTreeUpdates(["README.md"], ["README.md", "src/lib/a.ts"])).toEqual([
apps/web/src/components/diffs/diffFileTree.logic.ts
-43
@@ -85,49 +85,6 @@ export function collectDirectoryPaths(paths: ReadonlyArray<string>): ReadonlyArr
85
return [...directories];
86
}
87
88
-export function diffFileTreePositions(paths: ReadonlyArray<string>): ReadonlyMap<string, number> {
89
- const positions = new Map<string, number>();
90
- paths.forEach((path, index) => {
91
- positions.set(path, index);
92
- let directory = "";
93
- for (const segment of path.split("/").slice(0, -1)) {
94
- directory += `${segment}/`;
95
- if (!positions.has(directory)) positions.set(directory, index);
96
- }
97
- });
98
- return positions;
99
-}
100
-
101
-// A flat list in tree order: each directory stays contiguous, siblings ranked as
102
-// compareDiffFileTreeEntries ranks them, so the list never disagrees with the tree.
103
-export function orderDiffFilesLikeTree(
104
- files: ReadonlyArray<FileDiffMetadata>,
105
-): ReadonlyArray<FileDiffMetadata> {
106
- const paths = files.map(resolveFileDiffPath);
107
- const positions = diffFileTreePositions(paths);
108
- const keys = paths.map((path) => {
109
- const key: number[] = [];
110
- let directory = "";
111
- for (const segment of path.split("/").slice(0, -1)) {
112
- directory += `${segment}/`;
113
- key.push(positions.get(directory)!);
114
- }
115
- key.push(positions.get(path)!);
116
- return key;
117
- });
118
- return files
119
- .map((file, index) => ({ file, key: keys[index]! }))
120
- .toSorted((left, right) => {
121
- const length = Math.min(left.key.length, right.key.length);
122
- for (let index = 0; index < length; index++) {
123
- const difference = left.key[index]! - right.key[index]!;
124
- if (difference !== 0) return difference;
125
- }
126
- return left.key.length - right.key.length;
127
- })
128
- .map(({ file }) => file);
129
-}
130
-
88
export function compareDiffFileTreeEntries(
89
getPositions: () => ReadonlyMap<string, number>,
90
): FileTreeSortComparator {
apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
+5
-6
@@ -81,8 +81,8 @@ import {
81
} from "@t3tools/client-runtime/pull-request-editing";
82
import { fileDiffRows } from "@t3tools/client-runtime/review-guide-tour";
83
import { isTestChangedFile } from "@t3tools/shared/changedFileKind";
84
+import { orderDiffFilesForReview } from "@t3tools/client-runtime/diff-file-order";
85
import { isWorkspaceImagePreviewPath } from "@t3tools/shared/mediaPreview";
85
-import { orderDiffFiles } from "./pullRequestFileOrder.logic";
86
import {
87
buildFileDiffIdentityKey,
88
buildFileDiffRenderKey,
@@ -116,7 +116,7 @@ import { DiffFileTree } from "../diffs/DiffFileTree";
116
import { FileExplorerPane } from "../files/FileExplorerPane";
117
import { useCodeViewFileReveal } from "../diffs/useCodeViewFileReveal";
118
import { useHideTestFiles } from "../diffs/useHideTestFiles";
119
-import { diffFileTreeEntries, orderDiffFilesLikeTree } from "../diffs/diffFileTree.logic";
119
+import { diffFileTreeEntries } from "../diffs/diffFileTree.logic";
120
import { StyledDiffCodeView } from "../diffs/StyledDiffCodeView";
121
import { Button } from "../ui/button";
122
import { Checkbox } from "../ui/checkbox";
@@ -261,10 +261,9 @@ function diffModel(
261
});
262
const model: DiffModel = {
263
parsedSlices,
264
- loadedFiles: orderDiffFilesLikeTree(
265
- parsedSlices.flatMap((parsed) =>
266
- parsed?.kind === "files" ? orderDiffFiles(parsed.files) : [],
267
- ),
264
+ loadedFiles: orderDiffFilesForReview(
265
+ parsedSlices.flatMap((parsed) => (parsed?.kind === "files" ? parsed.files : [])),
266
+ resolveFileDiffPath,
267
),
268
sourceFileStats: new Map(
269
parsedSlices.flatMap((parsed) =>
packages/client-runtime/package.json
+4
@@ -59,6 +59,10 @@
59
"types": "./src/imageAttachmentSize.ts",
60
"default": "./src/imageAttachmentSize.ts"
61
},
62
+ "./diff-file-order": {
63
+ "types": "./src/diffFileOrder.ts",
64
+ "default": "./src/diffFileOrder.ts"
65
+ },
66
"./handoff": {
67
"types": "./src/handoff.ts",
68
"default": "./src/handoff.ts"
packages/client-runtime/src/diffFileOrder.test.ts
renamed
+48
-8
@@ -1,17 +1,22 @@
1
-import type { FileDiffMetadata } from "@pierre/diffs";
1
import { describe, expect, it } from "vite-plus/test";
2
4
-import { orderDiffFiles } from "./pullRequestFileOrder.logic";
3
+import { orderDiffFilesForReview } from "./diffFileOrder.ts";
4
6
-function file(name: string, additionLines: ReadonlyArray<string> = []): FileDiffMetadata {
7
- return { name, hunks: [], additionLines, deletionLines: [] } as unknown as FileDiffMetadata;
5
+interface TestFile {
6
+ readonly name: string;
7
+ readonly additionLines: ReadonlyArray<string>;
8
+ readonly deletionLines: ReadonlyArray<string>;
9
}
10
10
-function order(files: ReadonlyArray<FileDiffMetadata>): Array<string> {
11
- return orderDiffFiles(files).map((entry) => entry.name);
11
+function file(name: string, additionLines: ReadonlyArray<string> = []): TestFile {
12
+ return { name, additionLines, deletionLines: [] };
13
}
14
14
-describe("orderDiffFiles", () => {
15
+function order(files: ReadonlyArray<TestFile>): Array<string> {
16
+ return orderDiffFilesForReview(files, (entry) => entry.name).map((entry) => entry.name);
17
+}
18
+
19
+describe("orderDiffFilesForReview", () => {
20
it("places source before tests and generated files across path conventions", () => {
21
const source = ["src/app.ts", "src/dist.ts", "src/testing.ts"];
22
const tests = [
@@ -33,7 +38,42 @@ describe("orderDiffFiles", () => {
38
];
39
expect(
40
order([...generated, ...tests, ...source].toReversed().map((path) => file(path))),
36
- ).toEqual([...source, ...tests, ...generated]);
41
+ ).toEqual([
42
+ ...source,
43
+ ...tests.slice(0, 3),
44
+ // Generated files under src/ stay in src/ so the list matches the tree.
45
+ "src/__snapshots__/app.ts",
46
+ "src/api.generated.ts",
47
+ "src/app.test.ts.snap",
48
+ ...tests.slice(3),
49
+ ...generated.slice(0, 5),
50
+ ]);
51
+ });
52
+
53
+ it("keeps both entries of a path that changed type, such as a file replaced by a symlink", () => {
54
+ const deleted = { ...file("AGENTS.md"), additionLines: [], deletionLines: ["# Agents"] };
55
+ const added = file("AGENTS.md", ["CLAUDE.md"]);
56
+ expect(
57
+ orderDiffFilesForReview([file("CLAUDE.md"), deleted, added], (entry) => entry.name),
58
+ ).toEqual([deleted, added, file("CLAUDE.md")]);
59
+ });
60
+
61
+ it("pulls a test listed last into its directory, keeping the first-seen order", () => {
62
+ expect(
63
+ order([
64
+ file("server/device/Host.ts"),
65
+ file("web/settings.ts"),
66
+ file("server/ws.ts"),
67
+ file("server/device/Host.test.ts"),
68
+ file("web/settings.test.ts"),
69
+ ]),
70
+ ).toEqual([
71
+ "server/device/Host.ts",
72
+ "server/device/Host.test.ts",
73
+ "server/ws.ts",
74
+ "web/settings.ts",
75
+ "web/settings.test.ts",
76
+ ]);
77
});
78
79
it("resolves a specifier that climbs out of the importer's directory", () => {
packages/client-runtime/src/diffFileOrder.ts
renamed
+74
-20
@@ -1,8 +1,9 @@
1
-import type { FileDiffMetadata } from "@pierre/diffs";
2
-
3
-import { resolveFileDiffPath } from "~/lib/diffRendering";
1
+interface OrderableDiffFile {
2
+ readonly additionLines: ReadonlyArray<string>;
3
+ readonly deletionLines: ReadonlyArray<string>;
4
+}
5
5
-export type DiffFileTier = "source" | "test" | "generated";
6
+type DiffFileTier = "source" | "test" | "generated";
7
8
const GENERATED_FILE_NAMES = new Set([
9
"pnpm-lock.yaml",
@@ -139,11 +140,16 @@ function testedSourcePosition(path: string, sourcePositions: ReadonlyMap<string,
140
}
141
}
142
142
-export function orderDiffFiles(
143
- files: ReadonlyArray<FileDiffMetadata>,
144
-): ReadonlyArray<FileDiffMetadata> {
145
- const byPath = new Map<string, FileDiffMetadata>();
146
- for (const file of files) byPath.set(resolveFileDiffPath(file), file);
143
+function orderByImportsAndTier<F extends OrderableDiffFile>(
144
+ files: ReadonlyArray<F>,
145
+ pathOf: (file: F) => string,
146
+): ReadonlyArray<F> {
147
+ // A type change (file to symlink) is a delete and an add under one path; keep both.
148
+ const byPath = new Map<string, F[]>();
149
+ for (const file of files) {
150
+ const path = pathOf(file);
151
+ byPath.set(path, [...(byPath.get(path) ?? []), file]);
152
+ }
153
const tiers = new Map<string, DiffFileTier>();
154
for (const path of byPath.keys()) tiers.set(path, diffFileTier(path));
155
@@ -161,16 +167,10 @@ export function orderDiffFiles(
167
168
const imports = new Map<string, ReadonlySet<string>>();
169
for (const path of sourcePaths) {
164
- const file = byPath.get(path)!;
165
- imports.set(
166
- path,
167
- importedPaths(
168
- path,
169
- [...file.additionLines, ...file.deletionLines],
170
- byModulePath,
171
- byModuleName,
172
- ),
173
- );
170
+ const lines = byPath
171
+ .get(path)!
172
+ .flatMap((file) => [...file.additionLines, ...file.deletionLines]);
173
+ imports.set(path, importedPaths(path, lines, byModulePath, byModuleName));
174
}
175
const orderedSource = orderByImports(sourcePaths, imports);
176
@@ -192,5 +192,59 @@ export function orderDiffFiles(
192
.filter((path) => tiers.get(path) === "generated")
193
.sort((left, right) => left.localeCompare(right));
194
195
- return [...orderedSource, ...orderedTests, ...orderedGenerated].map((path) => byPath.get(path)!);
195
+ return [...orderedSource, ...orderedTests, ...orderedGenerated].flatMap((path) =>
196
+ byPath.get(path)!,
197
+ );
198
+}
199
+
200
+/** Where each file, and each directory (with a trailing slash), first appears in `paths`. */
201
+export function diffFileTreePositions(paths: ReadonlyArray<string>): ReadonlyMap<string, number> {
202
+ const positions = new Map<string, number>();
203
+ paths.forEach((path, index) => {
204
+ positions.set(path, index);
205
+ let directory = "";
206
+ for (const segment of path.split("/").slice(0, -1)) {
207
+ directory += `${segment}/`;
208
+ if (!positions.has(directory)) positions.set(directory, index);
209
+ }
210
+ });
211
+ return positions;
212
+}
213
+
214
+function orderLikeTree<F>(files: ReadonlyArray<F>, pathOf: (file: F) => string): ReadonlyArray<F> {
215
+ const paths = files.map(pathOf);
216
+ const positions = diffFileTreePositions(paths);
217
+ const keys = paths.map((path) => {
218
+ const key: number[] = [];
219
+ let directory = "";
220
+ for (const segment of path.split("/").slice(0, -1)) {
221
+ directory += `${segment}/`;
222
+ key.push(positions.get(directory)!);
223
+ }
224
+ key.push(positions.get(path)!);
225
+ return key;
226
+ });
227
+ return files
228
+ .map((file, index) => ({ file, key: keys[index]! }))
229
+ .sort((left, right) => {
230
+ const length = Math.min(left.key.length, right.key.length);
231
+ for (let index = 0; index < length; index++) {
232
+ const difference = left.key[index]! - right.key[index]!;
233
+ if (difference !== 0) return difference;
234
+ }
235
+ return left.key.length - right.key.length;
236
+ })
237
+ .map(({ file }) => file);
238
+}
239
+
240
+/**
241
+ * Review order shared by every client: dependencies before dependents, tests after their source,
242
+ * generated files last, then regrouped so each directory stays contiguous and a file tree built
243
+ * from `diffFileTreePositions` lists files in the same order.
244
+ */
245
+export function orderDiffFilesForReview<F extends OrderableDiffFile>(
246
+ files: ReadonlyArray<F>,
247
+ pathOf: (file: F) => string,
248
+): ReadonlyArray<F> {
249
+ return orderLikeTree(orderByImportsAndTier(files, pathOf), pathOf);
250
}