|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import {useRef, useState, useEffect} from 'react'; |
| 4 | +import {useParams} from 'next/navigation'; |
| 5 | +import Image from 'next/image'; |
| 6 | +import {useRecoilValue} from 'recoil'; |
| 7 | +import userState from '@/src/recoil/atoms/userState'; |
| 8 | +import chatRoomAPI from '@/src/api/chatRoom'; |
| 9 | +import getUserInfo from '@/src/api/classUser/getUserInfo'; |
| 10 | +import ChatInput from '@/src/app/classes/[cId]/[mId]/components/subComponents/ChatInput'; |
| 11 | +import {User} from '@/src/interfaces/user'; |
| 12 | + |
| 13 | +interface UserInfo extends User { |
| 14 | + id: number; |
| 15 | +} |
| 16 | + |
| 17 | +const ShowMain = () => { |
| 18 | + const user = useRecoilValue(userState) as User; |
| 19 | + const [message, setMessage] = useState(''); |
| 20 | + const [messages, setMessages] = useState<string[]>([]); |
| 21 | + const [chatUsers, setChatUsers] = useState<{[key: string]: User}>({}); |
| 22 | + const params = useParams(); |
| 23 | + const getClassId = Number(params.cId); |
| 24 | + const getScheduleId = 14; |
| 25 | + const messagesEndRef = useRef<HTMLDivElement>(null); |
| 26 | + |
| 27 | + const handleGetMessages = () => { |
| 28 | + chatRoomAPI |
| 29 | + .getMessages(getScheduleId) |
| 30 | + .then(res => { |
| 31 | + if (res && 'data' in res) { |
| 32 | + const {data} = res; |
| 33 | + setMessages(data || []); |
| 34 | + } else { |
| 35 | + console.error('응답에서 data를 찾을 수 없습니다.'); |
| 36 | + setMessages([]); |
| 37 | + } |
| 38 | + }) |
| 39 | + .catch(error => { |
| 40 | + console.error('메시지를 가져오는 중 오류 발생:', error); |
| 41 | + }); |
| 42 | + }; |
| 43 | + |
| 44 | + const handleGetLiveStart = () => { |
| 45 | + const eventSource = chatRoomAPI.getLiveMessages(getScheduleId, message => { |
| 46 | + setMessages(prevMessages => [...prevMessages, message]); |
| 47 | + }); |
| 48 | + |
| 49 | + eventSource.onerror = function (event) { |
| 50 | + console.error('EventSource failed:', event); |
| 51 | + }; |
| 52 | + |
| 53 | + return eventSource; |
| 54 | + }; |
| 55 | + |
| 56 | + const handleGetUserInfo = async (uid: number) => { |
| 57 | + const userInfo = await getUserInfo(uid, getClassId); |
| 58 | + return userInfo; |
| 59 | + }; |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + if (message === '') return; |
| 63 | + chatRoomAPI |
| 64 | + .postMessage(getScheduleId, user.id.toString(), message) |
| 65 | + .then(res => { |
| 66 | + setMessage(''); |
| 67 | + console.log(res); |
| 68 | + }); |
| 69 | + }, [message]); |
| 70 | + |
| 71 | + useEffect(() => { |
| 72 | + const fetchChatUsers = async () => { |
| 73 | + const newChatUsers: {[id: string]: UserInfo} = {}; |
| 74 | + for (const msg of messages) { |
| 75 | + const [id] = msg.split(': '); |
| 76 | + if (!newChatUsers[id]) { |
| 77 | + newChatUsers[id] = await handleGetUserInfo(Number(id)); |
| 78 | + } |
| 79 | + } |
| 80 | + setChatUsers(newChatUsers); |
| 81 | + }; |
| 82 | + |
| 83 | + fetchChatUsers(); |
| 84 | + }, [messages]); |
| 85 | + |
| 86 | + useEffect(() => { |
| 87 | + if (messagesEndRef.current) { |
| 88 | + messagesEndRef.current.scrollIntoView({behavior: 'smooth'}); |
| 89 | + } |
| 90 | + }, [messages]); |
| 91 | + |
| 92 | + useEffect(() => { |
| 93 | + const eventSource = handleGetLiveStart(); |
| 94 | + return () => { |
| 95 | + eventSource.close(); |
| 96 | + }; |
| 97 | + }, [getScheduleId]); |
| 98 | + |
| 99 | + useEffect(() => { |
| 100 | + handleGetMessages(); |
| 101 | + }, [getScheduleId]); |
| 102 | + |
| 103 | + return ( |
| 104 | + <div className="flex justify-center mt-10"> |
| 105 | + <div className="w-full max-w-md"> |
| 106 | + <div className="w-full mt-2"> |
| 107 | + <div className="border-4 rounded-lg border-gray-500 p-2 h-[550px] overflow-auto"> |
| 108 | + <p className="text-center inline-block px-4 py-2 text-sm text-white bg-violet-300 rounded-lg w-full"> |
| 109 | + 최상단 채팅 내용입니다 |
| 110 | + </p> |
| 111 | + {messages.map((msg, index) => { |
| 112 | + const [id, message] = msg.split(': '); |
| 113 | + const chatUser = chatUsers[id]; |
| 114 | + if (!chatUser) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + return ( |
| 118 | + <div |
| 119 | + key={index} |
| 120 | + className={ |
| 121 | + Number(id) === user.id |
| 122 | + ? 'flex justify-end items-start' |
| 123 | + : 'flex justify-start items-start' |
| 124 | + } |
| 125 | + > |
| 126 | + {Number(id) !== user.id && ( |
| 127 | + <div className="flex items-center mt-2"> |
| 128 | + <div className="rounded-full w-8 h-8 overflow-hidden max-w-20"> |
| 129 | + <Image |
| 130 | + src={chatUser.image || user.image} |
| 131 | + alt={'userImage'} |
| 132 | + width={40} |
| 133 | + height={40} |
| 134 | + /> |
| 135 | + </div> |
| 136 | + <div className="ml-2"> |
| 137 | + <p>{chatUser.nickname || user.name}</p> |
| 138 | + <p |
| 139 | + className={ |
| 140 | + 'inline-block px-3 py-2 text-sm text-white bg-gray-500 rounded-lg max-w-72' |
| 141 | + } |
| 142 | + > |
| 143 | + {message} |
| 144 | + </p> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + )} |
| 148 | + {Number(id) === user.id && ( |
| 149 | + <div className="mt-2"> |
| 150 | + <p |
| 151 | + className={ |
| 152 | + 'inline-block px-4 py-2 text-sm text-white bg-blue-400 rounded-lg max-w-72' |
| 153 | + } |
| 154 | + > |
| 155 | + {message} |
| 156 | + </p> |
| 157 | + </div> |
| 158 | + )} |
| 159 | + </div> |
| 160 | + ); |
| 161 | + })} |
| 162 | + <div ref={messagesEndRef} /> |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + <ChatInput setMsg={setMessage} /> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + ); |
| 169 | +}; |
| 170 | + |
| 171 | +export default ShowMain; |
0 commit comments