import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../../../core/transport/pipelines_client.dart' show configuredGatewayEndpoint; import '../../../ui/layout/app_list_avatar.dart'; import '../../../ui/layout/app_overflow_menu.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'; /// 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(8), child: SizedBox.square( dimension: kAppListAvatarSize, 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()}, }, ); } } return AppOverflowMenu<_ArtifactOpen>( key: ValueKey('search.actions.${hit['target']}'), label: 'Open…', actions: const [ AppMenuAction(value: _ArtifactOpen.stage, label: 'Open stage'), AppMenuAction(value: _ArtifactOpen.run, label: 'Open run'), AppMenuAction(value: _ArtifactOpen.pipeline, label: 'Open pipeline'), ], onSelected: (_, choice) => pick(choice), ); }