fix: improve chat message deduplication, add conversation refresh logic, and update UI components for consistency

This commit is contained in:
pradeepkumar
2026-04-18 11:34:48 +05:30
parent 164d590d31
commit afa1f3489a
6 changed files with 47 additions and 8 deletions

View File

@@ -77,6 +77,9 @@ class SupportChatNotifier extends StateNotifier<SupportChatState> {
}
}
// Track message IDs we've sent via REST to avoid socket echo duplicates
final Set<String> _sentMessageIds = {};
Future<void> sendMessage(String content) async {
final chat = state.chat;
if (chat == null || content.trim().isEmpty) return;
@@ -85,6 +88,7 @@ class SupportChatNotifier extends StateNotifier<SupportChatState> {
state = state.copyWith(isSending: true);
try {
final message = await _repo.sendMessage(chat.id, content.trim());
_sentMessageIds.add(message.id);
// Dedup — socket broadcast may have already added this message
if (!state.messages.any((m) => m.id == message.id)) {
state = state.copyWith(
@@ -121,7 +125,9 @@ class SupportChatNotifier extends StateNotifier<SupportChatState> {
void addIncomingMessage(SupportMessage message) {
if (state.chat == null || message.chatId != state.chat!.id) return;
// Skip if already in list OR if we sent this via REST (socket echo)
if (state.messages.any((m) => m.id == message.id)) return;
if (_sentMessageIds.contains(message.id)) return;
state = state.copyWith(messages: [...state.messages, message]);
_repo.markAsRead(state.chat!.id);

View File

@@ -333,7 +333,17 @@ class _SupportChatScreenState extends ConsumerState<SupportChatScreen> {
}
Widget _buildMessageArea(SupportChatState state, String currentUserId) {
final groups = _groupByDate(state.messages);
// Filter out any messages with empty content (phantom/blank cards)
// and deduplicate by ID (socket echo + REST response race condition)
final seen = <String>{};
final cleanMessages = <SupportMessage>[];
for (final msg in state.messages) {
if (msg.content.trim().isEmpty) continue;
if (seen.contains(msg.id)) continue;
seen.add(msg.id);
cleanMessages.add(msg);
}
final groups = _groupByDate(cleanMessages);
return Container(
color: const Color(0xFFF9F9F9),