Files
pipelines/app/lib/features/search/domain/search_order.dart
T
alan 6ca415c134 Search: order results newest first in every group
Chat hits sort by the chat's last use whatever field matched, and run,
step, field, artifact and pipeline hits sort by their own date, before
the result cap applies so the cap drops the oldest. Each kind's row scan
also walks newest first. Score still reports the match tier. The app
sorts both groups newest first as well.
2026-11-03 13:35:27 +00:00

29 lines
1.1 KiB
Dart

import '../../../core/api_gateway.dart';
/// Search hits newest first, the order the server sends them in. The app
/// sorts again so each displayed group stays newest first even from an older
/// server that ranked by match tier. Equal or missing dates keep the server's
/// order; a hit without a date sorts last.
List<JsonMap> searchHitsNewestFirst(List<JsonMap> hits) {
final indexed = [
for (var i = 0; i < hits.length; i++) (i, _hitMillis(hits[i]), hits[i]),
];
indexed.sort((a, b) {
final byDate = b.$2.compareTo(a.$2);
return byDate != 0 ? byDate : a.$1.compareTo(b.$1);
});
return [for (final entry in indexed) entry.$3];
}
/// A chat hit's last-used time (`chat.updated_at`, epoch milliseconds), else
/// any hit's `recency` timestamp; -1 when neither is present.
int _hitMillis(JsonMap hit) {
final chat = hit['chat'];
if (chat is Map) {
final updated = chat['updated_at'];
if (updated is num && updated > 0) return updated.toInt();
}
final recency = DateTime.tryParse('${hit['recency'] ?? ''}');
return recency?.millisecondsSinceEpoch ?? -1;
}