Skip to content

Commit 4654bb5

Browse files
perf(web): project content search compiles with the React Compiler
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a702563 commit 4654bb5

2 files changed

Lines changed: 28 additions & 26 deletions

File tree

‎apps/web/src/components/search/ProjectContentSearchDialog.test.tsx‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ const state = vi.hoisted(() => ({
1010
error: null as string | null,
1111
readScope: vi.fn(),
1212
openFile: vi.fn(),
13+
// The real query hands back the same result array until the search changes.
14+
matches: [
15+
{
16+
resultIndex: 0,
17+
path: "src/index.ts",
18+
lineNumber: 3,
19+
lineContent: "match",
20+
matchRanges: [],
21+
},
22+
],
23+
noMatches: [],
1324
}));
1425

1526
const target = {
@@ -37,18 +48,7 @@ vi.mock("~/state/queries", () => ({
3748
hasQuery: query.length > 0,
3849
truncated: false,
3950
invalidRegex: false,
40-
matches:
41-
state.canReadFiles && query.length > 0
42-
? [
43-
{
44-
resultIndex: 0,
45-
path: "src/index.ts",
46-
lineNumber: 3,
47-
lineContent: "match",
48-
matchRanges: [],
49-
},
50-
]
51-
: [],
51+
matches: state.canReadFiles && query.length > 0 ? state.matches : state.noMatches,
5252
}),
5353
}));
5454
vi.mock("../CommandPaletteContent", () => ({ CommandPaletteContent: "section" }));

‎apps/web/src/components/search/ProjectContentSearchDialog.tsx‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Spinner } from "~/components/ui/spinner";
22
import { AuthFilesystemReadScope, type ProjectContentMatch } from "@t3tools/contracts";
33

4-
import { useCallback, useEffect, useMemo, useState, type ReactNode } from "react";
4+
import { useEffect, useState, type ReactNode } from "react";
55

66
import { useActiveProjectTarget, type ActiveProjectTarget } from "~/hooks/useActiveProjectTarget";
77
import { useTheme } from "~/hooks/useTheme";
@@ -124,25 +124,27 @@ function OpenContentSearchDialog(props: {
124124
});
125125
const canOpenMatches = search.canReadFiles && !search.isPending && search.error === null;
126126
const matches = search.matches;
127-
const visibleMatches = useMemo(() => matches.slice(0, visibleCount), [matches, visibleCount]);
128-
const groups = useMemo(() => groupMatches(visibleMatches), [visibleMatches]);
129-
130-
useEffect(() => {
127+
// New results restart the selection and the rendered window.
128+
const [resultsFor, setResultsFor] = useState(matches);
129+
if (resultsFor !== matches) {
130+
setResultsFor(matches);
131131
setSelectedIndex(0);
132132
setVisibleCount(VISIBLE_MATCH_WINDOW);
133-
}, [matches]);
133+
}
134+
// Keyboard selection past the window grows it before the row has to scroll into view.
135+
if (selectedIndex >= visibleCount) {
136+
setVisibleCount(selectedIndex + VISIBLE_MATCH_WINDOW);
137+
}
138+
const visibleMatches = matches.slice(0, visibleCount);
139+
const groups = groupMatches(visibleMatches);
134140

135141
useEffect(() => {
136-
if (selectedIndex >= visibleCount) {
137-
setVisibleCount(selectedIndex + VISIBLE_MATCH_WINDOW);
138-
return;
139-
}
140142
document
141143
.querySelector<HTMLElement>(`[data-content-search-result="${selectedIndex}"]`)
142144
?.scrollIntoView({ block: "nearest" });
143-
}, [selectedIndex, visibleCount]);
145+
}, [selectedIndex]);
144146

145-
const observeLoadMoreSentinel = useCallback((sentinel: HTMLElement | null) => {
147+
const observeLoadMoreSentinel = (sentinel: HTMLElement | null) => {
146148
if (!sentinel) return;
147149
const observer = new IntersectionObserver((entries) => {
148150
if (entries.some((entry) => entry.isIntersecting)) {
@@ -151,7 +153,7 @@ function OpenContentSearchDialog(props: {
151153
});
152154
observer.observe(sentinel);
153155
return () => observer.disconnect();
154-
}, []);
156+
};
155157

156158
const openMatch = (match: ProjectContentMatch) => {
157159
if (!canOpenMatches || !readEnvironmentScope(target.environmentId, AuthFilesystemReadScope)) {
@@ -160,7 +162,7 @@ function OpenContentSearchDialog(props: {
160162
props.onOpenChange(false);
161163
useRightPanelStore.getState().openFile(target.threadRef, match.path, match.lineNumber);
162164
};
163-
const fileCount = useMemo(() => new Set(matches.map((match) => match.path)).size, [matches]);
165+
const fileCount = new Set(matches.map((match) => match.path)).size;
164166
const showSearchStatus =
165167
search.canReadFiles &&
166168
(search.hasQuery || search.isPending || search.error !== null || search.invalidRegex);

0 commit comments

Comments
 (0)