Sort conversations by last activity
Bump Conversation.updated_at on every message (not just the first) so actively-used conversations rise to the top of the list endpoint's existing -updated_at sort. Refresh the sidebar after every send so the just-used conversation reorders live. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import tortoise.exceptions
|
||||
|
||||
import blueprints.users.models
|
||||
@@ -19,10 +21,17 @@ async def add_message_to_conversation(
|
||||
) -> ConversationMessage:
|
||||
print(conversation, message, speaker)
|
||||
|
||||
# Bump updated_at on every message so conversations sort by last activity.
|
||||
# `update_fields` bypasses the auto_now recompute, so set it explicitly.
|
||||
update_fields = ["updated_at"]
|
||||
conversation.updated_at = datetime.now(timezone.utc)
|
||||
|
||||
# Name the conversation after the first user message
|
||||
if speaker == "user" and not await conversation.messages.all().exists():
|
||||
conversation.name = message[:100]
|
||||
await conversation.save()
|
||||
update_fields.append("name")
|
||||
|
||||
await conversation.save(update_fields=update_fields)
|
||||
|
||||
message = await ConversationMessage.create(
|
||||
text=message,
|
||||
|
||||
@@ -131,7 +131,6 @@ export function useChat({
|
||||
if ((!query.trim() && !pendingImage) || isLoading) return;
|
||||
|
||||
let activeId = conversationId;
|
||||
const createdNew = !activeId;
|
||||
if (!activeId) {
|
||||
const created = await createConversation();
|
||||
// Prevent the route-change loader from clearing what we're about to add.
|
||||
@@ -259,9 +258,10 @@ export function useChat({
|
||||
if (isMountedRef.current) {
|
||||
setIsLoading(false);
|
||||
setStreaming(false);
|
||||
if (createdNew) {
|
||||
refreshConversations();
|
||||
}
|
||||
// Refresh after every send so the just-used conversation floats to
|
||||
// the top of the sidebar (sorted by last activity) and picks up the
|
||||
// auto-generated title on the first message.
|
||||
refreshConversations();
|
||||
}
|
||||
streamingActiveRef.current = false;
|
||||
abortControllerRef.current = null;
|
||||
|
||||
Reference in New Issue
Block a user