diff --git a/app/lib/features/search/search_screen.dart b/app/lib/features/search/search_screen.dart index 2aff0e7d..4a7ad803 100644 --- a/app/lib/features/search/search_screen.dart +++ b/app/lib/features/search/search_screen.dart @@ -7,6 +7,7 @@ import 'data/search_repository.dart'; import 'domain/search_controller.dart'; import 'domain/search_models.dart'; import 'ui/cupertino_search_body.dart'; +import 'ui/search_result_view.dart'; import 'ui/material_search_body.dart'; /// Native omni-search over the Pipelines server: a standard search field, an @@ -118,11 +119,15 @@ class _SearchScreenState extends State { Widget _materialScreen() { final body = SafeArea( - child: MaterialSearchBody( - controller: _controller, - query: _controller.parsed.query, + child: SearchArtifactScope( + gateway: widget.gateway, onOpen: widget.onOpen, - header: _materialHeader(), + child: MaterialSearchBody( + controller: _controller, + query: _controller.parsed.query, + onOpen: widget.onOpen, + header: _materialHeader(), + ), ), ); if (widget.embedded) return body; @@ -185,11 +190,15 @@ class _SearchScreenState extends State { Widget _cupertinoScreen() { final body = SafeArea( - child: CupertinoSearchBody( - controller: _controller, - query: _controller.parsed.query, + child: SearchArtifactScope( + gateway: widget.gateway, onOpen: widget.onOpen, - header: _cupertinoHeader(context), + child: CupertinoSearchBody( + controller: _controller, + query: _controller.parsed.query, + onOpen: widget.onOpen, + header: _cupertinoHeader(context), + ), ), ); if (widget.embedded) return body; diff --git a/app/lib/features/search/ui/cupertino_search_body.dart b/app/lib/features/search/ui/cupertino_search_body.dart index 8df2bd10..9da701a5 100644 --- a/app/lib/features/search/ui/cupertino_search_body.dart +++ b/app/lib/features/search/ui/cupertino_search_body.dart @@ -103,7 +103,11 @@ class _CupertinoResult extends StatelessWidget { final row = searchRowView(hit, query); return CupertinoListTile( key: ValueKey('search.result.${hit['target']}'), - leading: Icon(searchCupertinoIcon('${hit['kind'] ?? ''}')), + leading: + searchArtifactThumbnail(context, hit) ?? + Icon(searchCupertinoIcon('${hit['kind'] ?? ''}')), + leadingSize: 44, + trailing: searchArtifactActions(context, hit), title: Text(row.title), subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, diff --git a/app/lib/features/search/ui/material_search_body.dart b/app/lib/features/search/ui/material_search_body.dart index 4a49f49b..edbe0fde 100644 --- a/app/lib/features/search/ui/material_search_body.dart +++ b/app/lib/features/search/ui/material_search_body.dart @@ -94,7 +94,10 @@ class _MaterialResult extends StatelessWidget { final row = searchRowView(hit, query); return ListTile( key: ValueKey('search.result.${hit['target']}'), - leading: Icon(searchMaterialIcon('${hit['kind'] ?? ''}')), + leading: + searchArtifactThumbnail(context, hit) ?? + Icon(searchMaterialIcon('${hit['kind'] ?? ''}')), + trailing: searchArtifactActions(context, hit), title: Text(row.title), subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, diff --git a/app/lib/features/search/ui/search_result_view.dart b/app/lib/features/search/ui/search_result_view.dart index fef687f3..1d23851f 100644 --- a/app/lib/features/search/ui/search_result_view.dart +++ b/app/lib/features/search/ui/search_result_view.dart @@ -1,6 +1,14 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import '../../../core/api_gateway.dart'; +import '../../../core/transport/pipelines_client.dart' + show configuredGatewayEndpoint; +import '../../../ui/adaptive/host.dart'; +import '../../artifacts/domain/artifact_kind.dart'; +import '../../artifacts/domain/artifact_record.dart'; +import '../../artifacts/ui/artifact_tile.dart'; +import '../../artifacts/ui/artifact_viewer_fetch.dart'; import '../domain/search_row.dart'; /// Kind, recency and read-only affordance for one row. Deliberately unstyled @@ -57,3 +65,145 @@ IconData searchCupertinoIcon(String kind) => switch (kind) { 'artifact' => CupertinoIcons.paperclip, _ => CupertinoIcons.question_circle, }; + +/// What artifact hits need from the screen: the app's gateway (to resolve a +/// thumbnail and look up a run's pipeline) and the screen's opener. +class SearchArtifactScope extends InheritedWidget { + const SearchArtifactScope({ + super.key, + required this.gateway, + required this.onOpen, + required super.child, + }); + + final ApiGateway gateway; + final ValueChanged onOpen; + + static SearchArtifactScope? maybeOf(BuildContext context) => + context.dependOnInheritedWidgetOfExactType(); + + @override + bool updateShouldNotify(SearchArtifactScope oldWidget) => + gateway != oldWidget.gateway || onOpen != oldWidget.onOpen; +} + +JsonMap? _artifactOf(JsonMap hit) => + hit['kind'] == 'artifact' && hit['artifact'] is Map + ? Map.from(hit['artifact'] as Map) + : null; + +/// An artifact hit's leading picture: the image itself when it is one, +/// otherwise null so the row keeps its kind icon. +Widget? searchArtifactThumbnail(BuildContext context, JsonMap hit) { + final artifact = _artifactOf(hit); + final scope = SearchArtifactScope.maybeOf(context); + if (artifact == null || scope == null) return null; + final record = ArtifactRecord.fromJson(artifact); + if (artifactPreviewFormat(record.contentType, record.name) != + ArtifactPreviewFormat.image) { + return null; + } + return ClipRRect( + borderRadius: BorderRadius.circular(6), + child: SizedBox.square( + dimension: 44, + child: ArtifactThumbnail( + key: ValueKey('search.thumbnail.${hit['target']}'), + artifact: record, + configuredEndpoint: configuredGatewayEndpoint(scope.gateway), + fetch: fetchArtifact, + ), + ), + ); +} + +enum _ArtifactOpen { stage, run, pipeline } + +/// An artifact hit's choices: open the stage that recorded it, its run, or +/// the run's pipeline. +Widget? searchArtifactActions(BuildContext context, JsonMap hit) { + final artifact = _artifactOf(hit); + final scope = SearchArtifactScope.maybeOf(context); + final runId = artifact?['run_id']; + if (artifact == null || scope == null || runId is! num) return null; + Future pick(_ArtifactOpen choice) async { + switch (choice) { + case _ArtifactOpen.stage: + scope.onOpen(hit); + case _ArtifactOpen.run: + scope.onOpen({ + 'kind': 'run', + 'target': 'run:${runId.toInt()}', + 'run': {'id': runId.toInt()}, + }); + case _ArtifactOpen.pipeline: + // The hit names only the run; its pinned pipeline comes from the run. + final reply = await scope.gateway.call('get_run', { + 'run_id': runId.toInt(), + }); + final run = reply['run_detail']; + if (run is! Map) return; + final id = run['pipeline_id']; + final version = run['version']; + if (id is! num || id <= 0) return; + scope.onOpen( + version is num && version > 0 + ? { + 'kind': 'version', + 'target': 'pipeline:${id.toInt()}:v${version.toInt()}', + 'version': { + 'pipeline_id': id.toInt(), + 'version': version.toInt(), + }, + } + : { + 'kind': 'pipeline', + 'target': 'pipeline:${id.toInt()}', + 'pipeline': {'id': id.toInt()}, + }, + ); + } + } + + const labels = { + _ArtifactOpen.stage: 'Open stage', + _ArtifactOpen.run: 'Open run', + _ArtifactOpen.pipeline: 'Open pipeline', + }; + final key = ValueKey('search.actions.${hit['target']}'); + if (usesCupertinoHost(context)) { + return CupertinoButton( + key: key, + padding: EdgeInsets.zero, + onPressed: () async { + final choice = await showCupertinoModalPopup<_ArtifactOpen>( + context: context, + builder: (sheet) => CupertinoActionSheet( + actions: [ + for (final entry in labels.entries) + CupertinoActionSheetAction( + onPressed: () => Navigator.pop(sheet, entry.key), + child: Text(entry.value), + ), + ], + cancelButton: CupertinoActionSheetAction( + onPressed: () => Navigator.pop(sheet), + child: const Text('Cancel'), + ), + ), + ); + if (choice != null) await pick(choice); + }, + child: const Icon(CupertinoIcons.ellipsis_circle), + ); + } + return PopupMenuButton<_ArtifactOpen>( + key: key, + tooltip: 'Open…', + onSelected: pick, + itemBuilder: (_) => [ + for (final entry in labels.entries) + PopupMenuItem(value: entry.key, child: Text(entry.value)), + ], + ); +} diff --git a/app/test/features/search/search_artifact_actions_test.dart b/app/test/features/search/search_artifact_actions_test.dart new file mode 100644 index 00000000..35d8137f --- /dev/null +++ b/app/test/features/search/search_artifact_actions_test.dart @@ -0,0 +1,77 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/search/domain/search_target.dart'; +import 'package:pipelines_app/features/search/ui/search_result_view.dart'; + +class _Gateway implements ApiGateway { + @override + Stream get frames => const Stream.empty(); + + @override + Future call(String type, [JsonMap fields = const {}]) async { + expect(type, 'get_run'); + expect(fields, {'run_id': 7}); + return { + 'run_detail': {'id': 7, 'pipeline_id': 3, 'version': 2}, + }; + } +} + +final _hit = { + 'kind': 'artifact', + 'target': '/artifacts/7/2/shot.png', + 'artifact': { + 'run_id': 7, + 'sequence': 2, + 'name': 'shot.png', + 'content_type': 'image/png', + 'size_bytes': 10, + 'url': '/artifacts/7/2/shot.png', + }, +}; + +void main() { + testWidgets('an artifact hit shows its picture and opens stage, run or ' + 'pipeline', (tester) async { + final opened = []; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SearchArtifactScope( + gateway: _Gateway(), + onOpen: opened.add, + child: Builder( + builder: (context) => ListTile( + leading: searchArtifactThumbnail(context, _hit), + title: const Text('shot.png'), + trailing: searchArtifactActions(context, _hit), + ), + ), + ), + ), + ), + ); + expect( + find.byKey(const ValueKey('search.thumbnail./artifacts/7/2/shot.png')), + findsOneWidget, + ); + + Future choose(String label) async { + await tester.tap(find.byTooltip('Open…')); + await tester.pumpAndSettle(); + await tester.tap(find.text(label)); + await tester.pumpAndSettle(); + } + + await choose('Open stage'); + expect(opened.last, same(_hit)); + await choose('Open run'); + expect(opened.last['target'], 'run:7'); + expect(searchTargetFor(opened.last), isA()); + await choose('Open pipeline'); + expect(opened.last['target'], 'pipeline:3:v2'); + // The app's opener accepts the hit the action builds. + expect(searchTargetFor(opened.last), isA()); + }); +}