Skip to content

Commit

Permalink
setselectedchatid fix on create and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
dissorial committed Jun 3, 2023
1 parent 27ff9ff commit bee3e9e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 30 deletions.
2 changes: 1 addition & 1 deletion components/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const Header: React.FC<HeaderProps> = ({ setSidebarOpen }) => {
</span>
</div>

<div className="min-w-[100px]">
<div className="w-fit">
<Button
buttonType="secondary"
buttonText="Settings"
Expand Down
5 changes: 1 addition & 4 deletions components/sidebar/ListOfChats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@ function classNames(...classes: string[]) {
const ListOfChats = ({
filteredChatList,
selectedChatId,
setChatId,
setSelectedChatId,
chatNames,
updateChatName,
deleteChat,
}: {
filteredChatList: string[];
selectedChatId: string;
setChatId: (chatId: string) => void;
setSelectedChatId: (chatId: string) => void;
chatNames: { [chatId: string]: string };
updateChatName: (chatId: string, newChatName: string) => void;
deleteChat: (chatId: string) => void;
}) => {
const handleChatClick = (chatId: string) => {
setChatId(chatId);
setSelectedChatId(chatId);
};

Expand All @@ -41,7 +38,7 @@ const ListOfChats = ({
};

return (
<ul role="list" className="-mx-2 mt-2 px-2 pb-6">
<ul role="list" className="-mx-2 mt-2 px-2 pb-6 space-y-1">
{[...filteredChatList].reverse().map((chatId, index) => (
<li
key={chatId}
Expand Down
6 changes: 1 addition & 5 deletions components/sidebar/SidebarList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ interface SidebarListProps {
setModelTemperature: React.Dispatch<React.SetStateAction<number>>;
filteredChatList: string[];
selectedChatId: string;
setChatId: (value: string) => void;
setSelectedChatId: React.Dispatch<React.SetStateAction<string>>;
nameSpaceHasChats: boolean;
chatNames: Record<string, string>;
Expand All @@ -33,7 +32,6 @@ const SidebarList: React.FC<SidebarListProps> = ({
setModelTemperature,
filteredChatList,
selectedChatId,
setChatId,
createChat,
setSelectedChatId,
nameSpaceHasChats,
Expand Down Expand Up @@ -63,7 +61,6 @@ const SidebarList: React.FC<SidebarListProps> = ({
buttonText="New chat"
onClick={async () => {
const newChatId = createChat();
setChatId(newChatId);
setSelectedChatId(newChatId);
}}
icon={PlusCircleIcon}
Expand All @@ -82,15 +79,14 @@ const SidebarList: React.FC<SidebarListProps> = ({
/>
</div>

<div className="px-4 text-xs sm:text-sm font-semibold leading-6 text-blue-400">
<div className="px-4 text-xs sm:text-sm font-semibold leading-6 text-blue-400">
Your chats
</div>
<div className="px-4 flex-grow overflow-y-auto">
{selectedNamespace && nameSpaceHasChats ? (
<ListOfChats
filteredChatList={filteredChatList}
selectedChatId={selectedChatId}
setChatId={setChatId}
setSelectedChatId={setSelectedChatId}
chatNames={chatNames}
updateChatName={updateChatName}
Expand Down
11 changes: 9 additions & 2 deletions hooks/useChats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ export function useChats(namespace: string) {
setAllChats(updatedAllChats);

if (chatIdToDelete === selectedChatId) {
const newSelectedChatId =
updatedAllChats.length > 0 ? updatedAllChats[0].chatId : '';
const deletedChatIndex = allChats.findIndex(
(chat) => chat.chatId === chatIdToDelete,
);
let newSelectedChatId = '';
if (updatedAllChats[deletedChatIndex]) {
newSelectedChatId = updatedAllChats[deletedChatIndex].chatId;
} else if (deletedChatIndex > 0) {
newSelectedChatId = updatedAllChats[deletedChatIndex - 1].chatId;
}
setSelectedChatId(newSelectedChatId);
}
}
Expand Down
1 change: 1 addition & 0 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default function Document() {
return (
<Html lang="en" className="h-full bg-gray-900">
<Head />

<body className="h-full m-0 p-0">
<Main />
<NextScript />
Expand Down
31 changes: 13 additions & 18 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,11 @@ export default function Home() {
const textAreaRef = useRef<HTMLTextAreaElement>(null);

const fetchChatHistory = useCallback(() => {
setLoading(true);

try {
const conversations = getConversation(selectedChatId);

console.log(
'Chat ID:',
selectedChatId,
'History:',
conversations.history,
);

if (!conversations || !conversations.messages) {
console.error('Failed to fetch chat history: No conversations found.');
return;
Expand Down Expand Up @@ -121,14 +116,22 @@ export default function Home() {
}));
} catch (error) {
console.error('Failed to fetch chat history:', error);
} finally {
setLoading(false);
}
}, [selectedChatId, getConversation]);

useEffect(() => {
if (selectedNamespace && chatList.length > 0) {
if (selectedNamespace && chatList.length > 0 && !selectedChatId) {
setSelectedChatId(chatList[0].chatId);
}
}, [selectedNamespace, chatList, setSelectedChatId]);
}, [selectedNamespace, chatList, selectedChatId, setSelectedChatId]);

useEffect(() => {
if (chatList.length > 0) {
setSelectedChatId(chatList[chatList.length - 1].chatId);
}
}, [selectedNamespace, setSelectedChatId, chatList]);

useEffect(() => {
if (selectedChatId) {
Expand Down Expand Up @@ -165,12 +168,6 @@ export default function Home() {
setQuery('');

const conversation = getConversation(selectedChatId);
console.log(
'Sending to API - Chat ID:',
selectedChatId,
'History:',
conversation.history,
);

const response = await fetch('/api/chat', {
method: 'POST',
Expand Down Expand Up @@ -235,7 +232,7 @@ export default function Home() {

return (
<>
{status === 'loading' ? (
{loading ? (
<LoadingState />
) : (
<div className="h-full">
Expand Down Expand Up @@ -298,7 +295,6 @@ export default function Home() {
(chat) => chat.chatId,
)}
selectedChatId={selectedChatId}
setChatId={setSelectedChatId}
setSelectedChatId={setSelectedChatId}
chatNames={chatNames}
updateChatName={updateChatName}
Expand Down Expand Up @@ -326,7 +322,6 @@ export default function Home() {
namespaces={namespaces}
filteredChatList={filteredChatList.map((chat) => chat.chatId)}
selectedChatId={selectedChatId}
setChatId={setSelectedChatId}
setSelectedChatId={setSelectedChatId}
chatNames={chatNames}
updateChatName={updateChatName}
Expand Down

0 comments on commit bee3e9e

Please sign in to comment.