114 lines
3.5 KiB
Dart
114 lines
3.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../../../core/api_gateway.dart';
|
|
import '../../domain/chat_sessions.dart';
|
|
import 'chat_adaptive.dart';
|
|
import 'session_list_cupertino.dart';
|
|
import 'session_list_material.dart';
|
|
import 'session_row_style.dart';
|
|
|
|
typedef ChatSessionSection = ({String title, List<JsonMap> sessions});
|
|
|
|
/// Groups the visible sessions the way the server reports them.
|
|
class ChatSessionsBody extends StatelessWidget {
|
|
const ChatSessionsBody({
|
|
super.key,
|
|
required this.sessions,
|
|
required this.query,
|
|
required this.loading,
|
|
required this.error,
|
|
required this.onOpen,
|
|
required this.onRename,
|
|
required this.onClose,
|
|
required this.onReopen,
|
|
required this.onStar,
|
|
required this.onHide,
|
|
required this.onClearSearch,
|
|
required this.isUnseen,
|
|
required this.errorFor,
|
|
this.onDismissError,
|
|
});
|
|
|
|
final List<JsonMap> sessions;
|
|
final String query;
|
|
final bool loading;
|
|
final String? error;
|
|
final ValueChanged<JsonMap> onOpen;
|
|
final ValueChanged<JsonMap> onRename;
|
|
final ValueChanged<JsonMap> onClose;
|
|
final ValueChanged<JsonMap> onReopen;
|
|
final ValueChanged<JsonMap> onStar;
|
|
final ValueChanged<JsonMap> onHide;
|
|
final VoidCallback onClearSearch;
|
|
final bool Function(JsonMap) isUnseen;
|
|
final String? Function(String sessionId) errorFor;
|
|
final ValueChanged<String>? onDismissError;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final visible = filterChatSessions(sessions, query);
|
|
final sections = <ChatSessionSection>[
|
|
if (visible.any((s) => s['starred'] == true && s['closed_at'] == null))
|
|
(
|
|
title: 'Starred',
|
|
sessions: visible
|
|
.where((s) => s['starred'] == true && s['closed_at'] == null)
|
|
.toList(),
|
|
),
|
|
if (visible.any((s) => s['starred'] != true && s['closed_at'] == null))
|
|
(
|
|
title: 'Chats',
|
|
sessions: visible
|
|
.where((s) => s['starred'] != true && s['closed_at'] == null)
|
|
.toList(),
|
|
),
|
|
if (visible.any((s) => s['closed_at'] != null))
|
|
(
|
|
title: 'Closed',
|
|
sessions: visible.where((s) => s['closed_at'] != null).toList(),
|
|
),
|
|
];
|
|
final emptyMessage = sessions.isEmpty
|
|
? error == null
|
|
? 'No chat sessions on this server yet.'
|
|
: null
|
|
: 'No chats match "${query.trim()}".';
|
|
|
|
if (loading && sessions.isEmpty) return const ChatLoadingIndicator();
|
|
final clearSearch = query.trim().isEmpty ? null : onClearSearch;
|
|
final duplicateTitles = duplicateChatTitles(visible);
|
|
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
|
return CupertinoSessionList(
|
|
sections: sections,
|
|
emptyMessage: visible.isEmpty ? emptyMessage : null,
|
|
duplicateTitles: duplicateTitles,
|
|
onOpen: onOpen,
|
|
onRename: onRename,
|
|
onClose: onClose,
|
|
onReopen: onReopen,
|
|
onStar: onStar,
|
|
onHide: onHide,
|
|
onClearSearch: clearSearch,
|
|
isUnseen: isUnseen,
|
|
errorFor: errorFor,
|
|
onDismissError: onDismissError,
|
|
);
|
|
}
|
|
return MaterialSessionList(
|
|
sections: sections,
|
|
emptyMessage: visible.isEmpty ? emptyMessage : null,
|
|
duplicateTitles: duplicateTitles,
|
|
onOpen: onOpen,
|
|
onRename: onRename,
|
|
onClose: onClose,
|
|
onReopen: onReopen,
|
|
onStar: onStar,
|
|
onHide: onHide,
|
|
onClearSearch: clearSearch,
|
|
isUnseen: isUnseen,
|
|
errorFor: errorFor,
|
|
onDismissError: onDismissError,
|
|
);
|
|
}
|
|
}
|