Skip to content
TypeScript

perf(web): the diff file tree compiles with the React Compiler (#20631)

Julius Marminge1b41060585be
diff --git a/apps/web/src/components/diffs/DiffFileTree.tsx b/apps/web/src/components/diffs/DiffFileTree.tsx
index 01c8d3f4643..59b49647dbe 100644
--- a/apps/web/src/components/diffs/DiffFileTree.tsx
+++ b/apps/web/src/components/diffs/DiffFileTree.tsx
@@ -1,8 +1,9 @@
import type { GitStatusEntry } from "@pierre/trees";
import { FileTree, useFileTree, useFileTreeSelector } from "@pierre/trees/react";
import { ChevronsDownUp, ChevronsUpDown } from "lucide";
-import { useEffect, useMemo, useRef, useState, type ReactNode } from "react";
+import { useEffect, useRef, useState, type ReactNode } from "react";
+import { useStableCallback } from "~/hooks/useStableCallback";
import { useTheme } from "~/hooks/useTheme";
import { cn } from "~/lib/utils";
import { T3_PIERRE_ICONS } from "~/pierre-icons";
@@ -44,9 +45,9 @@ export function DiffFileTree({
className,
}: DiffFileTreeProps) {
const { resolvedTheme } = useTheme();
- const paths = useMemo(() => entries.map((entry) => entry.path), [entries]);
- const directoryPaths = useMemo(() => collectDirectoryPaths(paths), [paths]);
- const positions = useMemo(() => diffFileTreePositions(paths), [paths]);
+ const paths = entries.map((entry) => entry.path);
+ const directoryPaths = collectDirectoryPaths(paths);
+ const positions = diffFileTreePositions(paths);
const [ordering] = useState(() => {
let currentPositions: ReadonlyMap<string, number> = new Map();
return {
@@ -56,31 +57,27 @@ export function DiffFileTree({
},
};
});
- const gitStatus = useMemo<ReadonlyArray<GitStatusEntry>>(
- () => entries.map((entry) => ({ path: entry.path, status: entry.status })),
- [entries],
- );
- const filePathsRef = useRef<ReadonlySet<string>>(new Set(paths));
- const onSelectFileRef = useRef(onSelectFile);
+ const gitStatus: ReadonlyArray<GitStatusEntry> = entries.map((entry) => ({
+ path: entry.path,
+ status: entry.status,
+ }));
const syncingSelectionRef = useRef(false);
const handledRevealRef = useRef<{ path: string; revealRequestId: number } | null>(null);
const mountedPathsRef = useRef<ReadonlyArray<string> | null>(null);
- useEffect(() => {
- filePathsRef.current = new Set(paths);
- onSelectFileRef.current = onSelectFile;
- }, [onSelectFile, paths]);
+ // The tree model keeps the first selection handler it was given.
+ const handleSelectionChange = useStableCallback((selectedPaths: ReadonlyArray<string>) => {
+ if (syncingSelectionRef.current) return;
+ const path = selectedPaths.at(-1)?.replace(/\/$/, "");
+ if (path && paths.includes(path)) onSelectFile(path);
+ });
const { model } = useFileTree({
density: "compact",
flattenEmptyDirectories: true,
initialExpansion: "open",
icons: T3_PIERRE_ICONS,
- onSelectionChange: (selectedPaths) => {
- if (syncingSelectionRef.current) return;
- const path = selectedPaths.at(-1)?.replace(/\/$/, "");
- if (path && filePathsRef.current.has(path)) onSelectFileRef.current(path);
- },
+ onSelectionChange: handleSelectionChange,
paths: [],
search: false,
sort: ordering.sort,
@@ -144,8 +141,13 @@ export function DiffFileTree({
queueMicrotask(() => {
syncingSelectionRef.current = false;
});
- // `paths` is a dependency so a file that arrives after it was asked for is still revealed.
- }, [model, paths, revealRequestId, selectedPath]);
+ }, [
+ model,
+ // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A file that arrives after it was asked for is still revealed.
+ paths,
+ revealRequestId,
+ selectedPath,
+ ]);
return (
<div className={cn("flex min-h-0 flex-1 flex-col bg-background", className)}>
@@ -202,13 +204,13 @@ export function DiffFileTree({
// Read selection before the row handles the click so new selections reveal only once.
const selected = model.getSelectedPaths();
const path = selected.length === 1 ? selected[0] : undefined;
- if (!path || !filePathsRef.current.has(path)) return;
+ if (!path || !paths.includes(path)) return;
const clickedSelectedRow = event.nativeEvent
.composedPath()
.some(
(node) => node instanceof HTMLElement && node.getAttribute("data-item-path") === path,
);
- if (clickedSelectedRow) onSelectFileRef.current(path);
+ if (clickedSelectedRow) onSelectFile(path);
}}
className="min-h-0 flex-1 overflow-hidden"
style={pierreTreeStyle(resolvedTheme)}