Commit ·
05fd38f
1
Parent(s): e586655
working app with local session storage
Browse files- Dockerfile +3 -0
- src/app/globals.css +16 -27
- src/app/layout.tsx +2 -2
- src/lib/SessionManager.ts +42 -21
- src/webzero/chat-page.tsx +171 -171
Dockerfile
CHANGED
|
@@ -8,6 +8,9 @@ RUN npm install
|
|
| 8 |
|
| 9 |
COPY . .
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
EXPOSE 3000
|
| 12 |
|
| 13 |
ENV NODE_ENV=development
|
|
|
|
| 8 |
|
| 9 |
COPY . .
|
| 10 |
|
| 11 |
+
RUN mkdir -p /usr/src/app/src/components/generated && \
|
| 12 |
+
chmod -R 755 /usr/src/app/src/components/generated
|
| 13 |
+
|
| 14 |
EXPOSE 3000
|
| 15 |
|
| 16 |
ENV NODE_ENV=development
|
src/app/globals.css
CHANGED
|
@@ -74,17 +74,6 @@
|
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
| 77 |
-
html,
|
| 78 |
-
body,
|
| 79 |
-
#__next {
|
| 80 |
-
height: 100%;
|
| 81 |
-
overflow: hidden;
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
.chat-scroll {
|
| 85 |
-
height: calc(100vh - 8rem);
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
.loader {
|
| 89 |
border: 4px solid hsl(var(--muted));
|
| 90 |
border-top: 4px solid hsl(var(--primary));
|
|
@@ -104,34 +93,34 @@ body,
|
|
| 104 |
}
|
| 105 |
}
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
::-webkit-scrollbar-track {
|
| 113 |
-
background: hsl(var(--background));
|
| 114 |
}
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
border-radius: 4px;
|
| 119 |
}
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
}
|
| 124 |
|
| 125 |
.fullscreen-preview {
|
| 126 |
-
top:
|
| 127 |
-
position: fixed;
|
| 128 |
left: 0;
|
| 129 |
right: 0;
|
| 130 |
bottom: 0;
|
|
|
|
| 131 |
z-index: 50;
|
|
|
|
| 132 |
background-color: hsl(var(--background));
|
| 133 |
}
|
| 134 |
|
| 135 |
-
.
|
| 136 |
-
|
|
|
|
|
|
|
| 137 |
}
|
|
|
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
.loader {
|
| 78 |
border: 4px solid hsl(var(--muted));
|
| 79 |
border-top: 4px solid hsl(var(--primary));
|
|
|
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
+
html,
|
| 97 |
+
body,
|
| 98 |
+
#__next {
|
| 99 |
+
height: 100%;
|
|
|
|
|
|
|
|
|
|
| 100 |
}
|
| 101 |
|
| 102 |
+
body {
|
| 103 |
+
overflow: auto;
|
|
|
|
| 104 |
}
|
| 105 |
|
| 106 |
+
.chat-scroll {
|
| 107 |
+
flex: 1;
|
| 108 |
+
overflow-y: auto;
|
| 109 |
}
|
| 110 |
|
| 111 |
.fullscreen-preview {
|
| 112 |
+
top: 0px;
|
|
|
|
| 113 |
left: 0;
|
| 114 |
right: 0;
|
| 115 |
bottom: 0;
|
| 116 |
+
position: fixed;
|
| 117 |
z-index: 50;
|
| 118 |
+
overflow-y: auto;
|
| 119 |
background-color: hsl(var(--background));
|
| 120 |
}
|
| 121 |
|
| 122 |
+
.flex-container {
|
| 123 |
+
height: 100%;
|
| 124 |
+
display: flex;
|
| 125 |
+
flex-direction: column;
|
| 126 |
}
|
src/app/layout.tsx
CHANGED
|
@@ -18,12 +18,12 @@ export default function RootLayout({
|
|
| 18 |
}>) {
|
| 19 |
return (
|
| 20 |
<html lang="en" suppressHydrationWarning>
|
| 21 |
-
<body className="antialiased
|
| 22 |
<SessionProvider>
|
| 23 |
<SidebarProvider>
|
| 24 |
<ThemeProvider>
|
| 25 |
<AppSidebar />
|
| 26 |
-
<main
|
| 27 |
<Toaster />
|
| 28 |
</ThemeProvider>
|
| 29 |
</SidebarProvider>
|
|
|
|
| 18 |
}>) {
|
| 19 |
return (
|
| 20 |
<html lang="en" suppressHydrationWarning>
|
| 21 |
+
<body className="antialiased">
|
| 22 |
<SessionProvider>
|
| 23 |
<SidebarProvider>
|
| 24 |
<ThemeProvider>
|
| 25 |
<AppSidebar />
|
| 26 |
+
<main className="flex-1 w-full">{children}</main>
|
| 27 |
<Toaster />
|
| 28 |
</ThemeProvider>
|
| 29 |
</SidebarProvider>
|
src/lib/SessionManager.ts
CHANGED
|
@@ -16,40 +16,61 @@ const SESSION_STORAGE_KEY = "webzero_sessions";
|
|
| 16 |
const CURRENT_SESSION_ID_KEY = "webzero_current_session_id";
|
| 17 |
|
| 18 |
export function useSessions() {
|
| 19 |
-
const [sessions, setSessions] = useState<Session[]>(
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
}
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
| 33 |
const newSession = createNewSessionObject();
|
| 34 |
setSessions([newSession]);
|
| 35 |
setCurrentSessionId(newSession.id);
|
| 36 |
-
} else if (
|
| 37 |
-
storedCurrentSessionId &&
|
| 38 |
-
parsedSessions.some((s) => s.id === storedCurrentSessionId)
|
| 39 |
-
) {
|
| 40 |
-
setCurrentSessionId(storedCurrentSessionId);
|
| 41 |
-
} else {
|
| 42 |
-
setCurrentSessionId(parsedSessions[0].id);
|
| 43 |
}
|
| 44 |
-
}, []);
|
| 45 |
|
| 46 |
useEffect(() => {
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
}, [sessions]);
|
| 49 |
|
| 50 |
useEffect(() => {
|
| 51 |
if (currentSessionId) {
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
}, [currentSessionId]);
|
| 55 |
|
|
|
|
| 16 |
const CURRENT_SESSION_ID_KEY = "webzero_current_session_id";
|
| 17 |
|
| 18 |
export function useSessions() {
|
| 19 |
+
const [sessions, setSessions] = useState<Session[]>(() => {
|
| 20 |
+
if (typeof window === "undefined") return [];
|
| 21 |
+
try {
|
| 22 |
+
const storedSessions = localStorage.getItem(SESSION_STORAGE_KEY);
|
| 23 |
+
return storedSessions ? JSON.parse(storedSessions) : [];
|
| 24 |
+
} catch (error) {
|
| 25 |
+
console.error("Failed to parse sessions from localStorage:", error);
|
| 26 |
+
return [];
|
| 27 |
+
}
|
| 28 |
+
});
|
| 29 |
|
| 30 |
+
const [currentSessionId, setCurrentSessionId] = useState<string | null>(
|
| 31 |
+
() => {
|
| 32 |
+
if (typeof window === "undefined") return null;
|
| 33 |
+
try {
|
| 34 |
+
const storedId = localStorage.getItem(CURRENT_SESSION_ID_KEY);
|
| 35 |
+
return storedId && sessions.some((s) => s.id === storedId)
|
| 36 |
+
? storedId
|
| 37 |
+
: sessions[0]?.id || null;
|
| 38 |
+
} catch (error) {
|
| 39 |
+
console.error(
|
| 40 |
+
"Failed to retrieve currentSessionId from localStorage:",
|
| 41 |
+
error
|
| 42 |
+
);
|
| 43 |
+
return sessions[0]?.id || null;
|
| 44 |
+
}
|
| 45 |
}
|
| 46 |
+
);
|
| 47 |
|
| 48 |
+
useEffect(() => {
|
| 49 |
+
if (sessions.length === 0) {
|
| 50 |
const newSession = createNewSessionObject();
|
| 51 |
setSessions([newSession]);
|
| 52 |
setCurrentSessionId(newSession.id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
+
}, [sessions]);
|
| 55 |
|
| 56 |
useEffect(() => {
|
| 57 |
+
try {
|
| 58 |
+
localStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(sessions));
|
| 59 |
+
} catch (error) {
|
| 60 |
+
console.error("Failed to save sessions to localStorage:", error);
|
| 61 |
+
}
|
| 62 |
}, [sessions]);
|
| 63 |
|
| 64 |
useEffect(() => {
|
| 65 |
if (currentSessionId) {
|
| 66 |
+
try {
|
| 67 |
+
localStorage.setItem(CURRENT_SESSION_ID_KEY, currentSessionId);
|
| 68 |
+
} catch (error) {
|
| 69 |
+
console.error(
|
| 70 |
+
"Failed to save currentSessionId to localStorage:",
|
| 71 |
+
error
|
| 72 |
+
);
|
| 73 |
+
}
|
| 74 |
}
|
| 75 |
}, [currentSessionId]);
|
| 76 |
|
src/webzero/chat-page.tsx
CHANGED
|
@@ -32,7 +32,13 @@ import ThemeToggle from "@/components/ThemeToggle";
|
|
| 32 |
import { SidebarTrigger } from "@/components/ui/sidebar";
|
| 33 |
import { useSessionContext } from "@/context/SessionContext";
|
| 34 |
|
| 35 |
-
function CodeMessageCard({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
return (
|
| 37 |
<div
|
| 38 |
onClick={onClick}
|
|
@@ -97,6 +103,9 @@ export function ChatPage() {
|
|
| 97 |
isFullscreen: false,
|
| 98 |
isOpen: false,
|
| 99 |
});
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
const { theme } = useTheme();
|
| 102 |
const isDark = theme === "dark";
|
|
@@ -121,6 +130,42 @@ export function ChatPage() {
|
|
| 121 |
(session) => session.id === currentSessionId
|
| 122 |
);
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
useEffect(() => {
|
| 125 |
scrollToBottom();
|
| 126 |
}, [currentSession?.messages]);
|
|
@@ -161,6 +206,7 @@ export function ChatPage() {
|
|
| 161 |
currentCode: response.content,
|
| 162 |
});
|
| 163 |
setPreviewState((prev) => ({ ...prev, isOpen: true }));
|
|
|
|
| 164 |
} catch (error) {
|
| 165 |
console.error("Error:", error);
|
| 166 |
} finally {
|
|
@@ -201,7 +247,7 @@ export function ChatPage() {
|
|
| 201 |
};
|
| 202 |
|
| 203 |
return (
|
| 204 |
-
<div className={`
|
| 205 |
<header className="h-16 border-b flex items-center justify-between px-4 bg-background z-10">
|
| 206 |
<div className="flex items-center">
|
| 207 |
<SidebarTrigger />
|
|
@@ -209,7 +255,6 @@ export function ChatPage() {
|
|
| 209 |
</div>
|
| 210 |
<ThemeToggle />
|
| 211 |
</header>
|
| 212 |
-
|
| 213 |
<div className="flex-1 flex overflow-hidden">
|
| 214 |
{!currentSession || currentSession.messages.length === 0 ? (
|
| 215 |
<div className="flex-1 flex flex-col items-center justify-center p-4">
|
|
@@ -229,7 +274,7 @@ export function ChatPage() {
|
|
| 229 |
type="submit"
|
| 230 |
size="icon"
|
| 231 |
className="absolute bottom-3 right-3"
|
| 232 |
-
disabled={isLoading}
|
| 233 |
>
|
| 234 |
<Send className="h-4 w-4" />
|
| 235 |
</Button>
|
|
@@ -238,50 +283,37 @@ export function ChatPage() {
|
|
| 238 |
</div>
|
| 239 |
) : (
|
| 240 |
<ResizablePanelGroup direction="horizontal" className="flex-1">
|
| 241 |
-
<ResizablePanel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
<div className="flex flex-col h-full">
|
| 243 |
<ScrollArea className="flex-1 p-4">
|
| 244 |
{currentSession.messages.map((msg) => (
|
| 245 |
-
<div
|
| 246 |
-
|
| 247 |
-
className={`mb-4 flex ${
|
| 248 |
-
msg.from === "user" ? "justify-end" : "justify-start"
|
| 249 |
-
}`}
|
| 250 |
-
>
|
| 251 |
-
<div
|
| 252 |
-
className={`flex items-start gap-3 max-w-[80%] ${
|
| 253 |
-
msg.from === "user" ? "flex-row-reverse" : ""
|
| 254 |
-
}`}
|
| 255 |
-
>
|
| 256 |
<Avatar>
|
| 257 |
-
<AvatarImage
|
| 258 |
-
src={
|
| 259 |
-
msg.from === "user"
|
| 260 |
-
? "/user-avatar.png"
|
| 261 |
-
: "/ai-avatar.png"
|
| 262 |
-
}
|
| 263 |
-
/>
|
| 264 |
<AvatarFallback>
|
| 265 |
{msg.from === "user" ? "U" : "AI"}
|
| 266 |
</AvatarFallback>
|
| 267 |
</Avatar>
|
| 268 |
-
<div
|
| 269 |
-
className={`p-3 rounded-lg ${
|
| 270 |
-
msg.from === "user"
|
| 271 |
-
? "bg-primary text-primary-foreground"
|
| 272 |
-
: "bg-muted"
|
| 273 |
-
}`}
|
| 274 |
-
>
|
| 275 |
{msg.from === "user" ? (
|
| 276 |
<p>{msg.content}</p>
|
| 277 |
) : (
|
| 278 |
<CodeMessageCard
|
| 279 |
-
|
|
|
|
|
|
|
| 280 |
setPreviewState((prev) => ({
|
| 281 |
...prev,
|
| 282 |
isOpen: true,
|
| 283 |
-
}))
|
| 284 |
-
}
|
| 285 |
/>
|
| 286 |
)}
|
| 287 |
</div>
|
|
@@ -290,7 +322,6 @@ export function ChatPage() {
|
|
| 290 |
))}
|
| 291 |
<div ref={messagesEndRef} />
|
| 292 |
</ScrollArea>
|
| 293 |
-
|
| 294 |
<div className="border-t p-4">
|
| 295 |
<form onSubmit={handleSubmit} className="relative">
|
| 296 |
{isLoading ? (
|
|
@@ -312,7 +343,7 @@ export function ChatPage() {
|
|
| 312 |
type="submit"
|
| 313 |
size="icon"
|
| 314 |
className="absolute bottom-3 right-3"
|
| 315 |
-
disabled={isLoading}
|
| 316 |
>
|
| 317 |
<Send className="h-4 w-4" />
|
| 318 |
</Button>
|
|
@@ -322,146 +353,115 @@ export function ChatPage() {
|
|
| 322 |
</div>
|
| 323 |
</div>
|
| 324 |
</ResizablePanel>
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
|
|
|
| 336 |
>
|
| 337 |
-
<
|
| 338 |
-
|
| 339 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
>
|
| 341 |
-
<
|
| 342 |
-
<
|
| 343 |
-
<
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
<div className="flex items-center gap-1">
|
| 361 |
-
<Button
|
| 362 |
-
variant="outline"
|
| 363 |
-
size="sm"
|
| 364 |
-
onClick={() =>
|
| 365 |
-
handleCopyCode(
|
| 366 |
-
currentSession.messages[
|
| 367 |
-
currentSession.messages.length - 1
|
| 368 |
-
].content
|
| 369 |
-
)
|
| 370 |
-
}
|
| 371 |
-
>
|
| 372 |
-
<Copy className="h-4 w-4 mr-2" />
|
| 373 |
-
Copy
|
| 374 |
-
</Button>
|
| 375 |
-
|
| 376 |
-
<Button
|
| 377 |
-
variant="outline"
|
| 378 |
-
size="sm"
|
| 379 |
-
onClick={() =>
|
| 380 |
-
handleDownloadCode(
|
| 381 |
-
currentSession.messages[
|
| 382 |
-
currentSession.messages.length - 1
|
| 383 |
-
].content,
|
| 384 |
-
`code-${
|
| 385 |
-
currentSession.messages[
|
| 386 |
-
currentSession.messages.length - 1
|
| 387 |
-
].id
|
| 388 |
-
}.tsx`
|
| 389 |
-
)
|
| 390 |
-
}
|
| 391 |
-
>
|
| 392 |
-
<Download className="h-4 w-4 mr-2" />
|
| 393 |
-
Download
|
| 394 |
-
</Button>
|
| 395 |
-
|
| 396 |
-
<Button
|
| 397 |
-
variant="ghost"
|
| 398 |
-
size="icon"
|
| 399 |
-
onClick={toggleFullscreen}
|
| 400 |
-
>
|
| 401 |
-
{previewState.isFullscreen ? (
|
| 402 |
-
<Minimize2 className="h-4 w-4" />
|
| 403 |
-
) : (
|
| 404 |
-
<Maximize2 className="h-4 w-4" />
|
| 405 |
-
)}
|
| 406 |
-
</Button>
|
| 407 |
-
|
| 408 |
-
<Button
|
| 409 |
-
variant="ghost"
|
| 410 |
-
size="icon"
|
| 411 |
-
onClick={() =>
|
| 412 |
-
setPreviewState((prev) => ({
|
| 413 |
-
...prev,
|
| 414 |
-
isOpen: false,
|
| 415 |
-
}))
|
| 416 |
-
}
|
| 417 |
>
|
| 418 |
-
|
| 419 |
-
</
|
| 420 |
</div>
|
| 421 |
-
</
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
<div className="p-4">
|
| 429 |
-
<DynamicFileRenderer
|
| 430 |
-
id={
|
| 431 |
-
currentSession.messages[
|
| 432 |
-
currentSession.messages.length - 1
|
| 433 |
-
].id
|
| 434 |
-
}
|
| 435 |
-
/>
|
| 436 |
-
</div>
|
| 437 |
-
</ScrollArea>
|
| 438 |
-
</TabsContent>
|
| 439 |
-
|
| 440 |
-
<TabsContent
|
| 441 |
-
value="code"
|
| 442 |
-
className="flex-1 overflow-auto"
|
| 443 |
-
>
|
| 444 |
-
<ScrollArea className="h-full">
|
| 445 |
-
<div className="p-4">
|
| 446 |
-
<SyntaxHighlighter
|
| 447 |
-
language="typescript"
|
| 448 |
-
style={isDark ? customOneDark : customOneLight}
|
| 449 |
-
className="!m-0 !bg-transparent"
|
| 450 |
-
>
|
| 451 |
-
{
|
| 452 |
-
currentSession.messages[
|
| 453 |
-
currentSession.messages.length - 1
|
| 454 |
-
].content
|
| 455 |
-
}
|
| 456 |
-
</SyntaxHighlighter>
|
| 457 |
-
</div>
|
| 458 |
-
</ScrollArea>
|
| 459 |
-
</TabsContent>
|
| 460 |
-
</Tabs>
|
| 461 |
-
</div>
|
| 462 |
-
</ResizablePanel>
|
| 463 |
-
</>
|
| 464 |
-
)}
|
| 465 |
</ResizablePanelGroup>
|
| 466 |
)}
|
| 467 |
</div>
|
|
|
|
| 32 |
import { SidebarTrigger } from "@/components/ui/sidebar";
|
| 33 |
import { useSessionContext } from "@/context/SessionContext";
|
| 34 |
|
| 35 |
+
function CodeMessageCard({
|
| 36 |
+
message,
|
| 37 |
+
onClick,
|
| 38 |
+
}: {
|
| 39 |
+
message: Message;
|
| 40 |
+
onClick: () => void;
|
| 41 |
+
}) {
|
| 42 |
return (
|
| 43 |
<div
|
| 44 |
onClick={onClick}
|
|
|
|
| 103 |
isFullscreen: false,
|
| 104 |
isOpen: false,
|
| 105 |
});
|
| 106 |
+
const [selectedAIMessage, setSelectedAIMessage] = useState<Message | null>(
|
| 107 |
+
null
|
| 108 |
+
);
|
| 109 |
|
| 110 |
const { theme } = useTheme();
|
| 111 |
const isDark = theme === "dark";
|
|
|
|
| 130 |
(session) => session.id === currentSessionId
|
| 131 |
);
|
| 132 |
|
| 133 |
+
useEffect(() => {
|
| 134 |
+
setSelectedAIMessage(null);
|
| 135 |
+
setPreviewState((prev) => ({
|
| 136 |
+
...prev,
|
| 137 |
+
isOpen: false,
|
| 138 |
+
isFullscreen: false,
|
| 139 |
+
}));
|
| 140 |
+
|
| 141 |
+
if (currentSession && currentSession.messages.length > 0) {
|
| 142 |
+
const lastAIMessage = currentSession.messages
|
| 143 |
+
.slice()
|
| 144 |
+
.reverse()
|
| 145 |
+
.find((msg) => msg.from === "ai");
|
| 146 |
+
|
| 147 |
+
if (lastAIMessage) {
|
| 148 |
+
const precedingUserMessage =
|
| 149 |
+
currentSession.messages[
|
| 150 |
+
currentSession.messages.indexOf(lastAIMessage) - 1
|
| 151 |
+
];
|
| 152 |
+
|
| 153 |
+
if (precedingUserMessage && precedingUserMessage.from === "user") {
|
| 154 |
+
setCurrentIteration({
|
| 155 |
+
previousDescription: precedingUserMessage.content,
|
| 156 |
+
currentCode: lastAIMessage.content,
|
| 157 |
+
});
|
| 158 |
+
} else {
|
| 159 |
+
setCurrentIteration(null);
|
| 160 |
+
}
|
| 161 |
+
} else {
|
| 162 |
+
setCurrentIteration(null);
|
| 163 |
+
}
|
| 164 |
+
} else {
|
| 165 |
+
setCurrentIteration(null);
|
| 166 |
+
}
|
| 167 |
+
}, [currentSessionId, currentSession]);
|
| 168 |
+
|
| 169 |
useEffect(() => {
|
| 170 |
scrollToBottom();
|
| 171 |
}, [currentSession?.messages]);
|
|
|
|
| 206 |
currentCode: response.content,
|
| 207 |
});
|
| 208 |
setPreviewState((prev) => ({ ...prev, isOpen: true }));
|
| 209 |
+
setSelectedAIMessage(response);
|
| 210 |
} catch (error) {
|
| 211 |
console.error("Error:", error);
|
| 212 |
} finally {
|
|
|
|
| 247 |
};
|
| 248 |
|
| 249 |
return (
|
| 250 |
+
<div className={`flex flex-col h-full ${isDark ? "dark" : ""}`}>
|
| 251 |
<header className="h-16 border-b flex items-center justify-between px-4 bg-background z-10">
|
| 252 |
<div className="flex items-center">
|
| 253 |
<SidebarTrigger />
|
|
|
|
| 255 |
</div>
|
| 256 |
<ThemeToggle />
|
| 257 |
</header>
|
|
|
|
| 258 |
<div className="flex-1 flex overflow-hidden">
|
| 259 |
{!currentSession || currentSession.messages.length === 0 ? (
|
| 260 |
<div className="flex-1 flex flex-col items-center justify-center p-4">
|
|
|
|
| 274 |
type="submit"
|
| 275 |
size="icon"
|
| 276 |
className="absolute bottom-3 right-3"
|
| 277 |
+
disabled={isLoading || message.trim() === ""}
|
| 278 |
>
|
| 279 |
<Send className="h-4 w-4" />
|
| 280 |
</Button>
|
|
|
|
| 283 |
</div>
|
| 284 |
) : (
|
| 285 |
<ResizablePanelGroup direction="horizontal" className="flex-1">
|
| 286 |
+
<ResizablePanel
|
| 287 |
+
defaultSize={50}
|
| 288 |
+
minSize={30}
|
| 289 |
+
className={`flex flex-col h-full ${
|
| 290 |
+
previewState.isOpen ? "pointer-events-none" : ""
|
| 291 |
+
}`}
|
| 292 |
+
>
|
| 293 |
<div className="flex flex-col h-full">
|
| 294 |
<ScrollArea className="flex-1 p-4">
|
| 295 |
{currentSession.messages.map((msg) => (
|
| 296 |
+
<div key={msg.id} className="mb-4 flex">
|
| 297 |
+
<div className="flex items-start gap-3 max-w-[80%]">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
<Avatar>
|
| 299 |
+
<AvatarImage />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
<AvatarFallback>
|
| 301 |
{msg.from === "user" ? "U" : "AI"}
|
| 302 |
</AvatarFallback>
|
| 303 |
</Avatar>
|
| 304 |
+
<div className="p-3 rounded-lg bg-muted">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
{msg.from === "user" ? (
|
| 306 |
<p>{msg.content}</p>
|
| 307 |
) : (
|
| 308 |
<CodeMessageCard
|
| 309 |
+
message={msg}
|
| 310 |
+
onClick={() => {
|
| 311 |
+
setSelectedAIMessage(msg);
|
| 312 |
setPreviewState((prev) => ({
|
| 313 |
...prev,
|
| 314 |
isOpen: true,
|
| 315 |
+
}));
|
| 316 |
+
}}
|
| 317 |
/>
|
| 318 |
)}
|
| 319 |
</div>
|
|
|
|
| 322 |
))}
|
| 323 |
<div ref={messagesEndRef} />
|
| 324 |
</ScrollArea>
|
|
|
|
| 325 |
<div className="border-t p-4">
|
| 326 |
<form onSubmit={handleSubmit} className="relative">
|
| 327 |
{isLoading ? (
|
|
|
|
| 343 |
type="submit"
|
| 344 |
size="icon"
|
| 345 |
className="absolute bottom-3 right-3"
|
| 346 |
+
disabled={isLoading || message.trim() === ""}
|
| 347 |
>
|
| 348 |
<Send className="h-4 w-4" />
|
| 349 |
</Button>
|
|
|
|
| 353 |
</div>
|
| 354 |
</div>
|
| 355 |
</ResizablePanel>
|
| 356 |
+
{previewState.isOpen && (
|
| 357 |
+
<>
|
| 358 |
+
<ResizableHandle />
|
| 359 |
+
<ResizablePanel defaultSize={50} minSize={30}>
|
| 360 |
+
<div
|
| 361 |
+
className={`h-full ${
|
| 362 |
+
previewState.isFullscreen ? "fullscreen-preview" : ""
|
| 363 |
+
}`}
|
| 364 |
+
>
|
| 365 |
+
<Tabs
|
| 366 |
+
defaultValue="preview"
|
| 367 |
+
className="h-full flex flex-col"
|
| 368 |
>
|
| 369 |
+
<div className="border-b px-4 flex justify-between items-center">
|
| 370 |
+
<TabsList>
|
| 371 |
+
<TabsTrigger
|
| 372 |
+
value="preview"
|
| 373 |
+
className="flex items-center gap-2"
|
| 374 |
+
>
|
| 375 |
+
<Eye className="h-4 w-4" /> Preview
|
| 376 |
+
</TabsTrigger>
|
| 377 |
+
<TabsTrigger
|
| 378 |
+
value="code"
|
| 379 |
+
className="flex items-center gap-1"
|
| 380 |
+
>
|
| 381 |
+
<Code className="h-4 w-4" /> Code
|
| 382 |
+
</TabsTrigger>
|
| 383 |
+
</TabsList>
|
| 384 |
+
<div className="flex items-center gap-1">
|
| 385 |
+
<Button
|
| 386 |
+
variant="outline"
|
| 387 |
+
size="sm"
|
| 388 |
+
onClick={() =>
|
| 389 |
+
handleCopyCode(selectedAIMessage?.content || "")
|
| 390 |
+
}
|
| 391 |
+
>
|
| 392 |
+
<Copy className="h-4 w-4 mr-2" /> Copy
|
| 393 |
+
</Button>
|
| 394 |
+
<Button
|
| 395 |
+
variant="outline"
|
| 396 |
+
size="sm"
|
| 397 |
+
onClick={() =>
|
| 398 |
+
handleDownloadCode(
|
| 399 |
+
selectedAIMessage?.content || "",
|
| 400 |
+
`code-${selectedAIMessage?.id || "unknown"}.tsx`
|
| 401 |
+
)
|
| 402 |
+
}
|
| 403 |
+
>
|
| 404 |
+
<Download className="h-4 w-4 mr-2" /> Download
|
| 405 |
+
</Button>
|
| 406 |
+
<Button
|
| 407 |
+
variant="ghost"
|
| 408 |
+
size="icon"
|
| 409 |
+
onClick={toggleFullscreen}
|
| 410 |
+
>
|
| 411 |
+
{previewState.isFullscreen ? (
|
| 412 |
+
<Minimize2 className="h-4 w-4" />
|
| 413 |
+
) : (
|
| 414 |
+
<Maximize2 className="h-4 w-4" />
|
| 415 |
+
)}
|
| 416 |
+
</Button>
|
| 417 |
+
<Button
|
| 418 |
+
variant="ghost"
|
| 419 |
+
size="icon"
|
| 420 |
+
onClick={() => {
|
| 421 |
+
setPreviewState((prev) => ({
|
| 422 |
+
...prev,
|
| 423 |
+
isOpen: false,
|
| 424 |
+
}));
|
| 425 |
+
setSelectedAIMessage(null);
|
| 426 |
+
}}
|
| 427 |
+
>
|
| 428 |
+
<X className="h-4 w-4" />
|
| 429 |
+
</Button>
|
| 430 |
+
</div>
|
| 431 |
+
</div>
|
| 432 |
+
<TabsContent
|
| 433 |
+
value="preview"
|
| 434 |
+
className="flex-1 overflow-auto"
|
| 435 |
>
|
| 436 |
+
<ScrollArea className="h-full">
|
| 437 |
+
<div className="p-4">
|
| 438 |
+
<DynamicFileRenderer
|
| 439 |
+
id={selectedAIMessage?.id || ""}
|
| 440 |
+
/>
|
| 441 |
+
</div>
|
| 442 |
+
</ScrollArea>
|
| 443 |
+
</TabsContent>
|
| 444 |
+
<TabsContent
|
| 445 |
+
value="code"
|
| 446 |
+
className="flex-1 overflow-auto"
|
| 447 |
+
>
|
| 448 |
+
<ScrollArea className="h-full">
|
| 449 |
+
<div className="p-4">
|
| 450 |
+
<SyntaxHighlighter
|
| 451 |
+
language="typescript"
|
| 452 |
+
style={isDark ? customOneDark : customOneLight}
|
| 453 |
+
className="!m-0 !bg-transparent"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
>
|
| 455 |
+
{selectedAIMessage?.content || ""}
|
| 456 |
+
</SyntaxHighlighter>
|
| 457 |
</div>
|
| 458 |
+
</ScrollArea>
|
| 459 |
+
</TabsContent>
|
| 460 |
+
</Tabs>
|
| 461 |
+
</div>
|
| 462 |
+
</ResizablePanel>
|
| 463 |
+
</>
|
| 464 |
+
)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
</ResizablePanelGroup>
|
| 466 |
)}
|
| 467 |
</div>
|