Skip to content

Commit

Permalink
Create a helper method to update chat from UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
bakar-io committed May 3, 2024
1 parent ff14091 commit acb58c1
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions frontend/src/hooks/useChatList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { Chat } from "../types";

export interface ChatListProps {
chats: Chat[] | null;
createChat: (
createChat: (name: string, assistant_id: string) => Promise<Chat>;
updateChat: (
name: string,
assistant_id: string,
thread_id?: string,
thread_id: string,
assistant_id: string | null,
) => Promise<Chat>;
deleteChat: (thread_id: string) => Promise<void>;
}
Expand Down Expand Up @@ -57,6 +58,23 @@ export function useChatList(): ChatListProps {
return saved;
}, []);

const updateChat = useCallback(
async (thread_id: string, name: string, assistant_id: string | null) => {
const response = await fetch(`/threads/${thread_id}`, {
method: "PUT",
body: JSON.stringify({ assistant_id, name }),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
});
const saved = await response.json();
setChats(saved);
return saved;
},
[],
);

const deleteChat = useCallback(
async (thread_id: string) => {
await fetch(`/threads/${thread_id}`, {
Expand All @@ -73,6 +91,7 @@ export function useChatList(): ChatListProps {
return {
chats,
createChat,
updateChat,
deleteChat,
};
}

0 comments on commit acb58c1

Please sign in to comment.