Files
pipelines/app/lib/features/search/ui/search_result_slivers.dart
T
alan ccc80a9a8d Add keyboard navigation to search results
Keep the search field focused while selecting results with Control or arrow keys, and open the selected result with Enter. Document the field-scoped shortcuts and cover navigation, reset, submission, and typing behavior with regression tests.
2026-11-13 15:51:00 +00:00

90 lines
3.1 KiB
Dart

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<String>('search.section.chats');
const searchOthersSectionKey = ValueKey<String>('search.section.others');
const searchChatsNoteKey = ValueKey<String>('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<Widget> searchResultSlivers({
required OmniSearchController controller,
required String query,
required ValueChanged<JsonMap> onOpen,
required Widget Function(String message) message,
required int selectedIndex,
required Map<int, GlobalKey> rowKeys,
}) {
final chats = controller.shownChats;
final others = controller.shownResults;
final allHits = [...chats, ...others];
final targetCounts = <String, int>{};
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<JsonMap> 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)),
];
}