@devries-cameron20 / t3code Mirror · read-only / commits / c8b3ebfdea
Fork 0

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 23, 06:45 UTC

perf(web): the changed files tree compiles without manual memoization (#20650)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 21:57 UTC c8b3ebfdea50371f0f5e361d96c303167d7089ce
1 file changed +23 -22
apps/web/src/components/chat/ChangedFilesTree.tsx
+23 -22
@@ -1,5 +1,5 @@
1 import { type RunId } from "@t3tools/contracts";
2 -import { type MouseEvent, memo, useCallback, useMemo, useState } from "react";
2 +import { type MouseEvent, memo, useState } from "react";
3 import { type TurnDiffFileChange } from "../../types";
4 import {
5 buildTurnDiffTree,
@@ -38,7 +38,7 @@ export const ChangedFilesCard = memo(function ChangedFilesCard(props: {
38 onOpenTurnDiff,
39 onFileContextMenu,
40 } = props;
41 - const summaryStat = useMemo(() => summarizeTurnDiffStats(files), [files]);
41 + const summaryStat = summarizeTurnDiffStats(files);
42 const hasDirectories = files.some((file) => /[/\\]/.test(file.path));
43
44 return (
@@ -122,6 +122,14 @@ export const ChangedFilesCard = memo(function ChangedFilesCard(props: {
122 );
123 });
124
125 +// Building the tree walks every file, and the compiler would redo it each render
126 +// because the expansion state hook runs after it.
127 +function useTurnDiffTree(files: ReadonlyArray<TurnDiffFileChange>) {
128 + "use memo";
129 + const treeNodes = buildTurnDiffTree(files);
130 + return { treeNodes, directoryPathsKey: collectDirectoryPaths(treeNodes).join("\u0000") };
131 +}
132 +
133 export const ChangedFilesTree = memo(function ChangedFilesTree(props: {
134 runId: RunId;
135 files: ReadonlyArray<TurnDiffFileChange>;
@@ -132,11 +140,7 @@ export const ChangedFilesTree = memo(function ChangedFilesTree(props: {
140 }) {
141 const { files, allDirectoriesExpanded, onOpenTurnDiff, resolvedTheme, runId, onFileContextMenu } =
142 props;
135 - const treeNodes = useMemo(() => buildTurnDiffTree(files), [files]);
136 - const directoryPathsKey = useMemo(
137 - () => collectDirectoryPaths(treeNodes).join("\u0000"),
138 - [treeNodes],
139 - );
143 + const { treeNodes, directoryPathsKey } = useTurnDiffTree(files);
144 const hasDirectoryNodes = directoryPathsKey.length > 0;
145 const expansionStateKey = `${allDirectoriesExpanded ? "expanded" : "collapsed"}\u0000${directoryPathsKey}`;
146 const [directoryExpansionState, setDirectoryExpansionState] = useState<{
@@ -151,21 +155,18 @@ export const ChangedFilesTree = memo(function ChangedFilesTree(props: {
155 ? directoryExpansionState.overrides
156 : EMPTY_DIRECTORY_OVERRIDES;
157
154 - const toggleDirectory = useCallback(
155 - (pathValue: string) => {
156 - setDirectoryExpansionState((current) => {
157 - const nextOverrides = current.key === expansionStateKey ? current.overrides : {};
158 - return {
159 - key: expansionStateKey,
160 - overrides: {
161 - ...nextOverrides,
162 - [pathValue]: !(nextOverrides[pathValue] ?? allDirectoriesExpanded),
163 - },
164 - };
165 - });
166 - },
167 - [allDirectoriesExpanded, expansionStateKey],
168 - );
158 + const toggleDirectory = (pathValue: string) => {
159 + setDirectoryExpansionState((current) => {
160 + const nextOverrides = current.key === expansionStateKey ? current.overrides : {};
161 + return {
162 + key: expansionStateKey,
163 + overrides: {
164 + ...nextOverrides,
165 + [pathValue]: !(nextOverrides[pathValue] ?? allDirectoriesExpanded),
166 + },
167 + };
168 + });
169 + };
170
171 const renderTreeNode = (node: TurnDiffTreeNode, depth: number) => {
172 const leftPadding = 8 + depth * 14;