refactor: enhance conversation fetching and folder management

- Improved the logic for fetching conversations within folders by introducing a new condition to determine when to fetch folder conversations.
- Added detailed logging for successful and failed fetch attempts to aid in debugging and monitoring.
- Implemented a method to resolve folder conversations, ensuring that conversations are displayed in the correct order and that placeholders are used when necessary.
- Updated the ChatsDrawer to utilize the new conversation resolution logic, enhancing the user experience by ensuring all relevant conversations are displayed.
- This refactor streamlines conversation management and improves the overall efficiency of the chat interface.
This commit is contained in:
cogwheel0
2025-10-02 00:06:00 +05:30
parent 2d35bf4e07
commit 73ccb14b20
3 changed files with 173 additions and 93 deletions

View File

@@ -814,20 +814,37 @@ Future<List<Conversation>> conversations(Ref ref) async {
final missingIds = folder.conversationIds
.where((id) => !existingIds.contains(id))
.toList();
if (missingIds.isEmpty) continue;
final hasKnownConversations = conversationMap.values.any(
(conversation) => conversation.folderId == folder.id,
);
final shouldFetchFolder =
apiSvc != null &&
(missingIds.isNotEmpty ||
(!hasKnownConversations && folder.conversationIds.isEmpty));
List<Conversation> folderConvs = const [];
try {
if (apiSvc != null) {
if (shouldFetchFolder) {
try {
folderConvs = await apiSvc.getConversationsInFolder(folder.id);
DebugLogger.log(
'folder-sync',
scope: 'conversations/map',
data: {
'folderId': folder.id,
'fetched': folderConvs.length,
'missingIds': missingIds.length,
},
);
} catch (e) {
DebugLogger.error(
'folder-fetch-failed',
scope: 'conversations/map',
error: e,
data: {'folderId': folder.id},
);
}
} catch (e) {
DebugLogger.error(
'folder-fetch-failed',
scope: 'conversations/map',
error: e,
data: {'folderId': folder.id},
);
}
// Index fetched folder conversations for quick lookup
@@ -867,6 +884,21 @@ Future<List<Conversation>> conversations(Ref ref) async {
);
}
}
if (folderConvs.isNotEmpty && folder.conversationIds.isEmpty) {
for (final conv in folderConvs) {
final toAdd = conv.folderId == null
? conv.copyWith(folderId: folder.id)
: conv;
conversationMap[toAdd.id] = toAdd;
existingIds.add(toAdd.id);
DebugLogger.log(
'add-folder-fetch',
scope: 'conversations/map',
data: {'conversationId': toAdd.id, 'folderId': folder.id},
);
}
}
}
// Convert map back to list - this ensures no duplicates by ID