diff --git a/src/app/chat/[roomId]/page.tsx b/src/app/chat/[roomId]/page.tsx new file mode 100644 index 00000000..81b1da1d --- /dev/null +++ b/src/app/chat/[roomId]/page.tsx @@ -0,0 +1,63 @@ +'use client'; +import { useEffect, useRef, useState } from 'react'; + +import { DEFAULT_PROFILE_IMAGE } from 'constants/default-images'; + +import { ChatHeader, ChatInput, MyChat, OtherChat } from '@/components/pages/chat'; + +// 임시 데이터 +const data = Array.from({ length: 30 }, (_, index) => ({ + id: index + 1, + text: '안녕하세요 멍선생입니다 계속 입력하면 어떻게 되나 봅시다', + time: '오후 11:24', + profileImage: DEFAULT_PROFILE_IMAGE, + nickName: '흰둥이', + userId: index % 3 === 0 ? 0 : 1, +})); + +const myId = 0; + +const ChatRoomPage = () => { + const [messages, setMessages] = useState(data); + + const handleSubmit = (text: string) => { + const newMessage = { + id: Date.now(), + text, + profileImage: DEFAULT_PROFILE_IMAGE, + nickName: '흰둥이', + time: '지금', + userId: myId, + }; + + setMessages((prev) => [...prev, newMessage]); + }; + + const bottomRef = useRef(null); + + // 마지막 메세지 ref로 스크롤 이동 + useEffect(() => { + bottomRef.current?.scrollIntoView({ behavior: 'smooth' }); + }, [messages]); + + return ( +
+ + +
+ {messages.map((item) => + item.userId === myId ? ( + + ) : ( + + ), + )} +
+
+ + +
+ ); +}; + +export default ChatRoomPage; diff --git a/src/app/message/page.tsx b/src/app/message/page.tsx index 988e359a..66b5ddf7 100644 --- a/src/app/message/page.tsx +++ b/src/app/message/page.tsx @@ -7,7 +7,8 @@ import { useEffect, useState } from 'react'; import Cookies from 'js-cookie'; import { API } from '@/api'; -import { Chat, FollowingList, FollowingNone, FollowingSearch } from '@/components/pages/message'; +import { Chat } from '@/components/pages/chat'; +import { FollowingList, FollowingNone, FollowingSearch } from '@/components/pages/message'; import { TabNavigation } from '@/components/shared'; import { useInfiniteScroll } from '@/hooks/use-group/use-group-infinite-list'; import { useIntersectionObserver } from '@/hooks/use-intersection-observer'; diff --git a/src/components/pages/chat/chat-header/index.tsx b/src/components/pages/chat/chat-header/index.tsx new file mode 100644 index 00000000..186b9700 --- /dev/null +++ b/src/components/pages/chat/chat-header/index.tsx @@ -0,0 +1,20 @@ +'use client'; + +import { useRouter } from 'next/navigation'; + +import { Icon } from '@/components/icon'; + +export const ChatHeader = () => { + const router = useRouter(); + return ( +
+ router.back()} + /> + 분당 보드게임 동아리 +
+
+ ); +}; diff --git a/src/components/pages/chat/chat-input/index.tsx b/src/components/pages/chat/chat-input/index.tsx new file mode 100644 index 00000000..5a059e90 --- /dev/null +++ b/src/components/pages/chat/chat-input/index.tsx @@ -0,0 +1,54 @@ +'use client'; +import { useRef, useState } from 'react'; + +import { Icon } from '@/components/icon'; + +interface IProps { + onSubmit: (text: string) => void; +} + +export const ChatInput = ({ onSubmit }: IProps) => { + const [message, setMessage] = useState(''); + const inputRef = useRef(null); + + // Enter 키로 전송, Shift + Enter 로 줄바꿈 + const handleKeyDown = (e: React.KeyboardEvent) => { + if (e.key === 'Enter' && !e.shiftKey) { + e.preventDefault(); + handleSubmit(); + } + }; + + const handleSubmit = () => { + const changedMessage = message.trim(); + + if (!changedMessage) { + inputRef.current?.focus(); + setMessage(''); + return; + } + + onSubmit(changedMessage); + setMessage(''); + inputRef.current?.focus(); + }; + + return ( +
+
+