import 'package:flutter/material.dart'; import '../../../ui/adaptive/floating_bar_layout.dart'; import '../../../ui/layout/app_list_avatar.dart'; import '../../../ui/layout/app_list_row.dart'; import '../../../ui/status/outcome_palette.dart'; import '../../../ui/status/run_status_icon.dart'; import '../../../ui/text/live_elapsed_builder.dart'; import '../../pipelines/pipeline_format.dart' show formatDuration; import '../domain/run_models.dart'; import 'run_detail_content.dart'; /// The Timeline tab's recorded visits, one shared list row per visit. /// /// The list is flat on the page on both hosts, like the runs list and the /// Summary, with inset dividers between rows (no grouped card). /// /// [onSelected] receives the row's index in [visits], so the caller opens /// exactly the recorded step it was built from. [selectedSequence] highlights a /// row when a selection is applied while the list is rendered; the current /// flow replaces the rows with the pane, so it only matters for a defensive /// host that keeps both visible. Only an actual recorded visit can match, so an /// unvisited stage leaves every row unselected. class RunTimelineList extends StatelessWidget { const RunTimelineList({ super.key, required this.visits, this.selectedSequence, this.onSelected, }); final List visits; final int? selectedSequence; final ValueChanged? onSelected; @override Widget build(BuildContext context) { if (visits.isEmpty) { return ListView( key: const Key('run-detail-tab-body-timeline'), padding: withBarClearance(context, const EdgeInsets.all(16)), children: const [Text('No visits recorded.')], ); } // One colour per distinct outcome name across the whole run, so the same // outcome reads the same on every row and different ones never match. final palette = OutcomePalette(visits.map((visit) => visit.outcome)); return ListView( key: const Key('run-detail-tab-body-timeline'), padding: withBarClearance( context, const EdgeInsets.symmetric(vertical: 8), ), children: [ for (var index = 0; index < visits.length; index += 1) RunTimelineRow( key: ValueKey( 'run-timeline-visit-${visits[index].sequence ?? index}', ), visit: visits[index], selected: visits[index].sequence == selectedSequence, divider: index < visits.length - 1, palette: palette, onTap: onSelected == null ? null : () => onSelected!(index), ), ], ); } } /// One recorded visit. [selected] marks the row an applied selection identifies; /// the current flow opens the pane instead of leaving a highlighted row. It is /// never true for a stage with no recorded visit. class RunTimelineRow extends StatelessWidget { const RunTimelineRow({ super.key, required this.visit, required this.selected, this.divider = false, this.onTap, this.palette, }); final DetailVisit visit; final bool selected; final bool divider; final VoidCallback? onTap; /// The run's outcome colours; without one the row colours its own outcome /// as the first (and only) name. final OutcomePalette? palette; /// The visit's state for its glyph: the outcome when it has one (a completed /// step can still have failed), else the step status. String get _state { final outcome = visit.outcome.toLowerCase(); if (outcome == 'error' || outcome == 'failed' || outcome == 'failure') { return 'failed'; } return runStateOf(visit.status); } Widget _outcome(BuildContext context) { final label = visit.outcome.isNotEmpty ? visit.outcome : (visit.status.isEmpty ? 'unknown' : visit.status); // An outcome is coloured by its name (no word is wired to a colour); a // visit with no outcome yet keeps its status colour. final color = visit.outcome.isNotEmpty ? (palette ?? OutcomePalette([visit.outcome])).colorFor( context, visit.outcome, ) : runStatusColor(_state, Theme.of(context).colorScheme); return Container( padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2), decoration: BoxDecoration( color: color.withValues(alpha: 0.14), borderRadius: BorderRadius.circular(10), ), child: Text( label, style: TextStyle( color: color, fontSize: 12, fontWeight: FontWeight.w600, ), ), ); } @override Widget build(BuildContext context) { final state = _state; // The outcome chip leads the second line, then where the visit went and // the tokens it reported; the duration is the row's time. return AppListRow( selected: selected, divider: divider, onTap: onTap, leading: AppListAvatar( foreground: runStatusColor(state, Theme.of(context).colorScheme), child: RunStatusIcon( state: state, label: visit.status.isEmpty ? 'Status unavailable' : visit.status, ), ), title: Text(visit.title), time: visit.duration, // A running visit's elapsed time ticks; only that label rebuilds. timeWidget: visit.runningSince == null ? null : LiveElapsedBuilder( since: visit.runningSince, builder: (context, now) => Text( formatDuration(now.difference(visit.runningSince!).inSeconds), ), ), subtitleLeading: _outcome(context), subtitle: visit.target.isEmpty && visit.tokens == null ? null : Text( [ if (visit.target.isNotEmpty) '→ ${visit.target}', ?visit.tokens, ].join(' · '), ), ); } }