11import { Spinner } from "~/components/ui/spinner" ;
22import { 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
66import { useActiveProjectTarget , type ActiveProjectTarget } from "~/hooks/useActiveProjectTarget" ;
77import { 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