merge(app): land recorded artifact gallery in run detail

This commit is contained in:
2026-10-21 22:34:25 +01:00
13 changed files with 1722 additions and 25 deletions
@@ -0,0 +1,14 @@
import '../domain/artifact_url.dart';
import 'artifact_external_open_stub.dart'
if (dart.library.js_interop) 'artifact_external_open_web.dart'
as impl;
/// Opens an already validated artifact url with the host's external handler.
///
/// The url must come from [resolveArtifactUrl]; this capability never resolves,
/// rewrites, fetches or previews it. Returns whether the host accepted the
/// request, so a refusal is reported instead of being silently swallowed.
typedef ArtifactExternalOpen = Future<bool> Function(Uri uri);
Future<bool> openArtifactExternally(Uri uri) =>
impl.openArtifactExternally(uri);
@@ -0,0 +1,6 @@
/// Non-web fallback for external artifact urls.
///
/// A native launcher needs a platform plugin that this slice does not depend
/// on, so a non-web host reports the refusal instead of pretending to open the
/// link. The page surfaces that message to the operator.
Future<bool> openArtifactExternally(Uri uri) async => false;
@@ -0,0 +1,20 @@
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
/// Web external open: a new browser tab. The url is already validated by
/// [resolveArtifactUrl]; it is passed through unchanged and never fetched.
///
/// `window.open` is reached through `js_interop` rather than `dart:html`, so the
/// same source compiles for both the JavaScript and WebAssembly web targets.
Future<bool> openArtifactExternally(Uri uri) async {
try {
final opened = globalContext.callMethod(
'open'.toJS,
uri.toString().toJS,
'_blank'.toJS,
);
return opened.isDefinedAndNotNull;
} catch (_) {
return false;
}
}
@@ -0,0 +1,104 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../../../core/transport/pipelines_client.dart';
import '../../../ui/adaptive/host.dart';
import 'artifact_external_open.dart';
import 'artifact_gallery_page.dart';
/// The run detail's action label key, so a host can find the one action.
const ValueKey<String> runArtifactsButtonKey = ValueKey<String>(
'run-detail-artifacts',
);
const Set<String> _executingStatuses = {'queued', 'running', 'waiting'};
/// The detail's `steps` exactly as returned by the server. The pushed page
/// snapshots the returned list once, so it never re-derives on each rebuild.
List<JsonMap> runDetailArtifactSteps(JsonMap run) {
final steps = run['steps'];
if (steps is List<JsonMap>) return steps;
if (steps is List) return steps.cast<JsonMap>();
return const <JsonMap>[];
}
/// Whether the run detail offers the gallery action at all: only once the
/// loaded detail actually carries steps. A run with no steps has nothing to
/// list, so the action is absent rather than opening a page that says so.
bool showRunArtifactsAction(JsonMap run) =>
runDetailArtifactSteps(run).isNotEmpty;
/// Whether the run is still executing, so the page can say capture pending
/// instead of claiming nothing was recorded.
bool _runExecuting(JsonMap run) =>
run['executing'] == true ||
_executingStatuses.contains('${run['status'] ?? ''}'.trim().toLowerCase());
/// The configured endpoint from the app's shared transport, when the gateway
/// exposes it. Feature fixtures without an endpoint leave it blank, which only
/// disables relative url resolution: an absolute recorded http(s) url still
/// resolves. The url rules themselves live in `artifact_url.dart`.
String _gatewayEndpoint(ApiGateway gateway) {
final endpoint = gateway is PipelinesClient ? gateway.endpoint : null;
return endpoint?.toString() ?? '';
}
/// The run detail's standard native action that opens the recorded artifact
/// groups. It is composed only when [showRunArtifactsAction] is true.
class RunArtifactsButton extends StatelessWidget {
const RunArtifactsButton({
super.key,
required this.run,
required this.gateway,
});
final JsonMap run;
final ApiGateway gateway;
@override
Widget build(BuildContext context) {
final steps = runDetailArtifactSteps(run);
void open() => showArtifactGallery(
context,
steps: steps,
executing: _runExecuting(run),
configuredEndpoint: _gatewayEndpoint(gateway),
);
if (usesCupertinoHost(context)) {
return CupertinoButton.tinted(
key: runArtifactsButtonKey,
onPressed: open,
child: const Text(artifactGalleryTitle),
);
}
return FilledButton.tonalIcon(
key: runArtifactsButtonKey,
onPressed: open,
icon: const Icon(Icons.inventory_2_outlined),
label: const Text(artifactGalleryTitle),
);
}
}
/// Opens the recorded artifact groups on a native page. The steps are
/// snapshotted once; the page never re-fetches or re-derives them.
Future<void> showArtifactGallery(
BuildContext context, {
required List<JsonMap> steps,
bool executing = false,
String configuredEndpoint = '',
ArtifactExternalOpen openExternal = openArtifactExternally,
}) {
final page = ArtifactGalleryPage(
steps: List<JsonMap>.unmodifiable(steps),
executing: executing,
configuredEndpoint: configuredEndpoint,
openExternal: openExternal,
);
return Navigator.of(context).push<void>(
usesCupertinoHost(context)
? CupertinoPageRoute<void>(builder: (_) => page)
: MaterialPageRoute<void>(builder: (_) => page),
);
}
@@ -0,0 +1,194 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../../core/api_gateway.dart';
import '../../../ui/adaptive/host.dart';
import '../domain/artifact_groups.dart';
import '../domain/artifact_record.dart';
import '../domain/artifact_summary.dart';
import '../domain/artifact_url.dart';
import 'artifact_external_open.dart';
import 'artifact_selection_facts.dart';
import 'artifact_visit_list.dart';
/// The gallery page title.
const String artifactGalleryTitle = 'Artifacts';
/// A read-only page of recorded stage/visit artifact groups.
///
/// Selection uses [artifactVisitIdentity], so the same filename in two visits
/// stays two distinct selections. Selecting a row opens a native dialog with
/// the recorded name, MIME and size, the visit's capture facts and, only when
/// the recorded url resolves through [resolveArtifactUrl], native `Open` and
/// `Copy link` actions. It never previews, downloads, shares or filters.
class ArtifactGalleryPage extends StatefulWidget {
const ArtifactGalleryPage({
super.key,
required this.steps,
this.executing = false,
this.configuredEndpoint = '',
this.openExternal = openArtifactExternally,
});
final List<JsonMap> steps;
final bool executing;
final String configuredEndpoint;
final ArtifactExternalOpen openExternal;
@override
State<ArtifactGalleryPage> createState() => _ArtifactGalleryPageState();
}
class _ArtifactGalleryPageState extends State<ArtifactGalleryPage> {
late final Map<String, JsonMap> _stepsByGroup = recordedStepsByGroup(
widget.steps,
);
String? _selectedIdentity;
String? _status;
void _select(ArtifactGroup group, ArtifactRecord artifact) {
setState(() {
_selectedIdentity = artifactVisitIdentity(group, artifact);
_status = null;
});
_showSelected(group, artifact);
}
Future<void> _showSelected(ArtifactGroup group, ArtifactRecord artifact) {
final step = _stepsByGroup[artifactGroupKey(group.stageKey, group.visit)];
final resolved = artifact.hasUrl
? resolveArtifactUrl(widget.configuredEndpoint, artifact.url)
: null;
final facts = ArtifactSelectionFacts(
group: group,
artifact: artifact,
step: step,
resolved: resolved,
);
if (usesCupertinoHost(context)) {
return showCupertinoDialog<void>(
context: context,
builder: (dialogContext) => CupertinoAlertDialog(
title: Text(artifactDisplayName(artifact)),
content: facts,
actions: [
if (resolved != null)
CupertinoDialogAction(
isDefaultAction: true,
onPressed: () => _open(resolved, dialogContext),
child: const Text('Open'),
),
if (resolved != null)
CupertinoDialogAction(
onPressed: () => _copyLink(resolved, dialogContext),
child: const Text('Copy link'),
),
CupertinoDialogAction(
onPressed: () => Navigator.of(dialogContext).pop(),
child: const Text('Close'),
),
],
),
);
}
return showDialog<void>(
context: context,
builder: (dialogContext) => AlertDialog(
title: Text(artifactDisplayName(artifact)),
content: SingleChildScrollView(child: facts),
actions: [
if (resolved != null)
TextButton(
onPressed: () => _copyLink(resolved, dialogContext),
child: const Text('Copy link'),
),
TextButton(
onPressed: () => Navigator.of(dialogContext).pop(),
child: const Text('Close'),
),
if (resolved != null)
TextButton(
onPressed: () => _open(resolved, dialogContext),
child: const Text('Open'),
),
],
),
);
}
Future<void> _open(
ResolvedArtifactUrl resolved,
BuildContext dialogContext,
) async {
Navigator.of(dialogContext).pop();
final opened = await widget.openExternal(resolved.uri);
if (!mounted) return;
setState(
() => _status = opened
? 'Link opened externally.'
: 'Could not open the link.',
);
}
Future<void> _copyLink(
ResolvedArtifactUrl resolved,
BuildContext dialogContext,
) async {
Navigator.of(dialogContext).pop();
String message;
try {
await Clipboard.setData(ClipboardData(text: resolved.url));
message = 'Link copied.';
} catch (_) {
message = 'Could not copy link.';
}
if (!mounted) return;
setState(() => _status = message);
}
@override
Widget build(BuildContext context) {
final groups = artifactGroups(widget.steps);
final summary = widget.steps.isEmpty
? 'No steps recorded for this run.'
: artifactsHeader(widget.steps, executing: widget.executing);
final body = Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsetsDirectional.fromSTEB(16, 12, 16, 4),
child: Text(summary),
),
if (_status != null)
Padding(
padding: const EdgeInsetsDirectional.fromSTEB(16, 0, 16, 4),
child: Semantics(liveRegion: true, child: Text(_status!)),
),
if (groups.isNotEmpty)
Expanded(
child: ArtifactVisitList(
groups: groups,
selectedIdentity: _selectedIdentity,
onSelected: _select,
),
),
],
);
if (usesCupertinoHost(context)) {
return CupertinoPageScaffold(
// Sections paint the grouped background; the page must match it or a
// white seam shows below the last section in light mode.
backgroundColor: CupertinoColors.systemGroupedBackground,
navigationBar: const CupertinoNavigationBar(
middle: Text(artifactGalleryTitle),
),
child: SafeArea(child: body),
);
}
return Scaffold(
appBar: AppBar(title: const Text(artifactGalleryTitle)),
body: SafeArea(top: false, child: body),
);
}
}
@@ -0,0 +1,71 @@
import 'package:flutter/widgets.dart';
import '../../../core/api_gateway.dart';
import '../../graph/domain/stage_visits.dart';
import '../domain/artifact_groups.dart';
import '../domain/artifact_record.dart';
import '../domain/artifact_summary.dart';
import '../domain/artifact_url.dart';
import 'artifact_visit_list.dart';
/// The key one recorded group maps to, used only to find its step.
String artifactGroupKey(String stageKey, int visit) => '$stageKey\u0000$visit';
/// Each recorded group's step, keyed by stage and display visit, so a selected
/// row can show its own visit's capture facts. Derives the same grouping the
/// gallery uses; an absent key yields null rather than a fabricated capture.
Map<String, JsonMap> recordedStepsByGroup(List<JsonMap> steps) {
final result = <String, JsonMap>{};
for (final entry in visitsByStage(steps).entries) {
for (var index = 0; index < entry.value.length; index += 1) {
final step = entry.value[index];
result[artifactGroupKey(entry.key, displayVisit(step, index))] = step;
}
}
return result;
}
/// The recorded facts of one selected artifact: name, MIME/size, its visit's
/// capture summary and notes, and the url state. Nothing is inferred.
class ArtifactSelectionFacts extends StatelessWidget {
const ArtifactSelectionFacts({
super.key,
required this.group,
required this.artifact,
required this.step,
required this.resolved,
});
final ArtifactGroup group;
final ArtifactRecord artifact;
final JsonMap? step;
final ResolvedArtifactUrl? resolved;
@override
Widget build(BuildContext context) {
final recordedStep = step;
final url = resolved;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(artifactFactsLabel(artifact)),
const SizedBox(height: 8),
Text(artifactGroupLabel(group)),
if (recordedStep != null) ...[
const SizedBox(height: 8),
Text(artifactsSummary(recordedStep, count: group.artifacts.length)),
for (final note in artifactCaptureNotes(recordedStep))
Padding(padding: const EdgeInsets.only(top: 4), child: Text(note)),
],
if (!artifact.hasUrl) ...[
const SizedBox(height: 8),
const Text('No artifact URL recorded.'),
] else if (url == null) ...[
const SizedBox(height: 8),
const Text('The recorded artifact URL cannot be opened safely.'),
],
],
);
}
}
@@ -22,6 +22,25 @@ String artifactVisitIdentity(ArtifactGroup group, ArtifactRecord artifact) {
return '${group.stageKey}\u0000${group.visit}\u0000$recorded';
}
/// The recorded group heading, matching the web client's group vocabulary.
String artifactGroupLabel(ArtifactGroup group) =>
'${group.stageKey} · visit ${group.visit}';
/// The displayed artifact name: a missing server name stays explicit.
String artifactDisplayName(ArtifactRecord artifact) =>
artifact.name.isEmpty ? '(unnamed artifact)' : artifact.name;
/// The recorded MIME and byte size, never inferred from the name.
///
/// The value and its unit are joined by a non-breaking space so a narrow row
/// never wraps `120.6` away from `KiB`.
String artifactFactsLabel(ArtifactRecord artifact) {
final type = artifact.contentType.split(';').first.trim();
final mime = type.isEmpty ? 'type not recorded' : type;
final size = humanBytes(artifact.sizeBytes).replaceFirst(' ', '\u00a0');
return '$mime · $size';
}
/// A read-only, selectable list of recorded artifact groups.
///
/// Each group shows its recorded stage and visit, and each row its recorded
@@ -60,7 +79,7 @@ class ArtifactVisitList extends StatelessWidget {
header: Semantics(
container: true,
header: true,
child: Text(_groupLabel(group)),
child: Text(artifactGroupLabel(group)),
),
hasLeading: false,
children: [
@@ -85,7 +104,7 @@ class ArtifactVisitList extends StatelessWidget {
child: Semantics(
container: true,
header: true,
child: Text(_groupLabel(group)),
child: Text(artifactGroupLabel(group)),
),
),
for (var index = 0; index < group.artifacts.length; index += 1)
@@ -108,25 +127,6 @@ class ArtifactVisitList extends StatelessWidget {
ValueKey<String>('${artifactVisitIdentity(group, artifact)}\u0000$index');
}
/// The recorded group heading, matching the web client's group vocabulary.
String _groupLabel(ArtifactGroup group) =>
'${group.stageKey} · visit ${group.visit}';
/// The displayed artifact name: a missing server name stays explicit.
String _displayName(ArtifactRecord artifact) =>
artifact.name.isEmpty ? '(unnamed artifact)' : artifact.name;
/// The recorded MIME and byte size, never inferred from the name.
///
/// The value and its unit are joined by a non-breaking space so a narrow row
/// never wraps `120.6` away from `KiB`.
String _facts(ArtifactRecord artifact) {
final type = artifact.contentType.split(';').first.trim();
final mime = type.isEmpty ? 'type not recorded' : type;
final size = humanBytes(artifact.sizeBytes).replaceFirst(' ', '\u00a0');
return '$mime · $size';
}
class _MaterialRow extends StatelessWidget {
const _MaterialRow({
super.key,
@@ -145,8 +145,8 @@ class _MaterialRow extends StatelessWidget {
Widget build(BuildContext context) {
return ListTile(
selected: selected,
title: Text(_displayName(artifact)),
subtitle: Text(_facts(artifact)),
title: Text(artifactDisplayName(artifact)),
subtitle: Text(artifactFactsLabel(artifact)),
trailing: selected ? const Icon(Icons.check) : null,
onTap: onSelected == null ? null : () => onSelected!(group, artifact),
);
@@ -181,8 +181,8 @@ class _CupertinoRow extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(_displayName(artifact)),
Text(_facts(artifact), style: factsStyle),
Text(artifactDisplayName(artifact)),
Text(artifactFactsLabel(artifact), style: factsStyle),
],
),
),
@@ -1,6 +1,7 @@
import 'package:flutter/widgets.dart';
import '../../../core/api_gateway.dart';
import '../../artifacts/ui/artifact_gallery_action.dart';
import '../actions/cancel_run_action.dart';
import '../actions/title_action.dart';
import 'run_decision_notice.dart';
@@ -36,6 +37,8 @@ class RunDetailActions extends StatelessWidget {
gateway: gateway,
onRunChanged: onRunChanged,
),
if (showRunArtifactsAction(run))
RunArtifactsButton(run: run, gateway: gateway),
],
),
RunDecisionNotice(run: run, gateway: gateway, onRunChanged: onRunChanged),