Files
pipelines/app/lib/features/artifacts/ui/artifact_viewer_page.dart
T
alan fde086cc4a feat(app): glass viewer bars, graph controls and immersive image viewer
Review findings F12, F13, F25 and F1 for viewers: the artifact viewer's
actions/pager and the Changes pager float as glass pills over content,
file facts move to the top bar, graph overlay controls are one glass pill
(duplicate fullscreen glyph removed), the image viewer is immersive, and
diff/artifact/graph views let content pass behind the bar. Implemented by
pipeline run #204 (Terra); its Review stopped after three rejections, the
last being an empty glass bar for a single unresolved artifact, which the
manager fixed here with a regression test.
2026-10-31 04:26:22 +00:00

297 lines
8.5 KiB
Dart

import '../../../ui/adaptive/segmented_bar.dart';
import '../../../ui/theme/app_theme.dart';
import '../../../ui/input/arrow_key_pager.dart';
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../../ui/adaptive/host.dart';
import 'artifact_selection_facts.dart';
import '../actions/artifact_file_bridge.dart';
import '../domain/artifact_groups.dart';
import '../domain/artifact_preview.dart';
import '../domain/artifact_url.dart';
import '../domain/gallery_navigation.dart';
import 'artifact_external_open.dart';
import 'artifact_viewer_body.dart';
import 'artifact_viewer_content.dart';
import 'artifact_viewer_fetch.dart';
import 'artifact_visit_list.dart';
/// The full-screen viewer's title, matching the web client's wording.
const String artifactViewerTitle = 'Artifact viewer';
/// One artifact of a recorded visit, previewed in-app.
///
/// Images are drawn from bytes fetched from the already-resolved safe url, a
/// `text/plain` artifact is selectable text, and a `.md`/`.markdown` or
/// `text/markdown` artifact is handed to [markdownBuilder]. The selected page
/// keeps its recorded facts visible and offers the existing external Open and
/// Copy link actions plus Download/Share of the same-origin bytes as a named
/// file. Previous/Next walk the same filtered list the gallery draws and stop at
/// both ends; closing returns to the gallery row.
///
/// Nothing is ever drawn for an external, unsafe or unsupported url: the viewer
/// reports the exact state instead. External urls are never fetched.
class ArtifactViewerPage extends StatefulWidget {
const ArtifactViewerPage({
super.key,
required this.groups,
required this.selectedIdentity,
this.configuredEndpoint = '',
this.fetch = fetchArtifact,
this.markdownBuilder = defaultArtifactMarkdown,
this.openExternal = openArtifactExternally,
this.fileBridge = const ArtifactFileBridge(),
});
final List<ArtifactGroup> groups;
/// [artifactVisitIdentityOf] of the row the operator selected.
final String selectedIdentity;
final String configuredEndpoint;
final ArtifactFetch fetch;
final ArtifactMarkdownBuilder markdownBuilder;
final ArtifactExternalOpen openExternal;
final ArtifactFileBridge fileBridge;
@override
State<ArtifactViewerPage> createState() => _ArtifactViewerPageState();
}
class _FetchedPreview {
const _FetchedPreview({this.text, this.bytes});
final String? text;
final Uint8List? bytes;
}
class _ArtifactViewerPageState extends State<ArtifactViewerPage> {
late final List<PagedArtifact> _pages = pagedArtifacts(widget.groups);
final Map<String, _FetchedPreview> _cache = <String, _FetchedPreview>{};
late int _index = _initialIndex();
int _generation = 0;
bool _loading = false;
String? _error;
_FetchedPreview? _fetched;
String? _status;
int _initialIndex() {
final found = _pages.indexWhere(
(page) =>
artifactVisitIdentityOf(page.group, page.artifact) ==
widget.selectedIdentity,
);
return found < 0 ? 0 : found;
}
PagedArtifact? get _page => _index < _pages.length ? _pages[_index] : null;
ArtifactPreviewPlan? get _plan {
final page = _page;
if (page == null) return null;
return planArtifactPreview(
artifact: page.artifact,
configuredEndpoint: widget.configuredEndpoint,
);
}
@override
void initState() {
super.initState();
_beginLoad(initial: true);
}
void _beginLoad({bool initial = false}) {
final generation = ++_generation;
final plan = _plan;
if (plan == null || !plan.canFetch) {
void clear() {
_loading = false;
_error = null;
_fetched = null;
}
if (initial) {
clear();
} else {
setState(clear);
}
return;
}
final key = plan.resolved!.url;
final cached = _cache[key];
if (cached != null) {
void apply() {
_loading = false;
_error = null;
_fetched = cached;
}
if (initial) {
apply();
} else {
setState(apply);
}
return;
}
void start() {
_loading = true;
_error = null;
_fetched = null;
}
if (initial) {
start();
} else {
setState(start);
}
widget
.fetch(plan.resolved!.uri)
.then(
(result) {
if (!mounted || generation != _generation) return;
if (!result.ok) {
setState(() {
_loading = false;
_error = 'The server answered ${result.status}.';
});
return;
}
final fetched = _decode(plan.kind, result.bytes);
_cache[key] = fetched;
setState(() {
_loading = false;
_fetched = fetched;
});
},
onError: (Object error) {
if (!mounted || generation != _generation) return;
setState(() {
_loading = false;
_error = 'Could not load preview: $error';
});
},
);
}
static _FetchedPreview _decode(ArtifactPreviewKind kind, Uint8List bytes) {
if (kind == ArtifactPreviewKind.image) {
return _FetchedPreview(bytes: bytes);
}
return _FetchedPreview(text: utf8.decode(bytes, allowMalformed: true));
}
void _step(int delta) {
final next = pagedIndex(_index, delta, _pages.length);
if (next == _index) return;
setState(() {
_index = next;
_status = null;
});
_beginLoad();
}
Future<void> _open(ResolvedArtifactUrl resolved) async {
final opened = await widget.openExternal(resolved.uri);
if (!mounted) return;
setState(
() =>
_status = opened ? 'Link opened externally.' : 'Could not open link.',
);
}
Future<void> _copy(ResolvedArtifactUrl resolved) async {
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 page = _page;
final plan = _plan;
final title = page == null
? artifactViewerTitle
: artifactDisplayName(page.artifact);
final subtitle = page == null
? null
: artifactViewerPositionLabel(
_index,
_pages.length,
page.artifact.contentType,
);
final info = page == null || plan == null
? const <Widget>[]
: <Widget>[
ArtifactViewerInfoAction(
facts: ArtifactSelectionFacts(
group: page.group,
artifact: page.artifact,
step: null,
resolved: plan.resolved,
),
),
];
Widget body = page == null || plan == null
? const Center(child: Text('No previewable artifacts recorded.'))
: ArtifactViewerBody(
page: page,
plan: plan,
index: _index,
count: _pages.length,
loading: _loading,
markdownBuilder: widget.markdownBuilder,
text: _fetched?.text,
bytes: _fetched?.bytes,
error: _error,
status: _status,
fetch: widget.fetch,
fileBridge: widget.fileBridge,
onPrevious: () => _step(-1),
onNext: () => _step(1),
onOpen: plan.resolved == null ? () {} : () => _open(plan.resolved!),
onCopy: plan.resolved == null ? () {} : () => _copy(plan.resolved!),
);
// Left/right arrows walk the same list as the Previous/Next buttons.
body = ArrowKeyPager(
onPrevious: () => _step(-1),
onNext: () => _step(1),
// Escape closes the viewer, like the web's overlay.
onEscape: () => Navigator.of(context).maybePop(),
child: body,
);
if (usesCupertinoHost(context)) {
return CupertinoPageScaffold(
backgroundColor: AppColors.groupedBackground,
navigationBar: SegmentedBar(
cupertino: true,
title: title,
subtitle: subtitle,
actions: info,
),
child: body,
);
}
return Scaffold(
extendBodyBehindAppBar: true,
appBar: SegmentedBar(
cupertino: false,
title: title,
subtitle: subtitle,
actions: info,
),
body: body,
);
}
}