import '../../../../core/api_gateway.dart'; import '../../../../ui/text/relative_time.dart'; import '../../domain/chat_sessions.dart'; /// Titles shared by more than one visible session, so row action labels can /// add the session id the way the web client does. Set duplicateChatTitles(Iterable sessions) { final seen = {}; final duplicates = {}; for (final session in sessions) { final title = chatSessionTitle(session); if (!seen.add(title)) duplicates.add(title); } return duplicates; } String sessionActionsLabel(JsonMap session, Set duplicateTitles) { final title = chatSessionTitle(session); return duplicateTitles.contains(title) ? 'Chat actions for $title (${session['id'] ?? ''})' : 'Chat actions for $title'; } /// The shared list-row recency, or nothing for an unusable `updated_at`. String relativeChatSessionTime(Object? updatedAt, {DateTime? now}) { final millis = chatSessionMillis(updatedAt); if (millis <= 0) return ''; return formatRelativeTime( DateTime.fromMillisecondsSinceEpoch(millis), now: now, ) ?? ''; } /// The row's one muted line: the recorded model and the folder (home as `~`). /// The harness is the row's avatar, so it is not repeated as a word. String chatSessionSubtitle(JsonMap session) => [ '${session['model'] ?? ''}'.trim(), homeRelativePath('${session['directory'] ?? ''}'.trim()), ].where((part) => part.isNotEmpty).join(' ยท '); final _homePrefix = RegExp(r'^(/Users/[^/]+|/home/[^/]+|/root)(?=/|$)'); /// `/Users/alan/repos/x` (or `/home/alan/...`) as `~/repos/x`. String homeRelativePath(String path) => path.replaceFirst(_homePrefix, '~'); /// The row's attention/state word: `Closed` takes precedence, otherwise only /// actually unread content is `New`. Loading activity is rendered separately. String? chatSessionRowStatus(JsonMap session, {required bool unseen}) { if (session['closed_at'] != null || session['closed'] == true) { return 'Closed'; } return unseen ? 'New' : null; }