import 'package:flutter/widgets.dart'; import '../../../core/api_gateway.dart'; import '../../../ui/layout/section_heading.dart'; import '../domain/search_controller.dart'; import '../domain/search_models.dart'; import 'search_bounds_note.dart'; import 'search_result_row.dart'; const searchChatsSectionKey = ValueKey('search.section.chats'); const searchOthersSectionKey = ValueKey('search.section.others'); const searchChatsNoteKey = ValueKey('search.chats.note'); /// The results body shared by both hosts. With every kind searched, chat hits /// lead as their own "Chats" group above the pipeline/run/artifact hits; with /// Chats selected alone they are the list. A run search with no hits still /// says so under the Chats group. List searchResultSlivers({ required OmniSearchController controller, required String query, required ValueChanged onOpen, required Widget Function(String message) message, required int selectedIndex, required Map rowKeys, }) { final chats = controller.shownChats; final others = controller.shownResults; final allHits = [...chats, ...others]; final targetCounts = {}; for (final hit in allHits) { final target = '${hit['target'] ?? ''}'; targetCounts[target] = (targetCounts[target] ?? 0) + 1; } final grouped = controller.showsChatSection; final note = controller.chatsNote; Widget rows(List hits, int offset) => SliverList.builder( itemCount: hits.length, itemBuilder: (context, index) { final hit = hits[index]; final target = '${hit['target'] ?? ''}'; final indexInResults = offset + index; final key = rowKeys.putIfAbsent(indexInResults, GlobalKey.new); final rowKey = target.isEmpty || targetCounts[target]! > 1 ? ValueKey('search.result.$target.$indexInResults') : ValueKey('search.result.$target'); return SearchResultRow( rowKey: rowKey, hit: hit, query: query, onOpen: onOpen, divider: index < hits.length - 1, selected: indexInResults == selectedIndex, visibilityKey: key, ); }, ); final empty = controller.emptyMessage; return [ if (grouped) SliverToBoxAdapter( child: SectionHeading( key: searchChatsSectionKey, title: chats.isEmpty ? 'Chats' : 'Chats (${chats.length})', header: true, ), ), if (chats.isNotEmpty) rows(chats, 0), if (note != null) SliverToBoxAdapter( child: Padding( padding: const EdgeInsets.symmetric(vertical: 8), child: SearchBoundsNote(key: searchChatsNoteKey, text: note), ), ), if (grouped && others.isNotEmpty) const SliverToBoxAdapter( child: SectionHeading( key: searchOthersSectionKey, title: 'Pipelines and runs', header: true, ), ), if (others.isNotEmpty) rows(others, chats.length), if (others.isEmpty && controller.parsed.kind != SearchKind.chat && empty != null) SliverToBoxAdapter(child: message(empty)), ]; }