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 runArtifactsButtonKey = ValueKey( 'run-detail-artifacts', ); const Set _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 runDetailArtifactSteps(JsonMap run) { final steps = run['steps']; if (steps is List) return steps; if (steps is List) return steps.cast(); return const []; } /// 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 runDetailExecuting(JsonMap run) => _runExecuting(run); bool _runExecuting(JsonMap run) => run['executing'] == true || _executingStatuses.contains('${run['status'] ?? ''}'.trim().toLowerCase()); /// 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: configuredGatewayEndpoint(gateway), ); if (usesCupertinoHost(context)) { return CupertinoButton.tinted( sizeStyle: CupertinoButtonSize.small, key: runArtifactsButtonKey, onPressed: open, child: const Text(artifactGalleryTitle), ); } return OutlinedButton.icon( 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 showArtifactGallery( BuildContext context, { required List steps, int? focusIndex, bool executing = false, String configuredEndpoint = '', ArtifactExternalOpen openExternal = openArtifactExternally, }) { final page = ArtifactGalleryPage( steps: List.unmodifiable(steps), focusIndex: focusIndex, executing: executing, configuredEndpoint: configuredEndpoint, openExternal: openExternal, ); return Navigator.of(context).push( usesCupertinoHost(context) ? CupertinoPageRoute(builder: (_) => page) : MaterialPageRoute(builder: (_) => page), ); }