Skip to content
TypeScript

perf(web): project content search compiles with the React Compiler (#20641)

Julius Marmingef22415a075ca
diff --git a/apps/web/src/components/search/ProjectContentSearchDialog.test.tsx b/apps/web/src/components/search/ProjectContentSearchDialog.test.tsx
index 0559e50fded..40aa0b2cb9a 100644
--- a/apps/web/src/components/search/ProjectContentSearchDialog.test.tsx
+++ b/apps/web/src/components/search/ProjectContentSearchDialog.test.tsx
@@ -10,6 +10,17 @@ const state = vi.hoisted(() => ({
error: null as string | null,
readScope: vi.fn(),
openFile: vi.fn(),
+ // The real query hands back the same result array until the search changes.
+ matches: [
+ {
+ resultIndex: 0,
+ path: "src/index.ts",
+ lineNumber: 3,
+ lineContent: "match",
+ matchRanges: [],
+ },
+ ],
+ noMatches: [],
}));
const target = {
@@ -37,18 +48,7 @@ vi.mock("~/state/queries", () => ({
hasQuery: query.length > 0,
truncated: false,
invalidRegex: false,
- matches:
- state.canReadFiles && query.length > 0
- ? [
- {
- resultIndex: 0,
- path: "src/index.ts",
- lineNumber: 3,
- lineContent: "match",
- matchRanges: [],
- },
- ]
- : [],
+ matches: state.canReadFiles && query.length > 0 ? state.matches : state.noMatches,
}),
}));
vi.mock("../CommandPaletteContent", () => ({ CommandPaletteContent: "section" }));
diff --git a/apps/web/src/components/search/ProjectContentSearchDialog.tsx b/apps/web/src/components/search/ProjectContentSearchDialog.tsx
index 86e345bfac5..52e8b5a49db 100644
--- a/apps/web/src/components/search/ProjectContentSearchDialog.tsx
+++ b/apps/web/src/components/search/ProjectContentSearchDialog.tsx
@@ -1,7 +1,7 @@
import { Spinner } from "~/components/ui/spinner";
import { AuthFilesystemReadScope, type ProjectContentMatch } from "@t3tools/contracts";
-import { useCallback, useEffect, useMemo, useState, type ReactNode } from "react";
+import { useEffect, useState, type ReactNode } from "react";
import { useActiveProjectTarget, type ActiveProjectTarget } from "~/hooks/useActiveProjectTarget";
import { useTheme } from "~/hooks/useTheme";
@@ -124,25 +124,27 @@ function OpenContentSearchDialog(props: {
});
const canOpenMatches = search.canReadFiles && !search.isPending && search.error === null;
const matches = search.matches;
- const visibleMatches = useMemo(() => matches.slice(0, visibleCount), [matches, visibleCount]);
- const groups = useMemo(() => groupMatches(visibleMatches), [visibleMatches]);
-
- useEffect(() => {
+ // New results restart the selection and the rendered window.
+ const [resultsFor, setResultsFor] = useState(matches);
+ if (resultsFor !== matches) {
+ setResultsFor(matches);
setSelectedIndex(0);
setVisibleCount(VISIBLE_MATCH_WINDOW);
- }, [matches]);
+ }
+ // Keyboard selection past the window grows it before the row has to scroll into view.
+ if (selectedIndex >= visibleCount) {
+ setVisibleCount(selectedIndex + VISIBLE_MATCH_WINDOW);
+ }
+ const visibleMatches = matches.slice(0, visibleCount);
+ const groups = groupMatches(visibleMatches);
useEffect(() => {
- if (selectedIndex >= visibleCount) {
- setVisibleCount(selectedIndex + VISIBLE_MATCH_WINDOW);
- return;
- }
document
.querySelector<HTMLElement>(`[data-content-search-result="${selectedIndex}"]`)
?.scrollIntoView({ block: "nearest" });
- }, [selectedIndex, visibleCount]);
+ }, [selectedIndex]);
- const observeLoadMoreSentinel = useCallback((sentinel: HTMLElement | null) => {
+ const observeLoadMoreSentinel = (sentinel: HTMLElement | null) => {
if (!sentinel) return;
const observer = new IntersectionObserver((entries) => {
if (entries.some((entry) => entry.isIntersecting)) {
@@ -151,7 +153,7 @@ function OpenContentSearchDialog(props: {
});
observer.observe(sentinel);
return () => observer.disconnect();
- }, []);
+ };
const openMatch = (match: ProjectContentMatch) => {
if (!canOpenMatches || !readEnvironmentScope(target.environmentId, AuthFilesystemReadScope)) {
@@ -160,7 +162,7 @@ function OpenContentSearchDialog(props: {
props.onOpenChange(false);
useRightPanelStore.getState().openFile(target.threadRef, match.path, match.lineNumber);
};
- const fileCount = useMemo(() => new Set(matches.map((match) => match.path)).size, [matches]);
+ const fileCount = new Set(matches.map((match) => match.path)).size;
const showSearchStatus =
search.canReadFiles &&
(search.hasQuery || search.isPending || search.error !== null || search.invalidRegex);