-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathchat-layout.tsx
102 lines (93 loc) · 2.81 KB
/
chat-layout.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"use client";
import React, { useEffect, useState } from "react";
import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable";
import { cn } from "@/lib/utils";
import { Sidebar } from "../sidebar";
import { Message, useChat } from "ai/react";
import Chat, { ChatProps } from "./chat";
import ChatList from "./chat-list";
import { HamburgerMenuIcon } from "@radix-ui/react-icons";
import { useTranslation } from "react-i18next";
interface ChatLayoutProps {
defaultLayout: number[] | undefined;
defaultCollapsed?: boolean;
navCollapsedSize: number;
}
type MergedProps = ChatLayoutProps & ChatProps;
export function ChatLayout({
defaultLayout = [30, 160],
defaultCollapsed = false,
navCollapsedSize,
initialMessages,
id,
}: MergedProps) {
const [isCollapsed, setIsCollapsed] = React.useState(defaultCollapsed);
const [isMobile, setIsMobile] = useState(false);
useEffect(() => {
const checkScreenWidth = () => {
setIsMobile(window.innerWidth <= 1023);
};
// Initial check
checkScreenWidth();
// Event listener for screen width changes
window.addEventListener("resize", checkScreenWidth);
// Cleanup the event listener on component unmount
return () => {
window.removeEventListener("resize", checkScreenWidth);
};
}, []);
return (
<ResizablePanelGroup
direction="horizontal"
onLayout={(sizes: number[]) => {
document.cookie = `react-resizable-panels:layout=${JSON.stringify(
sizes
)}`;
}}
className="h-screen items-stretch"
>
<ResizablePanel
defaultSize={defaultLayout[0]}
collapsedSize={navCollapsedSize}
collapsible={true}
minSize={isMobile ? 0 : 12}
maxSize={isMobile ? 0 : 16}
onCollapse={() => {
setIsCollapsed(true);
document.cookie = `react-resizable-panels:collapsed=${JSON.stringify(
true
)}`;
}}
onExpand={() => {
setIsCollapsed(false);
document.cookie = `react-resizable-panels:collapsed=${JSON.stringify(
false
)}`;
}}
className={cn(
isCollapsed
? "min-w-[50px] md:min-w-[70px] transition-all duration-300 ease-in-out"
: "hidden md:block"
)}
>
<Sidebar
isCollapsed={isCollapsed || isMobile}
messages={initialMessages}
isMobile={isMobile}
chatId={id}
/>
</ResizablePanel>
<ResizableHandle className={cn("hidden md:flex")} withHandle />
<ResizablePanel
className="h-full w-full flex justify-center"
defaultSize={defaultLayout[1]}
>
<Chat id={id} initialMessages={initialMessages} isMobile={isMobile} />
</ResizablePanel>
</ResizablePanelGroup>
);
}