Skip to content

Commit c2147bb

Browse files
authored
Merge pull request #130 from ezcode-my/fix/chat
[feat] 채팅 전송시 스크롤 자동이동 , 채팅 보낸 유저 이름 추가
2 parents 4f08b8d + 92c8f97 commit c2147bb

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

next.config.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import type { NextConfig } from 'next';
33
const nextConfig: NextConfig = {
44
/* config options here */
55
reactStrictMode: false, // Strict Mode 비활성화
6-
remotePatterns: [
7-
{
8-
protocol: 'https',
9-
hostname: 'ezcode-s3.s3.ap-northeast-2.amazonaws.com',
10-
pathname: '/**',
11-
},
12-
],
6+
images: {
7+
remotePatterns: [
8+
{
9+
protocol: 'https',
10+
hostname: 'ezcode-s3.s3.ap-northeast-2.amazonaws.com',
11+
pathname: '/**',
12+
},
13+
],
14+
},
1315
webpack(config) {
1416
// Grab the existing rule that handles SVG imports
1517
const fileLoaderRule = config.module.rules.find((rule: any) => rule.test?.test?.('.svg'));

src/app/globals.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
--color-border_primary: #a4a1aa;
1414

1515
--color-gray700: #364153;
16+
--color-gray500: #888;
1617
--color-active: #1e3e2c;
1718
}
1819

src/features/chat/ui/chatMessage/ChatMessage.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@ export default function ChatMessage({ msg, userNickname }: IChatMessageProps) {
77
const formattedDate = formatDate(msg.time);
88

99
return (
10-
<li className={`flex ${isOwn ? 'justify-end' : 'justify-start'}`}>
11-
<div className={`max-w-[70%] ${isOwn ? 'order-2' : 'order-1'}`}>
12-
<div
13-
className={`px-4 py-2 rounded-[14px] ${
14-
isOwn ? 'bg-[#214d35] text-white' : 'bg-[#1a2332] text-white'
15-
}`}
16-
>
17-
<p className="whitespace-pre-wrap break-words">{msg.message}</p>
18-
</div>
19-
<p className={`text-xs text-[#888] mt-1 ${isOwn ? 'text-right' : 'text-left'}`}>
20-
{formattedDate}
21-
</p>
10+
<li className={`flex flex-col ${isOwn ? 'items-end' : 'items-start'}`}>
11+
<p className={`text-sm ${isOwn ? 'text-right' : 'text-left'}`}>{msg.name}</p>
12+
<div
13+
className={`max-w-[70%] w-fit px-4 py-2 rounded-[14px] ${isOwn ? ' bg-primary' : ' bg-secondary-background'}`}
14+
>
15+
<p className="whitespace-pre-wrap break-words">{msg.message}</p>
2216
</div>
17+
<p className={`text-xs text-gray500 mt-1 ${isOwn ? 'text-right' : 'text-left'}`}>
18+
{formattedDate}
19+
</p>
2320
</li>
2421
);
2522
}

src/features/chat/ui/chatRoom/index.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import SystemMessage from '../chatMessage/SystemMessage';
66
import ChatMessage from '../chatMessage/ChatMessage';
77
import ChatRoomHeader from './ChatRoomHeader';
88
import { useMyInfoQuery } from '@/entities/mypage/model/query';
9+
import { useEffect, useRef } from 'react';
910

1011
interface ChatProps {
1112
roomId: number;
@@ -15,6 +16,7 @@ interface ChatProps {
1516
export default function ChatRoom({ roomId, roomTitle }: ChatProps) {
1617
useJoinChatRoom(roomId);
1718
const { data } = useMyInfoQuery();
19+
const endRef = useRef<HTMLDivElement>(null);
1820
const nickname = data?.data.result.nickname;
1921
const { initMessages, realTimeMessages } = useChatWebSocketStore();
2022
// const { setIsLeaved } = useChatWebSocketActions();
@@ -23,6 +25,18 @@ export default function ChatRoom({ roomId, roomTitle }: ChatProps) {
2325
// setIsLeaved(false);
2426
// }, [roomId]);
2527

28+
useEffect(() => {
29+
if (endRef.current) {
30+
endRef.current.scrollIntoView({ behavior: 'instant' });
31+
}
32+
}, [roomId, initMessages]);
33+
34+
useEffect(() => {
35+
if (endRef.current) {
36+
endRef.current.scrollIntoView({ behavior: 'smooth' });
37+
}
38+
}, [realTimeMessages]);
39+
2640
return (
2741
<section className="w-full flex h-full flex-col justify-center flex-4/5">
2842
{!!roomId ? (
@@ -46,6 +60,7 @@ export default function ChatRoom({ roomId, roomTitle }: ChatProps) {
4660
})}
4761
</ul>
4862
)}
63+
<div ref={endRef} />
4964
</div>
5065
<ChatInput chatRoomId={roomId} />
5166
</>

0 commit comments

Comments
 (0)