-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4dc7d1
commit 5787057
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use client'; | ||
|
||
import React, { useEffect } from 'react'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useAuth } from '@/hooks/useAuth'; | ||
import { useChatSessions } from '@/hooks/useChatSessions'; | ||
import { Spinner } from '@/components/atoms/Spinner'; | ||
import { Button } from '@/components/atoms/Button'; | ||
|
||
export default function ChatPage() { | ||
const { user, isAuthenticated } = useAuth(); | ||
const { | ||
data: chatSessions = [], | ||
isLoading: sessionsLoading, | ||
isError: sessionsError, | ||
} = useChatSessions(user?.userId || ''); | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
if (!isAuthenticated) { | ||
router.push('/auth'); | ||
} | ||
}, [isAuthenticated, router]); | ||
|
||
if (sessionsLoading) { | ||
return <Spinner />; | ||
} | ||
|
||
if (sessionsError) { | ||
return <p className="text-red-500">Failed to load chat sessions.</p>; | ||
} | ||
|
||
if (chatSessions.length === 0) { | ||
return <p className="text-gray-500">You have no active now.</p>; | ||
} | ||
|
||
return ( | ||
<div className="max-w-2xl mx-auto p-6"> | ||
<h1 className="text-2xl font-bold mb-4">Your Chats</h1> | ||
<ul> | ||
{chatSessions.map((session) => ( | ||
<li key={session.id} className="border-b p-2"> | ||
<Button | ||
className="text-blue-500 underline" | ||
onClick={() => router.push(`/chat/${session.id}`)} | ||
> | ||
{session.sessionName || `Session ${session.id}`} | ||
</Button> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
import { useChatMessages } from '@/hooks/useChatMessages'; | ||
import { useSendMessage } from '@/hooks/useSendMessage'; | ||
import { ChatLayout } from '@/components/organisms/ChatLayout'; | ||
import { Spinner } from '@/components/atoms/Spinner'; | ||
|
||
interface ChatContainerProps { | ||
sessionId: string; | ||
} | ||
|
||
export const ChatContainer: React.FC<ChatContainerProps> = ({ sessionId }) => { | ||
const { | ||
data: messages = [], | ||
isLoading: messagesLoading, | ||
isError: messagesError, | ||
} = useChatMessages(sessionId); | ||
|
||
const { mutate: sendMessage, isPending: isSending } = useSendMessage(); | ||
|
||
const handleSendMessage = (message: string) => { | ||
sendMessage({ message, sender: 'user', sessionId, type: 'text' }); | ||
}; | ||
|
||
if (messagesLoading) return <Spinner />; | ||
if (messagesError) | ||
return <p className="text-red-500">Failed to load message.</p>; | ||
|
||
return ( | ||
<ChatLayout | ||
messages={messages} | ||
onSendMessage={handleSendMessage} | ||
isSending={isSending} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { collection, getDocs, query, where } from 'firebase/firestore'; | ||
import { db } from '../../firebaseConfig'; | ||
import type { ChatSession } from '@/types'; | ||
|
||
export const useChatSessions = (userId: string) => { | ||
return useQuery<ChatSession[], Error>({ | ||
queryKey: ['chatSessions', userId], | ||
queryFn: async () => { | ||
if (!userId) throw new Error('User ID is required'); | ||
|
||
const sessionRef = collection(db, 'chats'); | ||
const q = query(sessionRef, where('userIds', 'array-contains', userId)); | ||
const snapshot = await getDocs(q); | ||
|
||
return snapshot.docs.map((doc) => ({ | ||
...doc.data(), | ||
id: doc.id, | ||
})) as unknown as ChatSession[]; | ||
}, | ||
enabled: !!userId, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters