@@ -134,23 +134,66 @@ const EcoTreeView: React.FC<EcoTreeViewProps> = ({
134134 const filteredSections = useMemo ( ( ) => {
135135 if ( ! searchTerm ) return sections
136136
137+ const searchLower = searchTerm . toLowerCase ( )
138+
137139 return sections
138- . map ( ( section ) => ( {
139- ...section ,
140- openings : section . openings . filter (
141- ( opening ) =>
142- opening . name
143- . toLowerCase ( )
144- . includes ( searchTerm . toLowerCase ( ) ) ||
145- opening . eco . toLowerCase ( ) . includes ( searchTerm . toLowerCase ( ) ) ||
146- opening . variations . some ( ( variation ) =>
147- variation . name . toLowerCase ( ) . includes ( searchTerm . toLowerCase ( ) ) ,
148- ) ,
149- ) ,
150- } ) )
140+ . map ( ( section ) => {
141+ const matchingOpenings = section . openings
142+ . map ( ( opening ) => {
143+ const openingMatches =
144+ opening . name . toLowerCase ( ) . includes ( searchLower ) ||
145+ opening . eco . toLowerCase ( ) . includes ( searchLower )
146+
147+ const matchingVariations = opening . variations . filter ( ( variation ) =>
148+ variation . name . toLowerCase ( ) . includes ( searchLower )
149+ )
150+
151+ // Include opening if it matches or has matching variations
152+ if ( openingMatches || matchingVariations . length > 0 ) {
153+ return {
154+ ...opening ,
155+ // Only show matching variations when searching
156+ variations : matchingVariations
157+ }
158+ }
159+ return null
160+ } )
161+ . filter ( Boolean ) as EcoOpening [ ]
162+
163+ return {
164+ ...section ,
165+ openings : matchingOpenings
166+ }
167+ } )
151168 . filter ( ( section ) => section . openings . length > 0 )
152169 } , [ sections , searchTerm ] )
153170
171+ // Auto-expand sections and openings when searching
172+ const autoExpandedSections = useMemo ( ( ) => {
173+ if ( ! searchTerm ) return expandedSections
174+
175+ const newExpanded = new Set ( expandedSections )
176+ filteredSections . forEach ( ( section ) => {
177+ newExpanded . add ( section . code )
178+ } )
179+ return newExpanded
180+ } , [ expandedSections , filteredSections , searchTerm ] )
181+
182+ const autoExpandedOpenings = useMemo ( ( ) => {
183+ if ( ! searchTerm ) return expandedOpenings
184+
185+ const newExpanded = new Set ( expandedOpenings )
186+ filteredSections . forEach ( ( section ) => {
187+ section . openings . forEach ( ( opening ) => {
188+ // Auto-expand openings that have matching variations
189+ if ( opening . variations . length > 0 ) {
190+ newExpanded . add ( opening . id )
191+ }
192+ } )
193+ } )
194+ return newExpanded
195+ } , [ expandedOpenings , filteredSections , searchTerm ] )
196+
154197 const filteredPopularOpenings = useMemo ( ( ) => {
155198 if ( ! searchTerm ) return popularOpenings
156199
@@ -294,7 +337,7 @@ const EcoTreeView: React.FC<EcoTreeViewProps> = ({
294337 < div className = "py-1" >
295338 { filteredSections . map ( ( section , sectionIndex ) => {
296339 const isLastSection = sectionIndex === filteredSections . length - 1
297- const sectionExpanded = expandedSections . has ( section . code )
340+ const sectionExpanded = autoExpandedSections . has ( section . code )
298341
299342 return (
300343 < div key = { section . code } >
@@ -334,7 +377,7 @@ const EcoTreeView: React.FC<EcoTreeViewProps> = ({
334377 >
335378 { section . openings . map ( ( opening , openingIndex ) => {
336379 const isLastOpening = openingIndex === section . openings . length - 1
337- const openingExpanded = expandedOpenings . has ( opening . id )
380+ const openingExpanded = autoExpandedOpenings . has ( opening . id )
338381 const hasVariations = opening . variations . length > 0
339382
340383 return (
0 commit comments