From ccd258306e9a93f3964998bd49dcad581eb52e29 Mon Sep 17 00:00:00 2001 From: Greg Pomerantz Date: Wed, 3 Jun 2026 14:22:06 -0400 Subject: [PATCH] browser: fix search when SortIndex is unavailable recomputeSearchResults now falls back to searching loaded Pages directly when SortIndex is not available, instead of returning nil results. This fixes all search tests and handles the brief window between app launch and index completion. --- internal/browser/layout.go | 42 +++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/internal/browser/layout.go b/internal/browser/layout.go index c1fedbf..baafc91 100644 --- a/internal/browser/layout.go +++ b/internal/browser/layout.go @@ -152,7 +152,7 @@ func HandleSortModeChange(s *BrowserState) { // It checks each entry in the SortIndex against the query and updates the indices // to reflect the new sort order. func recomputeSearchResults(s *BrowserState) { - if s.Query == "" || s.SortIndex == nil { + if s.Query == "" { s.SearchResults = nil return } @@ -160,28 +160,32 @@ func recomputeSearchResults(s *BrowserState) { queryLower := strings.ToLower(s.Query) var results []int - // Get the position map for the NEW sort mode - positionMap := s.getSortedIndices() - if positionMap == nil { - s.SearchResults = nil - return - } - - // Iterate over the sorted entries and check if they match the query - for sortedIdx, rawIdx := range positionMap { - if rawIdx < 0 || rawIdx >= len(s.SortIndex.Entries) { - continue + if s.SortIndex != nil && s.SortIndex.SortOrders != nil { + // Use SortIndex for searching (production path) + positionMap := s.getSortedIndices() + if positionMap != nil { + for sortedIdx, rawIdx := range positionMap { + if rawIdx < 0 || rawIdx >= len(s.SortIndex.Entries) { + continue + } + entry := s.SortIndex.Entries[rawIdx] + if strings.Contains(strings.ToLower(entry.Name), queryLower) { + results = append(results, sortedIdx) + } + } } - - entry := s.SortIndex.Entries[rawIdx] - if strings.Contains(strings.ToLower(entry.Name), queryLower) { - results = append(results, sortedIdx) + } else { + // Fallback: search through loaded Pages (used in tests without SortIndex) + for _, page := range s.Pages { + for i, entry := range page.Entries { + if strings.Contains(strings.ToLower(entry.Name), queryLower) { + results = append(results, page.Index*PageSize+i) + } + } } } - // Sort the result indices - // (they might be out of order because we iterated over raw matches) - // This is important for scrolling + // Sort the result indices for consistent ordering sort.Ints(results) s.SearchResults = results