Skip to content

Commit

Permalink
feat : Chat Session Page
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-Dongwook committed Oct 28, 2024
1 parent b4dc7d1 commit 5787057
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/app/chat/page.tsx
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>
);
}
36 changes: 36 additions & 0 deletions src/components/containers/ChatContainer.tsx
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}
/>
);
};
23 changes: 23 additions & 0 deletions src/hooks/useChatSessions.ts
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,
});
};
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Timestamp } from 'firebase/firestore';
import { Key } from 'react';

// 1. Users
export type UserRole = 'user' | 'admin' | 'premium';
Expand Down Expand Up @@ -31,6 +32,7 @@ export interface ChatMessage {
}

export interface ChatSession {
id?: Key | null | undefined;
sessionName: string;
createdAt: Timestamp;
lastActivity: Timestamp;
Expand Down

0 comments on commit 5787057

Please sign in to comment.