Files
pipelines/app/lib/features/artifacts/ui/artifact_gallery_page.dart
T

247 lines
8.0 KiB
Dart

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_kind.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_viewer_content.dart';
import 'artifact_viewer_fetch.dart';
import 'artifact_viewer_page.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 viewer artifact with a resolved
/// url opens the native [ArtifactViewerPage] (an external origin is marked and
/// never fetched); any other selection 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 downloads, shares or filters.
class ArtifactGalleryPage extends StatefulWidget {
const ArtifactGalleryPage({
super.key,
required this.steps,
this.executing = false,
this.configuredEndpoint = '',
this.openExternal = openArtifactExternally,
this.fetch = fetchArtifact,
this.markdownBuilder = defaultArtifactMarkdown,
});
final List<JsonMap> steps;
final bool executing;
final String configuredEndpoint;
final ArtifactExternalOpen openExternal;
final ArtifactFetch fetch;
final ArtifactMarkdownBuilder markdownBuilder;
@override
State<ArtifactGalleryPage> createState() => _ArtifactGalleryPageState();
}
class _ArtifactGalleryPageState extends State<ArtifactGalleryPage> {
late final Map<String, JsonMap> _stepsByGroup = recordedStepsByGroup(
widget.steps,
);
late final List<ArtifactGroup> _groups = artifactGroups(widget.steps);
String? _selectedIdentity;
String? _status;
Future<void> _select(ArtifactGroup group, ArtifactRecord artifact) async {
final identity = artifactVisitIdentity(group, artifact);
final resolved = artifact.hasUrl
? resolveArtifactUrl(widget.configuredEndpoint, artifact.url)
: null;
setState(() {
_selectedIdentity = identity;
_status = null;
});
if (resolved != null &&
isViewerArtifact(artifact.contentType, artifact.name)) {
await _openViewer(identity);
return;
}
await _showSelected(group, artifact, resolved);
}
Future<void> _openViewer(String identity) async {
final previousFocus = FocusManager.instance.primaryFocus;
await Navigator.of(context).push<void>(
usesCupertinoHost(context)
? CupertinoPageRoute<void>(
builder: (_) => ArtifactViewerPage(
groups: _groups,
selectedIdentity: identity,
configuredEndpoint: widget.configuredEndpoint,
openExternal: widget.openExternal,
fetch: widget.fetch,
markdownBuilder: widget.markdownBuilder,
),
)
: MaterialPageRoute<void>(
builder: (_) => ArtifactViewerPage(
groups: _groups,
selectedIdentity: identity,
configuredEndpoint: widget.configuredEndpoint,
openExternal: widget.openExternal,
fetch: widget.fetch,
markdownBuilder: widget.markdownBuilder,
),
),
);
if (!mounted) return;
if (previousFocus != null && previousFocus.canRequestFocus) {
previousFocus.requestFocus();
}
}
Future<void> _showSelected(
ArtifactGroup group,
ArtifactRecord artifact,
ResolvedArtifactUrl? resolved,
) {
final step = _stepsByGroup[artifactGroupKey(group.stageKey, group.visit)];
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 = _groups;
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),
);
}
}