import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../../../core/transport/pipelines_client.dart'; import '../domain/artifact_chat_context.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, this.onSendToChat, }); final JsonMap run; final ApiGateway gateway; final void Function(String draft)? onSendToChat; @override Widget build(BuildContext context) { final steps = runDetailArtifactSteps(run); void open() => showArtifactGallery( context, steps: steps, executing: _runExecuting(run), configuredEndpoint: configuredGatewayEndpoint(gateway), artifactChatContext: ArtifactChatContext( runId: '${run['id'] ?? ''}', runTitle: '${run['title'] ?? run['name'] ?? ''}', pipelineName: '${run['name'] ?? ''}', pipelineVersion: run['version'] is num ? (run['version'] as num).toInt().toString() : null, configuredEndpoint: configuredGatewayEndpoint(gateway), ), onSendToChat: onSendToChat, ); 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, ArtifactChatContext? artifactChatContext, void Function(String draft)? onSendToChat, }) { final page = ArtifactGalleryPage( steps: List.unmodifiable(steps), focusIndex: focusIndex, executing: executing, configuredEndpoint: configuredEndpoint, openExternal: openExternal, artifactChatContext: artifactChatContext, onSendToChat: onSendToChat, ); return Navigator.of( context, ).push(MaterialPageRoute(builder: (_) => page)); }