11'use client' ;
22
33import { useEffect , useRef , useState } from 'react' ;
4- import BotMessage from './bot/BotMessage' ;
5- import UserMessage from './user/UserMessage' ;
6- import NewMessageAlert from './bot/NewMessageAlert' ;
74import MessageInput from './user/MessageInput' ;
8- import { useChatScroll } from '../hook/useChatScroll' ;
95import {
106 fetchChatHistory ,
117 fetchGreeting ,
@@ -23,8 +19,7 @@ import ChatList from './ChatList';
2319
2420function ChatSection ( ) {
2521 const [ messages , setMessages ] = useState < ChatMessage [ ] > ( [ ] ) ;
26- const { chatListRef, chatEndRef, showNewMessageAlert, handleCheckBottom, handleScrollToBottom } =
27- useChatScroll ( messages . length ) ;
22+
2823 const [ userCurrentStep , setUserCurrentStep ] = useState ( 0 ) ;
2924 const [ isBotTyping , setIsBotTyping ] = useState ( false ) ;
3025
@@ -34,6 +29,33 @@ function ChatSection() {
3429 selectedCocktailType ?: string ;
3530 } > ( { } ) ;
3631
32+ const handleSendMessage = async ( payload : stepPayload | { message : string ; userId : string } ) => {
33+ const typingTimer = setTimeout ( ( ) => setIsBotTyping ( true ) , 300 ) ;
34+
35+ try {
36+ if ( ! ( 'currentStep' in payload ) ) {
37+ const botMessage = await fetchSendTextMessage ( payload ) ;
38+ clearTimeout ( typingTimer ) ;
39+ setIsBotTyping ( false ) ;
40+
41+ if ( ! botMessage ) return ;
42+ setTimeout ( ( ) => setMessages ( ( prev ) => [ ...prev , botMessage ] ) , 500 ) ;
43+ return ;
44+ }
45+
46+ const botMessage = await fetchSendStepMessage ( payload ) ;
47+ clearTimeout ( typingTimer ) ;
48+ setIsBotTyping ( false ) ;
49+
50+ if ( ! botMessage ) return ;
51+ setTimeout ( ( ) => setMessages ( ( prev ) => [ ...prev , botMessage ] ) , 500 ) ;
52+ } catch ( err ) {
53+ clearTimeout ( typingTimer ) ;
54+ setIsBotTyping ( false ) ;
55+ console . error ( err ) ;
56+ }
57+ } ;
58+
3759 // 일반 텍스트 보낼 시
3860 const handleSubmitText = async ( message : string ) => {
3961 const userId = useAuthStore . getState ( ) . user ?. id ;
@@ -48,8 +70,7 @@ function ChatSection() {
4870 { id : tempId , userId, message, sender : 'USER' , type : 'text' , createdAt : tempCreatedAt } ,
4971 ] ) ;
5072
51- const botMessage = await fetchSendTextMessage ( { message, userId } ) ;
52- if ( botMessage ) setMessages ( ( prev ) => [ ...prev , botMessage ] ) ;
73+ await handleSendMessage ( { message, userId } ) ;
5374 } ;
5475
5576 // 옵션 클릭 시
@@ -92,9 +113,6 @@ function ChatSection() {
92113 case 3 :
93114 selectedOptions . current . selectedAlcoholBaseType = value ;
94115 break ;
95- case 4 :
96- selectedOptions . current . selectedCocktailType = value ;
97- break ;
98116 }
99117
100118 const payload : stepPayload = {
@@ -104,22 +122,7 @@ function ChatSection() {
104122 ...selectedOptions . current ,
105123 } ;
106124
107- const typingTimer = setTimeout ( ( ) => setIsBotTyping ( true ) , 300 ) ;
108-
109- try {
110- const botMessage = await fetchSendStepMessage ( payload ) ;
111-
112- clearTimeout ( typingTimer ) ;
113- setIsBotTyping ( false ) ;
114-
115- if ( botMessage ) {
116- setMessages ( ( prev ) => [ ...prev , botMessage ] ) ;
117- }
118- } catch ( err ) {
119- clearTimeout ( typingTimer ) ;
120- setIsBotTyping ( false ) ;
121- console . error ( err ) ;
122- }
125+ await handleSendMessage ( payload ) ;
123126 } ;
124127
125128 // 채팅 기록 불러오기 없으면 greeting 호출
@@ -145,18 +148,13 @@ function ChatSection() {
145148 } ;
146149
147150 return (
148- < section className = "relative flex-1 flex flex-col w-fulloverflow-hidden " >
151+ < section className = "relative flex-1 flex flex-col items-center w-full " >
149152 < h2 className = "sr-only" > 대화 목록 및 입력 창</ h2 >
150153 < ChatList
151154 messages = { messages }
152155 userCurrentStep = { userCurrentStep }
153156 onSelectedOption = { handleSelectedOption }
154157 getRecommendations = { getRecommendations }
155- chatListRef = { chatListRef }
156- chatEndRef = { chatEndRef }
157- showNewMessageAlert = { showNewMessageAlert }
158- handleCheckBottom = { handleCheckBottom }
159- handleScrollToBottom = { handleScrollToBottom }
160158 isBotTyping = { isBotTyping }
161159 />
162160 < MessageInput onSubmit = { handleSubmitText } />
0 commit comments