@@ -130,6 +130,7 @@ export class FiltersComponent implements OnInit, AfterViewInit, OnDestroy {
130130 fullFacetCountsComplete : boolean = false ; // made public for template gating
131131 private fullFacetCountsSubscription : any = null ;
132132 private searchResponseSub : any = null ;
133+ private externalToggleSubscription : any = null ;
133134
134135 filterStyle = {
135136 width : "100%" ,
@@ -211,13 +212,8 @@ export class FiltersComponent implements OnInit, AfterViewInit, OnDestroy {
211212 ) {
212213 //Clear filters when we conduct a new search
213214 this . clearFilters ( ) ;
214- // Reset facet aggregation state so new query can trigger fresh expanded fetch
215- this . fullFacetCountsInFlight = false ;
216- this . fullFacetCountsComplete = false ;
217- // Show skeletons again for all facet groups until rebuilt
218- this . recordHasLoading = true ;
219- this . themeLoading = true ;
220- this . resourceTypeLoading = true ;
215+ // Reset facet aggregation state so new query can trigger fresh expanded fetch
216+ this . resetFacetAggregationState ( ) ;
221217 this . onSearchValueChanged ( ) ;
222218 }
223219 }
@@ -239,12 +235,20 @@ export class FiltersComponent implements OnInit, AfterViewInit, OnDestroy {
239235 if ( this . lastOutboundFilterString === str ) return ; // ignore self echo
240236 this . applyFilterStringToSelections ( str ) ;
241237 } ) ;
238+
239+ // External toggle changes should reset facet aggregation to avoid stale filters
240+ this . externalToggleSubscription = this . searchService
241+ . watchExternalProducts ( )
242+ . subscribe ( ( ) => {
243+ this . resetFacetAggregationState ( ) ;
244+ } ) ;
242245 }
243246
244247 ngOnDestroy ( ) : void {
245248 if ( this . searchResponseSub ) { try { this . searchResponseSub . unsubscribe ( ) ; } catch { } }
246249 if ( this . fullFacetCountsSubscription ) { try { this . fullFacetCountsSubscription . unsubscribe ( ) ; } catch { } }
247250 if ( this . filterWatcherSub ) { try { this . filterWatcherSub . unsubscribe ( ) ; } catch { } }
251+ if ( this . externalToggleSubscription ) { try { this . externalToggleSubscription . unsubscribe ( ) ; } catch { } }
248252 }
249253
250254 toggleMoreOptions ( ) {
@@ -394,7 +398,7 @@ export class FiltersComponent implements OnInit, AfterViewInit, OnDestroy {
394398 const q = this . searchQueryService . buildQueryFromString ( lSearchValue , null , this . fields ) ;
395399 // Mark loading for possible UI skeletons
396400 this . recordHasLoading = true ; this . themeLoading = true ; this . resourceTypeLoading = true ;
397- this . searchService . fetchAllForFacetCounts ( q , this . searchTaxonomyKey , targetSize , this . searchService [ 'filterString' ] ?. getValue ?.( ) ) . subscribe ( expanded => {
401+ this . fullFacetCountsSubscription = this . searchService . fetchAllForFacetCounts ( q , this . searchTaxonomyKey , targetSize , this . searchService [ 'filterString' ] ?. getValue ?.( ) ) . subscribe ( expanded => {
398402 if ( expanded && expanded . ResultData && expanded . ResultData . length ) {
399403 this . buildFacetCounts ( expanded . ResultData , true ) ; // final pass
400404 } else {
@@ -411,6 +415,21 @@ export class FiltersComponent implements OnInit, AfterViewInit, OnDestroy {
411415 } ) ;
412416 }
413417
418+ private resetFacetAggregationState ( ) {
419+ if ( this . fullFacetCountsSubscription ) {
420+ try {
421+ this . fullFacetCountsSubscription . unsubscribe ( ) ;
422+ } catch { }
423+ this . fullFacetCountsSubscription = null ;
424+ }
425+ this . fullFacetCountsInFlight = false ;
426+ this . fullFacetCountsComplete = false ;
427+ // Show skeletons again for all facet groups until rebuilt
428+ this . recordHasLoading = true ;
429+ this . themeLoading = true ;
430+ this . resourceTypeLoading = true ;
431+ }
432+
414433 private buildFacetCounts ( data : any [ ] , finalPass : boolean = true ) {
415434 this . themesWithCount = [ ] ;
416435 this . componentsWithCount = [ ] ;
0 commit comments