Skip to content

Commit 5632abe

Browse files
committed
fix (chat): new chat creation issue
1 parent befeb54 commit 5632abe

10 files changed

Lines changed: 17 additions & 282 deletions

File tree

src/client/app/chat/[[...chatId]]/page.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ export default function ChatPage({ params }) {
325325
const urlChatId = params.chatId ? params.chatId[0] : null
326326

327327
const [activeChatId, setActiveChatId] = useState(urlChatId)
328-
const [isHistoryOpen, setIsHistoryOpen] = useState(true)
328+
const [isHistoryOpen, setIsHistoryOpen] = useState(false)
329329
const [activeDomain, setActiveDomain] = useState("Featured")
330330
const [chatList, setChatList] = useState([])
331331
const [messages, setMessages] = useState([])
@@ -589,6 +589,8 @@ export default function ChatPage({ params }) {
589589
}
590590

591591
const handleNewChat = () => {
592+
setActiveChatId(null)
593+
setMessages([])
592594
router.push("/chat")
593595
}
594596

src/client/app/home/page.js

Lines changed: 0 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -1,222 +0,0 @@
1-
"use client"
2-
3-
import React, { useState, useEffect, useRef, useCallback } from "react"
4-
import { useRouter, usePathname, useParams } from "next/navigation"
5-
import { IconSend, IconPlus, IconLoader } from "@tabler/icons-react"
6-
import ChatBubble from "@components/ChatBubble"
7-
import toast from "react-hot-toast"
8-
9-
export default function HomePage() {
10-
const router = useRouter()
11-
const pathname = usePathname()
12-
const params = useParams()
13-
14-
const [chatId, setChatId] = useState(null)
15-
const [messages, setMessages] = useState([])
16-
const [input, setInput] = useState("")
17-
const [isLoading, setIsLoading] = useState(false)
18-
const [isStreaming, setIsStreaming] = useState(false)
19-
const messagesEndRef = useRef(null)
20-
21-
// Existing useEffect for params and pathname
22-
useEffect(() => {
23-
const urlChatId =
24-
params.chatId ||
25-
(Array.isArray(params.slug) ? params.slug[0] : null)
26-
if (!urlChatId && pathname === "/chat") {
27-
setMessages([])
28-
setChatId(null)
29-
return
30-
}
31-
setChatId(urlChatId)
32-
}, [params, pathname])
33-
34-
// Existing useEffect for fetching history
35-
useEffect(() => {
36-
const fetchHistory = async () => {
37-
if (!chatId) {
38-
setMessages([])
39-
return
40-
}
41-
setIsLoading(true)
42-
try {
43-
const response = await fetch(`/api/chat/${chatId}/history`)
44-
if (!response.ok) {
45-
throw new Error(`HTTP error! status: ${response.status}`)
46-
}
47-
const data = await response.json()
48-
setMessages(data.messages)
49-
} catch (error) {
50-
console.error("Failed to fetch chat history:", error)
51-
toast.error("Failed to load chat history.")
52-
setMessages([])
53-
} finally {
54-
setIsLoading(false)
55-
}
56-
}
57-
fetchHistory()
58-
}, [chatId])
59-
60-
// New useEffect for route change
61-
useEffect(() => {
62-
const handleRouteChange = (url) => {
63-
if (url === "/chat") {
64-
setMessages([])
65-
setChatId(null)
66-
}
67-
}
68-
69-
router.events.on("routeChangeComplete", handleRouteChange)
70-
71-
return () => {
72-
router.events.off("routeChangeComplete", handleRouteChange)
73-
}
74-
}, [router])
75-
76-
const scrollToBottom = useCallback(() => {
77-
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" })
78-
}, [])
79-
80-
useEffect(scrollToBottom, [messages])
81-
82-
const handleNewChat = () => {
83-
setMessages([]) // always clear messages
84-
setChatId(null) // clear current chatId so that new chat gets created
85-
router.push("/chat") // navigate to base chat page
86-
}
87-
88-
const handleSend = async (e) => {
89-
e.preventDefault()
90-
if (!input.trim() || isStreaming) return
91-
92-
const userMessage = { role: "user", content: input }
93-
setMessages((prev) => [...prev, userMessage])
94-
setInput("")
95-
setIsStreaming(true)
96-
setIsLoading(true)
97-
98-
try {
99-
const response = await fetch("/api/chat", {
100-
method: "POST",
101-
headers: {
102-
"Content-Type": "application/json"
103-
},
104-
body: JSON.stringify({
105-
chat_id: chatId,
106-
message: input
107-
})
108-
})
109-
110-
if (!response.ok || !response.body) {
111-
throw new Error(`HTTP error! status: ${response.status}`)
112-
}
113-
114-
const assistantMessage = { role: "assistant", content: "" }
115-
setMessages((prev) => [...prev, assistantMessage])
116-
117-
const reader = response.body.getReader()
118-
const decoder = new TextDecoder()
119-
let currentChatId = chatId
120-
121-
while (true) {
122-
const { value, done } = await reader.read()
123-
if (done) break
124-
125-
const chunk = decoder.decode(value, { stream: true })
126-
const lines = chunk
127-
.split("\n")
128-
.filter((line) => line.trim() !== "")
129-
130-
for (const line of lines) {
131-
try {
132-
const json = JSON.parse(line)
133-
if (json.chat_id && !currentChatId) {
134-
const newChatId = json.chat_id
135-
// FIX: Update URL without a full page reload to preserve the stream
136-
window.history.replaceState(
137-
{},
138-
"",
139-
`/chat/${newChatId}`
140-
)
141-
setChatId(newChatId)
142-
currentChatId = newChatId // Update for subsequent chunks in this stream
143-
}
144-
if (json.token) {
145-
assistantMessage.content += json.token
146-
setMessages((prev) => {
147-
const newMessages = [...prev]
148-
newMessages[newMessages.length - 1] = {
149-
...assistantMessage
150-
}
151-
return newMessages
152-
})
153-
}
154-
} catch (error) {
155-
console.error(
156-
"Failed to parse JSON from stream:",
157-
error,
158-
"Line:",
159-
line
160-
)
161-
}
162-
}
163-
}
164-
} catch (error) {
165-
console.error("Streaming error:", error)
166-
toast.error("Failed to get response from AI.")
167-
setMessages((prev) => prev.slice(0, prev.length - 1)) // Remove the empty assistant message
168-
} finally {
169-
setIsStreaming(false)
170-
setIsLoading(false)
171-
}
172-
}
173-
174-
return (
175-
<div className="flex-1 flex flex-col h-screen bg-neutral-900 text-white md:pl-20">
176-
<main className="flex-1 flex flex-col overflow-hidden">
177-
<header className="flex items-center justify-between p-4 border-b border-neutral-800">
178-
<h1 className="text-xl font-semibold">Chat</h1>
179-
<button
180-
onClick={handleNewChat}
181-
className="flex items-center gap-2 px-3 py-2 text-sm rounded-lg bg-neutral-800 hover:bg-neutral-700"
182-
>
183-
<IconPlus size={16} /> New Chat
184-
</button>
185-
</header>
186-
<div className="flex-1 overflow-y-auto p-4 space-y-4">
187-
{messages.map((msg, i) => (
188-
<ChatBubble
189-
key={i}
190-
message={msg.content}
191-
isUser={msg.role === "user"}
192-
/>
193-
))}
194-
{isLoading && <IconLoader className="animate-spin" />}
195-
<div ref={messagesEndRef} />
196-
</div>
197-
<div className="p-4 border-t border-neutral-800">
198-
<form
199-
onSubmit={handleSend}
200-
className="flex items-center gap-3"
201-
>
202-
<input
203-
type="text"
204-
value={input}
205-
onChange={(e) => setInput(e.target.value)}
206-
placeholder="Ask Sentient anything..."
207-
className="flex-1 p-3 bg-neutral-800 rounded-lg focus:ring-2 focus:ring-blue-500"
208-
disabled={isStreaming}
209-
/>
210-
<button
211-
type="submit"
212-
className="p-3 bg-blue-600 rounded-lg"
213-
disabled={isStreaming || !input.trim()}
214-
>
215-
<IconSend size={20} />
216-
</button>
217-
</form>
218-
</div>
219-
</main>
220-
</div>
221-
)
222-
}

src/client/app/integrations/page.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,4 +815,3 @@ const IntegrationsPage = () => {
815815
}
816816

817817
export default IntegrationsPage
818-

src/client/app/page.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import React from "react"
1212
*
1313
* This component is responsible for checking the user's authentication and onboarding status,
1414
* then redirecting them to the appropriate page (/auth/login, /onboarding, or /chat).
15-
* It displays a loading animation during this process.
15+
* It displays a loading animation during this process.
1616
* The final destination for an authenticated and onboarded user is /chat.
1717
* @returns {React.ReactNode} - The Home component UI.
1818
*/
@@ -124,7 +124,7 @@ const Home = () => {
124124

125125
// Show a loading screen while checking authentication or onboarding status.
126126
return (
127-
<div className="flex-1 min-h-screen flex flex-col items-center justify-center bg-[var(--color-primary-background)]">
127+
<div className="flex-1 min-h-screen flex flex-col items-center justify-center bg-black">
128128
<div className="flex flex-col items-center justify-center h-full backdrop-blur-xs">
129129
<AnimatedLogo />
130130
<h1 className="text-white text-4xl mt-4">Sentient</h1>
@@ -134,4 +134,3 @@ const Home = () => {
134134
}
135135

136136
export default Home
137-

src/client/app/settings/page.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ const ShortcutsSettings = () => {
404404
{ keys: ["Ctrl", "K"], description: "Open Command Palette" }
405405
],
406406
Navigation: [
407-
{ keys: ["Ctrl", "H"], description: "Go to Home" },
407+
{ keys: ["Ctrl", "H"], description: "Go to Chat" },
408408
{ keys: ["Ctrl", "J"], description: "Go to Notes" },
409409
{ keys: ["Ctrl", "A"], description: "Go to Tasks" },
410410
{ keys: ["Ctrl", "I"], description: "Go to Integrations" },
@@ -1463,4 +1463,3 @@ const ProfilePage = () => {
14631463
}
14641464

14651465
export default ProfilePage
1466-

src/client/app/tasks/page.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,3 @@ export default function TasksPage() {
479479
</Suspense>
480480
)
481481
}
482-

src/client/components/CommandPallete.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import React, { useState, useEffect } from "react"
44
import { Command } from "cmdk"
55
import {
6-
IconHome,
6+
IconMessage,
77
IconChecklist,
88
IconPlugConnected,
99
IconAdjustments,
@@ -44,8 +44,8 @@ const CommandPalette = ({ open, setOpen }) => {
4444
<Command.Item
4545
onSelect={() => runCommand(() => router.push("/chat"))}
4646
>
47-
<IconHome className="mr-2 h-4 w-4" />
48-
Go to Home
47+
<IconMessage className="mr-2 h-4 w-4" />
48+
Go to Chat
4949
</Command.Item>
5050
<Command.Item
5151
onSelect={() =>

src/client/components/FloatingNav.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { FloatingDock } from "@components/ui/floating-dock"
55
import {
66
IconAdjustments,
77
IconChecklist,
8-
IconHome,
8+
IconMessage,
99
IconLogout,
1010
IconPlugConnected
1111
} from "@tabler/icons-react"
@@ -30,7 +30,7 @@ export default function FloatingNav() {
3030
title: "Chat",
3131
href: "/chat",
3232
icon: (
33-
<IconHome className="h-full w-full text-neutral-500 dark:text-neutral-300" />
33+
<IconMessage className="h-full w-full text-neutral-500 dark:text-neutral-300" />
3434
)
3535
},
3636
{

src/client/components/Sidebar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const Sidebar = () => {
2323
.then((data) => setUserDetails(data))
2424
}, [])
2525
const navLinks = [
26-
{ title: "Chat", href: "/chat", icon: <IconHome size={28} /> },
26+
{ title: "Chat", href: "/chat", icon: <IconMessage size={28} /> },
2727
{ title: "Tasks", href: "/tasks", icon: <IconChecklist size={28} /> },
2828
{
2929
title: "Integrations",
@@ -95,4 +95,4 @@ const Sidebar = () => {
9595
</div>
9696
)
9797
}
98-
export default Sidebar
98+
export default Sidebar

0 commit comments

Comments
 (0)