155 lines
5.2 KiB
Dart
155 lines
5.2 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import 'run_detail_visit_facts.dart';
|
|
|
|
/// A search hit's requested selection inside one run's detail.
|
|
///
|
|
/// A recorded hit names only the run's own recorded [sequence] and, for a text
|
|
/// hit, the [field] it matched; the stage and timeline position are resolved
|
|
/// against the loaded detail, exactly as the browser client does. A request is
|
|
/// consumed once, so reopening the run later never replays it.
|
|
@immutable
|
|
class RunSearchSelection {
|
|
const RunSearchSelection({
|
|
required this.runId,
|
|
this.sequence,
|
|
this.field,
|
|
this.artifactUrl,
|
|
});
|
|
|
|
final int runId;
|
|
|
|
/// The server's recorded visit number, or null when the hit names no visit.
|
|
final int? sequence;
|
|
|
|
/// The matched step field (`input`, `output`, …), or null for a visit hit.
|
|
final String? field;
|
|
|
|
/// The recorded artifact URL for an artifact or turn-document hit.
|
|
final String? artifactUrl;
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
other is RunSearchSelection &&
|
|
other.runId == runId &&
|
|
other.sequence == sequence &&
|
|
other.field == field &&
|
|
other.artifactUrl == artifactUrl;
|
|
|
|
@override
|
|
int get hashCode => Object.hash(runId, sequence, field, artifactUrl);
|
|
}
|
|
|
|
/// The collapsible Summary section a text hit names.
|
|
enum RunSummarySection { input, output }
|
|
|
|
/// What a resolved search request selects in the loaded run detail.
|
|
sealed class RunSearchTarget {
|
|
const RunSearchTarget();
|
|
}
|
|
|
|
/// The run Summary, optionally with the matched field's section expanded.
|
|
final class RunSummaryTarget extends RunSearchTarget {
|
|
const RunSummaryTarget({this.section});
|
|
|
|
/// The section a text hit matched, or null for the plain Summary.
|
|
final RunSummarySection? section;
|
|
}
|
|
|
|
/// One recorded visit: [stageKey] plus the detail row index the Timeline and
|
|
/// the Graph selection also use.
|
|
final class RunVisitTarget extends RunSearchTarget {
|
|
const RunVisitTarget({required this.stageKey, required this.timelineIndex});
|
|
|
|
final String stageKey;
|
|
final int timelineIndex;
|
|
}
|
|
|
|
/// A recorded artifact hit selects its visit first, then opens the artifact
|
|
/// viewer on the exact recorded file.
|
|
final class RunArtifactTarget extends RunSearchTarget {
|
|
const RunArtifactTarget({required this.visit, required this.url});
|
|
|
|
final RunVisitTarget visit;
|
|
final String url;
|
|
}
|
|
|
|
/// Resolves [selection] against the loaded [run] into the detail state to show.
|
|
///
|
|
/// A text hit always selects the Summary, using the browser's `searchFieldTab`
|
|
/// mapping: `input` and `output` expand that section, while a retired or
|
|
/// unknown field leaves the plain Summary instead of guessing a section. A
|
|
/// visit hit selects the step with the recorded sequence; a sequence the run did
|
|
/// not record falls back to the plain Summary, as the browser does, so a stale
|
|
/// hit never leaves an unrelated visit pane open.
|
|
RunSearchTarget resolveRunSearchSelection(
|
|
RunSearchSelection selection,
|
|
JsonMap run,
|
|
) {
|
|
final field = selection.field?.trim() ?? '';
|
|
if (field.isNotEmpty) {
|
|
return RunSummaryTarget(
|
|
section: switch (field) {
|
|
'input' => RunSummarySection.input,
|
|
'output' => RunSummarySection.output,
|
|
_ => null,
|
|
},
|
|
);
|
|
}
|
|
final artifactUrl = selection.artifactUrl?.trim() ?? '';
|
|
if (artifactUrl.isNotEmpty) {
|
|
final visit = _visitTarget(selection, run);
|
|
return visit == null
|
|
? const RunSummaryTarget()
|
|
: RunArtifactTarget(visit: visit, url: artifactUrl);
|
|
}
|
|
final visit = _visitTarget(selection, run);
|
|
if (visit != null) return visit;
|
|
return const RunSummaryTarget();
|
|
}
|
|
|
|
RunVisitTarget? _visitTarget(RunSearchSelection selection, JsonMap run) {
|
|
final sequence = selection.sequence;
|
|
if (sequence == null || sequence <= 0) return null;
|
|
final steps = detailRunSteps(run);
|
|
for (var index = 0; index < steps.length; index++) {
|
|
if (_sequence(steps[index]) != sequence) continue;
|
|
final stage = '${steps[index]['stage'] ?? ''}'.trim();
|
|
if (stage.isEmpty) return null;
|
|
return RunVisitTarget(stageKey: stage, timelineIndex: index);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
int? _sequence(JsonMap step) {
|
|
final value = step['sequence'];
|
|
final number = value is num ? value.toInt() : int.tryParse('${value ?? ''}');
|
|
return number != null && number > 0 ? number : null;
|
|
}
|
|
|
|
/// Carries the shell's one pending search request to the mounted run detail.
|
|
///
|
|
/// The composition mounts it around the shell; [RunDetailTabs] reads it and
|
|
/// reports [onConsumed] once the request has been applied, so a later visit to
|
|
/// the same run starts plain. Keeping the request here means a field or artifact
|
|
/// hit needs no invented URL: the browser has no route for either selection.
|
|
class RunSearchSelectionScope extends InheritedWidget {
|
|
const RunSearchSelectionScope({
|
|
super.key,
|
|
required this.selection,
|
|
required this.onConsumed,
|
|
required super.child,
|
|
});
|
|
|
|
final RunSearchSelection? selection;
|
|
final VoidCallback onConsumed;
|
|
|
|
static RunSearchSelectionScope? maybeOf(BuildContext context) =>
|
|
context.dependOnInheritedWidgetOfExactType<RunSearchSelectionScope>();
|
|
|
|
@override
|
|
bool updateShouldNotify(RunSearchSelectionScope oldWidget) =>
|
|
oldWidget.selection != selection || oldWidget.onConsumed != onConsumed;
|
|
}
|