import '../../../core/api_gateway.dart'; import 'search_format.dart'; export 'search_format.dart' show humanBytes, formatSearchTimestamp; /// One run of a field snippet, split so the first case-insensitive occurrence of /// the query can be emphasised without colour or markup. class SearchSnippetSpan { const SearchSnippetSpan(this.text, {required this.matched}); final String text; final bool matched; } /// A render-ready search row. [target] is the server's own stable target token; /// it is null for an unknown/absent payload or a reply without a target, and /// such a row is read-only — the client never guesses a route. class SearchRowView { const SearchRowView({ required this.title, required this.secondary, required this.kindLabel, required this.snippetSpans, required this.target, this.snippet, this.recency, }); final String title; final String secondary; final String kindLabel; final String? snippet; final List snippetSpans; final String? recency; final String? target; bool get openable => target != null; } /// One rendered search row, mirroring the browser's per-kind row text. Every /// server-provided string is only ever compared or rendered as text. SearchRowView searchRowView(JsonMap hit, String query) { final pipeline = _map(hit['pipeline']); final version = _map(hit['version']); final run = _map(hit['run']); final step = _map(hit['step']); final field = _map(hit['field']); final artifact = _map(hit['artifact']); final recency = formatSearchTimestamp('${hit['recency'] ?? ''}'); final target = _serverTarget(hit); if (pipeline != null) { return SearchRowView( title: _nonBlank(pipeline['name']) ?? 'pipeline ${_num(pipeline['id'])}', secondary: 'pipeline ${_num(pipeline['id'])} · v${_num(pipeline['version'])}' ' · ${_stageCount(pipeline['stages'])}', kindLabel: 'Pipeline', snippetSpans: const [], recency: recency, target: target, ); } if (version != null) { final hash = _nonBlank(version['content_hash']); return SearchRowView( title: 'pipeline ${_num(version['pipeline_id'])}' ' · v${_num(version['version'])}', secondary: _join([ hash == null ? null : 'content $hash', formatSearchTimestamp('${version['created_at'] ?? ''}'), ]), kindLabel: 'Pipeline version', snippetSpans: const [], recency: recency, target: target, ); } if (run != null) { final name = _nonBlank(run['pipeline_name']); return SearchRowView( title: 'run #${_num(run['id'])}${name == null ? '' : ' · $name'}', secondary: _join([ _nonBlank(run['status']), _nonBlank(run['workspace']), formatSearchTimestamp('${run['created_at'] ?? ''}'), ]), kindLabel: 'Run', snippetSpans: const [], recency: recency, target: target, ); } if (step != null) { final stage = _nonBlank(step['stage']) ?? 'step'; return SearchRowView( title: 'run #${_num(step['run_id'])} · $stage' ' visit ${_num(step['visit'])}', secondary: _stepSecondary(step), kindLabel: 'Step', snippetSpans: const [], recency: recency, target: target, ); } if (field != null) { final snippet = '${field['snippet'] ?? ''}'; return SearchRowView( title: 'run #${_num(field['run_id'])} · step ${_num(field['sequence'])}' ' · ${_nonBlank(field['field']) ?? 'field'}', secondary: snippet, kindLabel: 'Field', snippet: snippet, snippetSpans: snippetSpans(snippet, query), recency: recency, target: target, ); } if (artifact != null) { return SearchRowView( title: _nonBlank(artifact['name']) ?? 'artifact', secondary: _join([ 'run #${_num(artifact['run_id'])}', 'step ${_num(artifact['sequence'])}', _nonBlank(artifact['content_type']), humanBytes(_num(artifact['size_bytes'])), ]), kindLabel: 'Artifact', snippetSpans: const [], recency: recency, target: target, ); } final kind = _nonBlank(hit['kind']); return SearchRowView( title: _nonBlank(hit['target']) ?? 'Unrecognised result', secondary: kind == null ? 'read-only' : 'kind "$kind" · read-only', kindLabel: 'Unrecognised result', snippetSpans: const [], recency: recency, target: target, ); } /// Splits [snippet] so the first case-insensitive occurrence of [query] is a /// distinct matched span. A blank query or absent match leaves the whole /// snippet unmatched, so no text is ever dropped. List snippetSpans(String snippet, String query) { if (snippet.isEmpty) return const []; final needle = query.trim(); if (needle.isEmpty) { return [SearchSnippetSpan(snippet, matched: false)]; } final index = snippet.toLowerCase().indexOf(needle.toLowerCase()); if (index < 0) return [SearchSnippetSpan(snippet, matched: false)]; final end = index + needle.length; return [ if (index > 0) SearchSnippetSpan(snippet.substring(0, index), matched: false), SearchSnippetSpan(snippet.substring(index, end), matched: true), if (end < snippet.length) SearchSnippetSpan(snippet.substring(end), matched: false), ]; } String _stepSecondary(JsonMap step) { final diagnostic = '${step['diagnostic'] ?? ''}'; String? line; for (final candidate in diagnostic.split(RegExp(r'\r?\n'))) { if (candidate.trim().isNotEmpty) { line = candidate; break; } } final target = _nonBlank(step['target']); final joined = _join([ _nonBlank(step['outcome']), target == null ? null : '→ $target', line?.trim(), ]); return joined.isEmpty ? 'step ${_num(step['sequence'])}' : joined; } /// The server's own target token, never a target the client invents. A known /// payload is not enough: an absent token makes the row read-only. String? _serverTarget(JsonMap hit) { final target = _nonBlank(hit['target']); if (target == null) return null; final known = [ 'pipeline', 'version', 'run', 'step', 'field', 'artifact', ].any((key) => _map(hit[key]) != null); return known ? target : null; } String _stageCount(Object? count) { final n = _num(count); return '$n ${n == 1 ? 'stage' : 'stages'}'; } Map? _map(Object? value) { if (value is! Map) return null; return Map.from(value); } String? _nonBlank(Object? value) { final text = '${value ?? ''}'; return text.trim().isEmpty ? null : text; } int _num(Object? value) { if (value is int) return value; if (value is num) return value.toInt(); return int.tryParse('${value ?? ''}') ?? 0; } String _join(List parts) => parts.whereType().where((part) => part.isNotEmpty).join(' · ');