Fetch history before nudges to not block

This commit is contained in:
Lucas Oliveira 2025-12-05 16:11:48 -03:00
parent 1f528c2935
commit e413ff98f9

View file

@ -10,6 +10,7 @@ import { useTask } from "@/contexts/task-context";
import { useChatStreaming } from "@/hooks/useChatStreaming"; import { useChatStreaming } from "@/hooks/useChatStreaming";
import { FILE_CONFIRMATION, FILES_REGEX } from "@/lib/constants"; import { FILE_CONFIRMATION, FILES_REGEX } from "@/lib/constants";
import { useLoadingStore } from "@/stores/loadingStore"; import { useLoadingStore } from "@/stores/loadingStore";
import { useGetConversationsQuery } from "../api/queries/useGetConversationsQuery";
import { useGetNudgesQuery } from "../api/queries/useGetNudgesQuery"; import { useGetNudgesQuery } from "../api/queries/useGetNudgesQuery";
import { AssistantMessage } from "./_components/assistant-message"; import { AssistantMessage } from "./_components/assistant-message";
import { ChatInput, type ChatInputHandle } from "./_components/chat-input"; import { ChatInput, type ChatInputHandle } from "./_components/chat-input";
@ -36,6 +37,7 @@ function ChatPage() {
forkFromResponse, forkFromResponse,
refreshConversations, refreshConversations,
refreshConversationsSilent, refreshConversationsSilent,
refreshTrigger,
previousResponseIds, previousResponseIds,
setPreviousResponseIds, setPreviousResponseIds,
placeholderConversation, placeholderConversation,
@ -71,6 +73,14 @@ function ChatPage() {
const lastLoadedConversationRef = useRef<string | null>(null); const lastLoadedConversationRef = useRef<string | null>(null);
const { addTask } = useTask(); const { addTask } = useTask();
console.log(endpoint, refreshTrigger);
// Check if chat history is loading
const { isLoading: isConversationsLoading } = useGetConversationsQuery(
endpoint,
refreshTrigger,
);
// Use conversation-specific filter instead of global filter // Use conversation-specific filter instead of global filter
const selectedFilter = conversationFilter; const selectedFilter = conversationFilter;
@ -116,7 +126,12 @@ function ChatPage() {
if (conversationFilter && typeof window !== "undefined") { if (conversationFilter && typeof window !== "undefined") {
const newKey = `conversation_filter_${responseId}`; const newKey = `conversation_filter_${responseId}`;
localStorage.setItem(newKey, conversationFilter.id); localStorage.setItem(newKey, conversationFilter.id);
console.log("[CHAT] Saved filter association:", newKey, "=", conversationFilter.id); console.log(
"[CHAT] Saved filter association:",
newKey,
"=",
conversationFilter.id,
);
} }
} }
}, },
@ -507,6 +522,9 @@ function ChatPage() {
setTimeout(() => { setTimeout(() => {
chatInputRef.current?.focusInput(); chatInputRef.current?.focusInput();
}, 100); }, 100);
} else if (!conversationData) {
// No conversation selected (new conversation)
lastLoadedConversationRef.current = null;
} }
}, [ }, [
conversationData, conversationData,
@ -677,7 +695,7 @@ function ChatPage() {
scoreThreshold: parsedFilterData?.scoreThreshold ?? 0, scoreThreshold: parsedFilterData?.scoreThreshold ?? 0,
}, },
{ {
enabled: isOnboardingComplete, // Only fetch nudges after onboarding is complete enabled: isOnboardingComplete && !isConversationsLoading, // Only fetch nudges after onboarding is complete AND chat history is not loading
}, },
); );
@ -841,7 +859,12 @@ function ChatPage() {
// Store the response ID if present for this endpoint // Store the response ID if present for this endpoint
if (result.response_id) { if (result.response_id) {
console.log("[DEBUG] Received response_id:", result.response_id, "currentConversationId:", currentConversationId); console.log(
"[DEBUG] Received response_id:",
result.response_id,
"currentConversationId:",
currentConversationId,
);
setPreviousResponseIds((prev) => ({ setPreviousResponseIds((prev) => ({
...prev, ...prev,
@ -850,11 +873,16 @@ function ChatPage() {
// If this is a new conversation (no currentConversationId), set it now // If this is a new conversation (no currentConversationId), set it now
if (!currentConversationId) { if (!currentConversationId) {
console.log("[DEBUG] Setting currentConversationId to:", result.response_id); console.log(
"[DEBUG] Setting currentConversationId to:",
result.response_id,
);
setCurrentConversationId(result.response_id); setCurrentConversationId(result.response_id);
refreshConversations(true); refreshConversations(true);
} else { } else {
console.log("[DEBUG] Existing conversation, doing silent refresh"); console.log(
"[DEBUG] Existing conversation, doing silent refresh",
);
// For existing conversations, do a silent refresh to keep backend in sync // For existing conversations, do a silent refresh to keep backend in sync
refreshConversationsSilent(); refreshConversationsSilent();
} }
@ -863,7 +891,12 @@ function ChatPage() {
if (conversationFilter && typeof window !== "undefined") { if (conversationFilter && typeof window !== "undefined") {
const newKey = `conversation_filter_${result.response_id}`; const newKey = `conversation_filter_${result.response_id}`;
localStorage.setItem(newKey, conversationFilter.id); localStorage.setItem(newKey, conversationFilter.id);
console.log("[DEBUG] Saved filter association:", newKey, "=", conversationFilter.id); console.log(
"[DEBUG] Saved filter association:",
newKey,
"=",
conversationFilter.id,
);
} }
} }
} else { } else {