Skip to content

Commit

Permalink
create chat + delete chat: mongodb -> localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
dissorial committed Jun 3, 2023
1 parent 877d644 commit a7cb32b
Show file tree
Hide file tree
Showing 10 changed files with 16,495 additions and 4,020 deletions.
8 changes: 2 additions & 6 deletions components/sidebar/SidebarList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SourceDocumentsToggle from './SourceDocumentsToggle';
import ModelTemperature from './ModelTemperature';

interface SidebarListProps {
createChat: () => string;
selectedNamespace: string;
returnSourceDocuments: boolean;
setReturnSourceDocuments: React.Dispatch<React.SetStateAction<boolean>>;
Expand All @@ -17,7 +18,6 @@ interface SidebarListProps {
chatList: string[];
selectedChatId: string;
setChatId: (value: string) => void;
createChat: () => Promise<string>;
setSelectedChatId: React.Dispatch<React.SetStateAction<string>>;
nameSpaceHasChats: boolean;
chatNames: Record<string, string>;
Expand Down Expand Up @@ -66,7 +66,7 @@ const SidebarList: React.FC<SidebarListProps> = ({
buttonType="primary"
buttonText="New chat"
onClick={async () => {
const newChatId = await createChat();
const newChatId = createChat();
setChatId(newChatId);
setSelectedChatId(newChatId);
}}
Expand All @@ -93,18 +93,14 @@ const SidebarList: React.FC<SidebarListProps> = ({
: 'Select a namespace to display chats'}
</div>
)}

{/* desktop */}
</li>
{/* desktop */}
<ListOfNamespaces
namespaces={namespaces}
selectedNamespace={selectedNamespace}
setSelectedNamespace={setSelectedNamespace}
/>
</ul>

{/* desktop */}
<Button
buttonType="secondary"
buttonText="Settings"
Expand Down
61 changes: 28 additions & 33 deletions hooks/useChats.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useState, useEffect } from 'react';
import { useState, useEffect, useMemo } from 'react';
import { v4 as uuidv4 } from 'uuid';
import axios from 'axios';

function useLocalStorage<T>(key: string, initialValue: T) {
const [storedValue, setStoredValue] = useState<T>(() => {
Expand All @@ -20,64 +19,59 @@ function useLocalStorage<T>(key: string, initialValue: T) {
}

export function useChats(namespace: string, userEmail: string | undefined) {
const [chatList, setChatList] = useLocalStorage<string[]>(
`chatList-${namespace}`,
[],
const [allChats, setAllChats] = useLocalStorage<
{ namespace: string; chatId: string }[]
>('allChats', []);
const chatList = useMemo(
() => allChats.filter((chat) => chat.namespace === namespace),
[allChats, namespace],
);

const [chatNames, setChatNames] = useLocalStorage<{ [key: string]: string }>(
`chatNames-${namespace}`,
{},
);

console.log(chatList, 'chatList');

const [selectedChatId, setSelectedChatId] = useState<string>('');

function updateChatName(chatId: string, newChatName: string) {
const updatedChatNames = { ...chatNames, [chatId]: newChatName };
setChatNames(updatedChatNames);
}

async function createChat() {
function createChat() {
const newChatId = uuidv4();
const updatedChatList = [...chatList, newChatId];
setChatList(updatedChatList);

try {
await axios.post('/api/create-chat', {
chatId: newChatId,
namespace,
userEmail,
});
} catch (error) {
console.error('Failed to create new chat:', error);
}
const updatedChatList = [...chatList, { namespace, chatId: newChatId }];
setAllChats(updatedChatList);

return newChatId;
}

async function deleteChat(chatIdToDelete: string) {
const updatedChatList = chatList.filter(
(chatId) => chatId !== chatIdToDelete,
(chat) => chat.chatId !== chatIdToDelete,
);
setChatList(updatedChatList);

try {
await axios.delete(`/api/delete-chat`, {
data: {
chatId: chatIdToDelete,
namespace,
userEmail,
},
});
} catch (error) {
console.error('Failed to delete chat:', error);
}
setAllChats(updatedChatList);

if (chatIdToDelete === selectedChatId) {
const newSelectedChatId =
updatedChatList.length > 0 ? updatedChatList[0] : '';
updatedChatList.length > 0 ? updatedChatList[0].chatId : '';
setSelectedChatId(newSelectedChatId);
}
}

const filteredChatList = chatList.filter(
(chat) => chat.namespace === namespace,
);

console.log(filteredChatList, 'filteredChatList');

useEffect(() => {
console.log('selectedChatId changed:', selectedChatId);
}, [selectedChatId]);

return {
chatList,
selectedChatId,
Expand All @@ -87,5 +81,6 @@ export function useChats(namespace: string, userEmail: string | undefined) {
chatNames,
updateChatName,
userEmail,
filteredChatList,
};
}
5 changes: 2 additions & 3 deletions hooks/useNamespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ export default function useNamespaces(userEmail: string | undefined) {
if (userEmail) {
const fetchNamespaces = async () => {
try {
const response = await fetch(
`/api/getNamespaces?userEmail=${userEmail}`,
);
const response = await fetch(`/api/getNamespaces`);
const data = await response.json();

console.log(data);
if (response.ok) {
setNamespaces(data);
} else {
Expand Down
Loading

0 comments on commit a7cb32b

Please sign in to comment.