Files
pipelines/app/lib/features/search/ui/search_result_view.dart
T

210 lines
6.8 KiB
Dart

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
/// beyond weight so Material and Cupertino inherit their own system typography
/// and colours, including dark mode and text scaling.
Widget searchRowMeta(SearchRowView row) {
final parts = [row.kindLabel, if (row.recency != null) row.recency!];
return Text(
parts.join(' · '),
style: const TextStyle(fontWeight: FontWeight.w600),
);
}
/// The snippet with the first query occurrence emphasised by weight, never by
/// parsing server text as markup or by a bespoke colour.
Widget searchRowSnippet(BuildContext context, SearchRowView row) {
final snippet = row.snippet;
if (snippet == null) {
return row.secondary.isEmpty
? const SizedBox.shrink()
: Text(row.secondary);
}
return Text.rich(
TextSpan(
children: [
for (final span in row.snippetSpans)
TextSpan(
text: span.text,
style: span.matched
? const TextStyle(fontWeight: FontWeight.bold)
: null,
),
],
),
);
}
IconData searchMaterialIcon(String kind) => switch (kind) {
'pipeline' => Icons.account_tree_outlined,
'version' => Icons.layers_outlined,
'run' => Icons.play_circle_outline,
'step' => Icons.arrow_forward,
'field' => Icons.notes,
'artifact' => Icons.attach_file,
_ => Icons.help_outline,
};
IconData searchCupertinoIcon(String kind) => switch (kind) {
'pipeline' => CupertinoIcons.arrow_branch,
'version' => CupertinoIcons.square_stack_3d_up,
'run' => CupertinoIcons.play_circle,
'step' => CupertinoIcons.arrow_right,
'field' => CupertinoIcons.doc_text,
'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<JsonMap> onOpen;
static SearchArtifactScope? maybeOf(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<SearchArtifactScope>();
@override
bool updateShouldNotify(SearchArtifactScope oldWidget) =>
gateway != oldWidget.gateway || onOpen != oldWidget.onOpen;
}
JsonMap? _artifactOf(JsonMap hit) =>
hit['kind'] == 'artifact' && hit['artifact'] is Map
? Map<String, dynamic>.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<void> 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)),
],
);
}