Skip to content

Commit

Permalink
proper chat message sidebar updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pablodanswer committed Sep 8, 2024
1 parent a200430 commit 98c6bf0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
5 changes: 4 additions & 1 deletion web/src/app/chat/ChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ export function ChatPage({
shouldShowWelcomeModal,
shouldDisplaySourcesIncompleteModal,
defaultAssistantId,
refreshChatSessions,
} = useChatContext();

const [showApiKeyModal, setShowApiKeyModal] = useState(true);

const { user, refreshUser, isLoadingUser } = useUser();
Expand Down Expand Up @@ -405,7 +407,7 @@ export function ChatPage({
// force re-name if the chat session doesn't have one
if (!chatSession.description) {
await nameChatSession(existingChatSessionId, seededMessage);
router.refresh(); // need to refresh to update name on sidebar
refreshChatSessions();
}
}
}
Expand Down Expand Up @@ -1339,6 +1341,7 @@ export function ChatPage({
if (!searchParamBasedChatSessionName) {
await new Promise((resolve) => setTimeout(resolve, 200));
await nameChatSession(currChatSessionId, currMessage);
refreshChatSessions();
}

// NOTE: don't switch pages if the user has navigated away from the chat
Expand Down
21 changes: 14 additions & 7 deletions web/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,26 @@ export default async function RootLayout({
) : (
<p>
It seems that you have not configured Danswer properly. Please
feel free to send us a quick email to{" "}
<a className="text-link" href="mailto:[email protected]">
[email protected]
check{" "}
<a
className="text-link"
href="https://docs.danswer.dev/introduction?utm_source=app&utm_medium=error_page&utm_campaign=config_error"
target="_blank"
rel="noopener noreferrer"
>
our docs
</a>{" "}
and we will help you out. Or checkout our{" "}
to see how to configure Danswer or reach out to our community
on{" "}
<a
className="text-link"
href="https://docs.danswer.ai/getting-started/installation/docker-compose"
href="https://danswer.ai?utm_source=app&utm_medium=error_page&utm_campaign=config_error"
target="_blank"
rel="noopener noreferrer"
>
troubleshooting guide
Slack
</a>{" "}
to help you get started.
for support and guidance.
</p>
)}
</Card>
Expand Down
28 changes: 25 additions & 3 deletions web/src/components/context/ChatContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import React, { createContext, useContext } from "react";
import React, { createContext, useContext, useState } from "react";
import { DocumentSet, Tag, User, ValidSources } from "@/lib/types";
import { ChatSession } from "@/app/chat/interfaces";
import { Persona } from "@/app/admin/assistants/interfaces";
Expand All @@ -21,15 +21,37 @@ interface ChatContextProps {
shouldShowWelcomeModal?: boolean;
shouldDisplaySourcesIncompleteModal?: boolean;
defaultAssistantId?: number;
refreshChatSessions: () => Promise<void>;
}

const ChatContext = createContext<ChatContextProps | undefined>(undefined);

// We use Omit to exclude 'refreshChatSessions' from the value prop type
// because we're defining it within the component
export const ChatProvider: React.FC<{
value: ChatContextProps;
value: Omit<ChatContextProps, "refreshChatSessions">;
children: React.ReactNode;
}> = ({ value, children }) => {
return <ChatContext.Provider value={value}>{children}</ChatContext.Provider>;
const [chatSessions, setChatSessions] = useState(value?.chatSessions || []);

const refreshChatSessions = async () => {
try {
const response = await fetch("/api/chat/get-user-chat-sessions");
if (!response.ok) throw new Error("Failed to fetch chat sessions");
const { sessions } = await response.json();
setChatSessions(sessions);
} catch (error) {
console.error("Error refreshing chat sessions:", error);
}
};

return (
<ChatContext.Provider
value={{ ...value, chatSessions, refreshChatSessions }}
>
{children}
</ChatContext.Provider>
);
};

export const useChatContext = (): ChatContextProps => {
Expand Down

0 comments on commit 98c6bf0

Please sign in to comment.