fix: the change assistant didn't take effect immediately #45 (#46)

* fix the change assistant didn't take effect immediately #45

* Update src/store/conversation.ts

Co-authored-by: boojack <stevenlgtm@gmail.com>

* change method name

* change method name

* fix the call method

---------

Co-authored-by: boojack <stevenlgtm@gmail.com>
This commit is contained in:
CorrectRoadH
2023-04-20 21:08:00 +08:00
committed by GitHub
parent c28c18b77a
commit 473abb857c
6 changed files with 17 additions and 18 deletions

View File

@ -132,7 +132,7 @@ const ConnectionSidebar = () => {
}; };
const handleConversationSelect = (conversation: Conversation) => { const handleConversationSelect = (conversation: Conversation) => {
conversationStore.setCurrentConversation(conversation); conversationStore.setCurrentConversationId(conversation.id);
if (layoutStore.isMobileView) { if (layoutStore.isMobileView) {
layoutStore.toggleSidebar(false); layoutStore.toggleSidebar(false);
} }
@ -148,8 +148,8 @@ const ConnectionSidebar = () => {
const handleDeleteConversation = (conversation: Conversation) => { const handleDeleteConversation = (conversation: Conversation) => {
conversationStore.clearConversation((item) => item.id !== conversation.id); conversationStore.clearConversation((item) => item.id !== conversation.id);
if (conversationStore.currentConversation?.id === conversation.id) { if (conversationStore.currentConversationId === conversation.id) {
conversationStore.setCurrentConversation(undefined); conversationStore.setCurrentConversationId(undefined);
} }
}; };
@ -244,11 +244,11 @@ const ConnectionSidebar = () => {
<div <div
key={conversation.id} key={conversation.id}
className={`w-full mt-2 first:mt-4 py-3 pl-4 pr-2 rounded-lg flex flex-row justify-start items-center cursor-pointer dark:text-gray-300 border border-transparent group hover:bg-white dark:hover:bg-zinc-800 ${ className={`w-full mt-2 first:mt-4 py-3 pl-4 pr-2 rounded-lg flex flex-row justify-start items-center cursor-pointer dark:text-gray-300 border border-transparent group hover:bg-white dark:hover:bg-zinc-800 ${
conversation.id === conversationStore.currentConversation?.id && "bg-white dark:bg-zinc-800 border-gray-200 font-medium" conversation.id === conversationStore.currentConversationId && "bg-white dark:bg-zinc-800 border-gray-200 font-medium"
}`} }`}
onClick={() => handleConversationSelect(conversation)} onClick={() => handleConversationSelect(conversation)}
> >
{conversation.id === conversationStore.currentConversation?.id ? ( {conversation.id === conversationStore.currentConversationId ? (
<Icon.IoChatbubble className="w-5 h-auto mr-1.5 shrink-0" /> <Icon.IoChatbubble className="w-5 h-auto mr-1.5 shrink-0" />
) : ( ) : (
<Icon.IoChatbubbleOutline className="w-5 h-auto mr-1.5 opacity-80 shrink-0" /> <Icon.IoChatbubbleOutline className="w-5 h-auto mr-1.5 opacity-80 shrink-0" />

View File

@ -13,8 +13,8 @@ const Header = (props: Props) => {
const layoutStore = useLayoutStore(); const layoutStore = useLayoutStore();
const conversationStore = useConversationStore(); const conversationStore = useConversationStore();
const isDarkMode = useDarkMode(); const isDarkMode = useDarkMode();
const currentConversation = conversationStore.currentConversation; const currentConversationId = conversationStore.currentConversationId;
const title = currentConversation?.title || "SQL Chat"; const title = conversationStore.getConversationById(currentConversationId)?.title || "SQL Chat";
useEffect(() => { useEffect(() => {
document.title = `${title}`; document.title = `${title}`;

View File

@ -34,7 +34,7 @@ const MessageTextarea = (props: Props) => {
}; };
const handleSend = async () => { const handleSend = async () => {
let conversation = conversationStore.currentConversation; let conversation = conversationStore.getConversationById(conversationStore.currentConversationId);
if (!conversation) { if (!conversation) {
const currentConnectionCtx = connectionStore.currentConnectionCtx; const currentConnectionCtx = connectionStore.currentConnectionCtx;
if (!currentConnectionCtx) { if (!currentConnectionCtx) {

View File

@ -32,7 +32,7 @@ const ConversationView = () => {
const [isStickyAtBottom, setIsStickyAtBottom] = useState<boolean>(true); const [isStickyAtBottom, setIsStickyAtBottom] = useState<boolean>(true);
const [showHeaderShadow, setShowHeaderShadow] = useState<boolean>(false); const [showHeaderShadow, setShowHeaderShadow] = useState<boolean>(false);
const conversationViewRef = useRef<HTMLDivElement>(null); const conversationViewRef = useRef<HTMLDivElement>(null);
const currentConversation = conversationStore.currentConversation; const currentConversation = conversationStore.getConversationById(conversationStore.currentConversationId);
const messageList = messageStore.messageList.filter((message) => message.conversationId === currentConversation?.id); const messageList = messageStore.messageList.filter((message) => message.conversationId === currentConversation?.id);
const lastMessage = last(messageList); const lastMessage = last(messageList);
@ -99,11 +99,10 @@ const ConversationView = () => {
conversation.connectionId === connectionStore.currentConnectionCtx?.connection.id && conversation.connectionId === connectionStore.currentConnectionCtx?.connection.id &&
conversation.databaseName === connectionStore.currentConnectionCtx?.database?.name conversation.databaseName === connectionStore.currentConnectionCtx?.database?.name
); );
conversationStore.setCurrentConversation(head(conversationList)); conversationStore.setCurrentConversationId(head(conversationList)?.id);
}, [currentConversation, connectionStore.currentConnectionCtx]); }, [currentConversation, connectionStore.currentConnectionCtx]);
const sendMessageToCurrentConversation = async () => { const sendMessageToCurrentConversation = async () => {
const currentConversation = conversationStore.getState().currentConversation;
if (!currentConversation) { if (!currentConversation) {
return; return;
} }

View File

@ -21,7 +21,7 @@ const EmptyView = (props: Props) => {
const isDarkMode = useDarkMode(); const isDarkMode = useDarkMode();
const handleExampleClick = async (content: string) => { const handleExampleClick = async (content: string) => {
let conversation = conversationStore.currentConversation; let conversation = conversationStore.getConversationById(conversationStore.currentConversationId);
if (!conversation) { if (!conversation) {
const currentConnectionCtx = connectionStore.currentConnectionCtx; const currentConnectionCtx = connectionStore.currentConnectionCtx;
if (!currentConnectionCtx) { if (!currentConnectionCtx) {

View File

@ -17,10 +17,10 @@ const getDefaultConversation = (): Conversation => {
interface ConversationState { interface ConversationState {
getState: () => ConversationState; getState: () => ConversationState;
conversationList: Conversation[]; conversationList: Conversation[];
currentConversation?: Conversation; currentConversationId?: Id;
createConversation: (connectionId?: Id, databaseName?: string) => Conversation; createConversation: (connectionId?: Id, databaseName?: string) => Conversation;
setCurrentConversation: (Conversation: Conversation | undefined) => void; setCurrentConversationId: (conversationId: Id | undefined) => void;
getConversationById: (conversationId: Id) => Conversation | undefined; getConversationById: (conversationId: Id | undefined) => Conversation | undefined;
updateConversation: (conversationId: Id, conversation: Partial<Conversation>) => void; updateConversation: (conversationId: Id, conversation: Partial<Conversation>) => void;
clearConversation: (filter: (conversation: Conversation) => boolean) => void; clearConversation: (filter: (conversation: Conversation) => boolean) => void;
} }
@ -45,8 +45,8 @@ export const useConversationStore = create<ConversationState>()(
})); }));
return conversation; return conversation;
}, },
setCurrentConversation: (conversation: Conversation | undefined) => set(() => ({ currentConversation: conversation })), setCurrentConversationId: (conversation: Id | undefined) => set(() => ({ currentConversationId: conversation })),
getConversationById: (conversationId: Id) => { getConversationById: (conversationId: Id | undefined) => {
return get().conversationList.find((item) => item.id === conversationId); return get().conversationList.find((item) => item.id === conversationId);
}, },
updateConversation: (conversationId: Id, conversation: Partial<Conversation>) => { updateConversation: (conversationId: Id, conversation: Partial<Conversation>) => {
@ -75,7 +75,7 @@ export const useConversationStore = create<ConversationState>()(
conversation.assistantId = "sql-chat-bot"; conversation.assistantId = "sql-chat-bot";
} }
} }
state.currentConversation = undefined; state.currentConversationId = undefined;
} }
return state; return state;