import '../../../core/api_gateway.dart'; import 'chat_list_activity.dart'; import 'chat_sessions.dart'; /// Whether an open chat needs the operator: it waits on an approval or a /// question, has replies not read yet ([unread]), or an action on it failed /// ([failed]). bool chatNeedsAttention( JsonMap session, { required bool unread, required bool failed, }) => chatSessionAwaitingOperator(session) > 0 || unread || failed; /// The chat Alt+J opens: the next open chat after [currentId], in the order /// the list draws them (starred, then by directory; folded sections count), /// that [needsAttention], wrapping around. With no current chat (or one not /// among the open chats) the first such chat. Null when no other chat needs /// the operator. JsonMap? chatNextNeedingAttention( List sessions, String? currentId, bool Function(JsonMap session) needsAttention, ) { final open = chatSessionsInListOrder( chatSessionSections( sessions .where((row) => row['closed_at'] == null && row['closed'] != true) .toList(), ), isOpen: (_) => true, ); if (open.isEmpty) return null; final at = open.indexWhere((row) => '${row['id'] ?? ''}' == currentId); for (var step = 1; step <= open.length; step++) { final row = open[(at + step) % open.length]; if ('${row['id'] ?? ''}' == currentId) continue; if (needsAttention(row)) return row; } return null; }