Skip to content
TypeScript

perf(web): reuse the collator when sorting diff files (#18420)

Alex Southwell7ab28a3b2e88
diff --git a/apps/web/src/components/diffs/useReviewFilePatches.ts b/apps/web/src/components/diffs/useReviewFilePatches.ts
index ad36f75c595..2ac7593d41d 100644
--- a/apps/web/src/components/diffs/useReviewFilePatches.ts
+++ b/apps/web/src/components/diffs/useReviewFilePatches.ts
@@ -15,6 +15,11 @@ import {
import { getRenderablePatch, resolveFileDiffPath, type RenderablePatch } from "~/lib/diffRendering";
import { reviewEnvironment } from "~/state/review";
+const filePathCollator = new Intl.Collator(undefined, {
+ numeric: true,
+ sensitivity: "base",
+});
+
export function useReviewFilePatches({
environmentId,
cwd,
@@ -49,10 +54,7 @@ export function useReviewFilePatches({
[requested, scope],
);
const files = useMemo(
- () =>
- source?.files?.toSorted((a, b) =>
- a.path.localeCompare(b.path, undefined, { numeric: true, sensitivity: "base" }),
- ) ?? [],
+ () => source?.files?.toSorted((a, b) => filePathCollator.compare(a.path, b.path)) ?? [],
[source?.files],
);
const queries = useMemo(
@@ -191,10 +193,7 @@ export function useReviewFilePatches({
};
})
: (preview?.kind === "files" ? preview.files : []).toSorted((a, b) =>
- resolveFileDiffPath(a).localeCompare(resolveFileDiffPath(b), undefined, {
- numeric: true,
- sensitivity: "base",
- }),
+ filePathCollator.compare(resolveFileDiffPath(a), resolveFileDiffPath(b)),
),
[source, files, patches, scope, preview],
);