Skip to content

Commit

Permalink
Merge pull request #45 from jakobhoeg/develop
Browse files Browse the repository at this point in the history
fix: clear messages without directly mutating the state
  • Loading branch information
jakobhoeg authored Apr 29, 2024
2 parents 0bbf98f + 1a55956 commit 15d317e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export default function Home() {
navCollapsedSize={10}
defaultLayout={[30, 160]}
formRef={formRef}
setMessages={setMessages}
/>
<DialogContent className="flex flex-col space-y-4">
<DialogHeader className="space-y-2">
Expand Down
5 changes: 4 additions & 1 deletion src/components/chat/chat-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "@/components/ui/resizable";
import { cn } from "@/lib/utils";
import { Sidebar } from "../sidebar";
import { useChat } from "ai/react";
import { Message, useChat } from "ai/react";
import Chat, { ChatProps } from "./chat";
import ChatList from "./chat-list";
import { HamburgerMenuIcon } from "@radix-ui/react-icons";
Expand All @@ -18,6 +18,7 @@ interface ChatLayoutProps {
defaultCollapsed?: boolean;
navCollapsedSize: number;
chatId: string;
setMessages: (messages: Message[]) => void;
}

type MergedProps = ChatLayoutProps & ChatProps;
Expand All @@ -37,6 +38,7 @@ export function ChatLayout({
setSelectedModel,
loadingSubmit,
formRef,
setMessages,
}: MergedProps) {
const [isCollapsed, setIsCollapsed] = React.useState(defaultCollapsed);
const [isMobile, setIsMobile] = useState(false);
Expand Down Expand Up @@ -97,6 +99,7 @@ export function ChatLayout({
messages={messages}
isMobile={isMobile}
chatId={chatId}
setMessages={setMessages}
/>
</ResizablePanel>
<ResizableHandle className={cn("hidden md:flex")} withHandle />
Expand Down
4 changes: 3 additions & 1 deletion src/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ interface SidebarProps {
onClick?: () => void;
isMobile: boolean;
chatId: string;
setMessages: (messages: Message[]) => void;
}

export function Sidebar({
messages,
isCollapsed,
isMobile,
chatId,
setMessages,
}: SidebarProps) {
const [localChats, setLocalChats] = useState<
{ chatId: string; messages: Message[] }[]
Expand Down Expand Up @@ -112,7 +114,7 @@ export function Sidebar({
onClick={() => {
router.push("/");
// Clear messages
messages.splice(0, messages.length);
setMessages([]);
}}
variant="ghost"
className="flex justify-between w-full h-14 text-sm xl:text-lg font-normal items-center "
Expand Down

0 comments on commit 15d317e

Please sign in to comment.