diff --git a/src/app/recommend/page.tsx b/src/app/recommend/page.tsx index c49c438..a324aac 100644 --- a/src/app/recommend/page.tsx +++ b/src/app/recommend/page.tsx @@ -4,7 +4,7 @@ import Bg from '@/shared/assets/images/recommend_bg.webp'; function Page() { return (

취향추천하기

diff --git a/src/domains/recommend/components/ChatList.tsx b/src/domains/recommend/components/ChatList.tsx index 56eb6f3..ae2286a 100644 --- a/src/domains/recommend/components/ChatList.tsx +++ b/src/domains/recommend/components/ChatList.tsx @@ -1,3 +1,4 @@ +import { useChatScroll } from '../hook/useChatScroll'; import { ChatListProps } from '../types/recommend'; import BotMessage from './bot/BotMessage'; import NewMessageAlert from './bot/NewMessageAlert'; @@ -9,24 +10,19 @@ function ChatList({ userCurrentStep, onSelectedOption, getRecommendations, - chatListRef, - chatEndRef, - showNewMessageAlert, - handleCheckBottom, - handleScrollToBottom, isBotTyping, }: ChatListProps) { + const { chatListRef, chatEndRef, showNewMessageAlert, handleCheckBottom, handleScrollToBottom } = + useChatScroll(messages[messages.length - 1]?.id); + return (
- {messages.map((msg, idx) => { - const isLastMessage = idx === messages.length - 1; - const showTyping = isLastMessage && msg.sender === 'CHATBOT' && isBotTyping; - + {messages.map((msg) => { if (msg.sender === 'USER') { return ; } diff --git a/src/domains/recommend/components/ChatSection.tsx b/src/domains/recommend/components/ChatSection.tsx index 2f29080..6aff917 100644 --- a/src/domains/recommend/components/ChatSection.tsx +++ b/src/domains/recommend/components/ChatSection.tsx @@ -1,11 +1,7 @@ 'use client'; import { useEffect, useRef, useState } from 'react'; -import BotMessage from './bot/BotMessage'; -import UserMessage from './user/UserMessage'; -import NewMessageAlert from './bot/NewMessageAlert'; import MessageInput from './user/MessageInput'; -import { useChatScroll } from '../hook/useChatScroll'; import { fetchChatHistory, fetchGreeting, @@ -23,8 +19,7 @@ import ChatList from './ChatList'; function ChatSection() { const [messages, setMessages] = useState([]); - const { chatListRef, chatEndRef, showNewMessageAlert, handleCheckBottom, handleScrollToBottom } = - useChatScroll(messages.length); + const [userCurrentStep, setUserCurrentStep] = useState(0); const [isBotTyping, setIsBotTyping] = useState(false); @@ -34,6 +29,33 @@ function ChatSection() { selectedCocktailType?: string; }>({}); + const handleSendMessage = async (payload: stepPayload | { message: string; userId: string }) => { + const typingTimer = setTimeout(() => setIsBotTyping(true), 300); + + try { + if (!('currentStep' in payload)) { + const botMessage = await fetchSendTextMessage(payload); + clearTimeout(typingTimer); + setIsBotTyping(false); + + if (!botMessage) return; + setTimeout(() => setMessages((prev) => [...prev, botMessage]), 500); + return; + } + + const botMessage = await fetchSendStepMessage(payload); + clearTimeout(typingTimer); + setIsBotTyping(false); + + if (!botMessage) return; + setTimeout(() => setMessages((prev) => [...prev, botMessage]), 500); + } catch (err) { + clearTimeout(typingTimer); + setIsBotTyping(false); + console.error(err); + } + }; + // 일반 텍스트 보낼 시 const handleSubmitText = async (message: string) => { const userId = useAuthStore.getState().user?.id; @@ -48,8 +70,7 @@ function ChatSection() { { id: tempId, userId, message, sender: 'USER', type: 'text', createdAt: tempCreatedAt }, ]); - const botMessage = await fetchSendTextMessage({ message, userId }); - if (botMessage) setMessages((prev) => [...prev, botMessage]); + await handleSendMessage({ message, userId }); }; // 옵션 클릭 시 @@ -92,9 +113,6 @@ function ChatSection() { case 3: selectedOptions.current.selectedAlcoholBaseType = value; break; - case 4: - selectedOptions.current.selectedCocktailType = value; - break; } const payload: stepPayload = { @@ -104,22 +122,7 @@ function ChatSection() { ...selectedOptions.current, }; - const typingTimer = setTimeout(() => setIsBotTyping(true), 300); - - try { - const botMessage = await fetchSendStepMessage(payload); - - clearTimeout(typingTimer); - setIsBotTyping(false); - - if (botMessage) { - setMessages((prev) => [...prev, botMessage]); - } - } catch (err) { - clearTimeout(typingTimer); - setIsBotTyping(false); - console.error(err); - } + await handleSendMessage(payload); }; // 채팅 기록 불러오기 없으면 greeting 호출 @@ -145,18 +148,13 @@ function ChatSection() { }; return ( -
+

대화 목록 및 입력 창

diff --git a/src/domains/recommend/components/bot/NewMessageAlert.tsx b/src/domains/recommend/components/bot/NewMessageAlert.tsx index 7b49008..61dd4fe 100644 --- a/src/domains/recommend/components/bot/NewMessageAlert.tsx +++ b/src/domains/recommend/components/bot/NewMessageAlert.tsx @@ -8,8 +8,8 @@ interface Props { function NewMessageAlert({ onClick }: Props) { return (