import '../../../core/servers/server_context.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../../../core/api_gateway.dart'; import '../../../core/transport/pipelines_client.dart'; import '../../../shell/shell_bar.dart'; import '../../../ui/adaptive/segmented_bar.dart'; import '../../../ui/adaptive/segmented_bar_overlay.dart'; import 'run_detail_actions.dart'; import 'run_more_menu.dart'; import 'run_pipeline_link.dart'; import 'run_detail_content.dart'; import 'run_detail_material.dart'; import 'runs_controller.dart'; class RunDetailView extends StatefulWidget { const RunDetailView({ super.key, required this.controller, required this.gateway, required this.onBack, this.onRunOpened, this.onOpenPipeline, this.initialStageKey, this.initialVisit, this.onOpenInChat, this.onSendToChat, }); final RunsController controller; /// The app's shared gateway; the rename action sends through it. final ApiGateway gateway; final VoidCallback onBack; /// Opens the run named by an accepted retry reply, so the host route follows /// the server-identified new run. Null keeps the detail on the retried run. final ValueChanged? onRunOpened; /// Opens the run's pipeline from its name in the header. final OpenPipeline? onOpenPipeline; /// The route's stage and visit, forwarded to the tab body so a `/runs/:id/ /// stage/:key[?visit=N]` deep link opens that recorded visit. Null is the /// plain detail. final String? initialStageKey; final int? initialVisit; final Future Function(BuildContext, JsonMap)? onOpenInChat; final Future Function(BuildContext, JsonMap, String)? onSendToChat; @override State createState() => _RunDetailViewState(); } class _RunDetailViewState extends State { bool _copying = false; String? _copyStatus; @override Widget build(BuildContext context) => AnimatedBuilder( animation: widget.controller, builder: (context, _) { final run = widget.controller.detail; if (run == null) return _placeholder(context); final prompt = detailPrompt(run); final endpoint = configuredGatewayEndpoint(widget.gateway); final headerActions = RunDetailActions( run: run, gateway: widget.gateway, onRunChanged: widget.controller.acceptRunDetail, onRunOpened: widget.onRunOpened, retry: false, ); final inShellBar = ShellBar.available(context); final detail = buildMaterialRunDetail( context: context, run: run, headerActions: headerActions, detailLoading: widget.controller.detailLoading, error: widget.controller.error, copying: _copying, copyStatus: _copyStatus, onCopy: () => _copyPrompt(prompt ?? ''), gateway: widget.gateway, onRunChanged: widget.controller.acceptRunDetail, configuredEndpoint: endpoint, initialStageKey: widget.initialStageKey, initialVisit: widget.initialVisit, onSendToChat: widget.onSendToChat == null ? null : (draft) => widget.onSendToChat!(context, run, draft), ); // The top bar becomes the run's: Back to the list, its title over its // number, pipeline and version (a link to that pipeline when the host // can open it) and its more menu, so the body repeats none of them. // Search and the tab name return when the run closes. final title = detailTitle(run); final subtitle = detailSubtitle(run); final openPipeline = _openPipeline(run); final pipelineName = detailPipeline(run); final pipelineAction = openPipeline == null ? null : Semantics( button: true, label: 'Open pipeline $pipelineName', excludeSemantics: true, child: GestureDetector( key: runPipelineLinkKey, behavior: HitTestBehavior.opaque, onTap: openPipeline, child: const Padding( padding: EdgeInsets.all(12), child: Icon(Icons.account_tree_outlined), ), ), ); final more = RunMoreMenu( run: run, gateway: widget.gateway, onRunChanged: widget.controller.acceptRunDetail, onRunOpened: widget.onRunOpened, onDeleted: widget.controller.applyDeletion, onOpenInChat: widget.onOpenInChat, ); if (inShellBar) { return ShellBar( title: title, subtitle: ServerContext.metadata(context, subtitle), onTitlePressed: openPipeline, expandableTitle: true, titleAction: pipelineAction, titleSemanticLabel: 'Run title $title', onBack: widget.onBack, actions: [more], hideNavigation: true, child: detail, ); } // On its own the run draws the same bar over its content. return SegmentedBarOverlay( bar: SegmentedBar( leading: SegmentedBackButton( key: const Key('run-detail-back'), onPressed: widget.onBack, ), title: title, subtitle: ServerContext.metadata(context, subtitle), onTitlePressed: openPipeline, expandableTitle: true, titleAction: pipelineAction, titleSemanticLabel: 'Run title $title', actions: [more], ), child: detail, ); }, ); /// Opens the run's pipeline at its pinned version, or null when the run /// names none or the host cannot navigate. VoidCallback? _openPipeline(JsonMap run) { final id = runPipelineId(run); final open = widget.onOpenPipeline; if (id == null || open == null) return null; final version = run['version']; return () => open(id, version is num ? version.toInt() : 0); } Widget _placeholder(BuildContext context) { final loading = widget.controller.detailLoading; final message = loading ? 'Loading run details…' : widget.controller.error ?? 'Run details unavailable.'; // Back stays available while the detail is pending as well as after a // failure, so a slow or refused request can never trap the destination. return ShellBar( title: 'Run', onBack: widget.onBack, hideNavigation: true, child: _placeholderBody(loading, message), ); } Widget _placeholderBody(bool loading, String message) { return Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ if (loading) ...[ const CircularProgressIndicator(), const SizedBox(height: 12), ], Padding( padding: const EdgeInsets.symmetric(horizontal: 24), child: Text(message, textAlign: TextAlign.center), ), const SizedBox(height: 12), FilledButton( onPressed: widget.onBack, child: const Text('Back to list'), ), ], ), ); } /// Copies the stored prompt byte-for-byte. A refusal is surfaced, never /// replaced by copying a title or the formatted detail. Future _copyPrompt(String prompt) async { if (_copying) return; setState(() => _copying = true); String message; try { await Clipboard.setData(ClipboardData(text: prompt)); message = 'Prompt copied to clipboard.'; } catch (_) { message = 'Could not copy prompt to clipboard.'; } if (!mounted) return; final scaffold = Scaffold.maybeOf(context); setState(() { _copying = false; _copyStatus = scaffold == null ? message : null; }); if (scaffold != null) { ScaffoldMessenger.maybeOf( context, )?.showSnackBar(SnackBar(content: Text(message))); } } }