import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../../../ui/adaptive/host.dart'; import '../../artifacts/domain/artifact_record.dart'; import '../../artifacts/domain/artifact_url.dart'; import '../../artifacts/ui/artifact_external_open.dart'; import 'run_detail_visit_session_state.dart'; /// The visit's recorded session pointer, its recorded export state and, when /// the visit exported one, a read-only external open of that document. /// /// The state comes from [detailVisitSession], so a legacy `session.html`, a /// repair, a still-running session and a failed one each report what the server /// recorded. Complete, partial, missing, skipped, repair and unrecognised /// states each keep the server's own word and diagnostic; recorded repair /// attempts and their open-option labels are shown read-only and make no /// request. The open goes through the landed safe-link helper: the recorded url /// is resolved by [resolveArtifactUrl] and passed to [openArtifactExternally] /// without being fetched, rewritten or previewed. A refusal is reported. class RunVisitSession extends StatefulWidget { const RunVisitSession({ super.key, required this.step, required this.configuredEndpoint, this.openExternal = openArtifactExternally, }); final JsonMap step; final String configuredEndpoint; final ArtifactExternalOpen openExternal; @override State createState() => _RunVisitSessionState(); } class _RunVisitSessionState extends State { String? _status; Future _open(ResolvedArtifactUrl resolved) async { final opened = await widget.openExternal(resolved.uri); if (!mounted) return; setState( () => _status = opened ? 'Link opened externally.' : 'Could not open the link.', ); } @override Widget build(BuildContext context) { final session = detailVisitSession(widget.step); final export = session.export; final resolved = export == null ? null : resolveArtifactUrl(widget.configuredEndpoint, export.artifact.url); final note = export?.statusNote; final fact = session.exportFact; final showReason = export != null && fact?.reason != null && fact!.state != VisitSessionExportState.repair; final children = [ if (session.sessionId != null) _SessionFact(label: 'Session', value: session.sessionId!), if (fact != null) _SessionFact( key: const Key('run-visit-session-export'), label: 'Export', value: fact.word, ), if (showReason) _SessionFact( key: const Key('run-visit-session-export-reason'), label: 'Reason', value: fact.reason!, ), if (export != null && note != null) Text(note), ..._repairFacts(session.repairs), if (export == null) Text(session.message) else if (resolved == null) const Text(visitSessionEndpointMissing) else _openButton(export.artifact, resolved), if (_status != null) Padding( padding: const EdgeInsets.only(top: 8), child: Semantics(liveRegion: true, child: Text(_status!)), ), ]; if (usesCupertinoHost(context)) { return ListView( key: const Key('run-visit-body-session'), children: [ CupertinoListSection.insetGrouped( children: [ Padding( padding: const EdgeInsetsDirectional.fromSTEB(16, 12, 16, 16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: _spaced(children), ), ), ], ), ], ); } return ListView( key: const Key('run-visit-body-session'), padding: const EdgeInsets.all(16), children: _spaced(children), ); } Widget _openButton(ArtifactRecord export, ResolvedArtifactUrl resolved) { final label = 'Open ${export.name.isEmpty ? 'session.html' : export.name}'; if (usesCupertinoHost(context)) { return CupertinoButton( padding: EdgeInsets.zero, onPressed: () => _open(resolved), child: Text(label), ); } return Align( alignment: AlignmentDirectional.centerStart, child: TextButton(onPressed: () => _open(resolved), child: Text(label)), ); } /// Recorded repair attempts, in the pane's own fact rows. Nothing here is a /// control: the repair request belongs to its own action and is not wired. List _repairFacts(List repairs) { final facts = []; for (var index = 0; index < repairs.length; index += 1) { final repair = repairs[index]; facts.add( _SessionFact( key: ValueKey('run-visit-session-repair-$index'), label: 'Repair', value: repair.label, ), ); final option = repair.option; if (option != null) { facts.add( _SessionFact( key: ValueKey('run-visit-session-repair-option-$index'), label: 'Repair option', value: option, ), ); } } return facts; } List _spaced(List children) => [ for (var index = 0; index < children.length; index += 1) ...[ if (index > 0) const SizedBox(height: 8), children[index], ], ]; } class _SessionFact extends StatelessWidget { const _SessionFact({super.key, required this.label, required this.value}); final String label; final String value; @override Widget build(BuildContext context) { final theme = Theme.of(context); final cupertino = CupertinoTheme.of(context); return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( label, style: usesCupertinoHost(context) ? cupertino.textTheme.tabLabelTextStyle.copyWith( color: CupertinoColors.secondaryLabel.resolveFrom(context), ) : (theme.textTheme.labelMedium ?? const TextStyle()).copyWith( color: theme.colorScheme.onSurfaceVariant, ), ), const SizedBox(height: 2), Text(value), ], ); } }