Skip to content

Commit f5fbd34

Browse files
committed
[refactor] 채팅기록 불러오기 hook 분리
1 parent 7c5623d commit f5fbd34

File tree

4 files changed

+41
-40
lines changed

4 files changed

+41
-40
lines changed

src/domains/recommend/components/ChatList.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import { useChatScroll } from '../hook/useChatScroll';
2-
import { ChatListProps } from '../types/recommend';
2+
import { ChatListProps, RecommendationItem, StepRecommendation } from '../types/recommend';
33
import BotMessage from './bot/BotMessage';
44
import NewMessageAlert from './bot/NewMessageAlert';
55
import UserMessage from './user/UserMessage';
66

7-
function ChatList({
8-
messages,
9-
userCurrentStep,
10-
onSelectedOption,
11-
getRecommendations,
12-
}: ChatListProps) {
7+
function ChatList({ messages, userCurrentStep, onSelectedOption }: ChatListProps) {
138
const { chatListRef, chatEndRef, showNewMessageAlert, handleCheckBottom, handleScrollToBottom } =
149
useChatScroll(messages[messages.length - 1]?.id);
1510

11+
const getRecommendations = (
12+
type: string | undefined,
13+
stepData?: StepRecommendation | null
14+
): RecommendationItem[] => {
15+
if (type !== 'CARD_LIST' || !stepData?.recommendations) return [];
16+
return stepData.recommendations;
17+
};
18+
1619
return (
1720
<div
1821
ref={chatListRef}
@@ -32,6 +35,8 @@ function ChatList({
3235

3336
const isTyping = msg.type === 'TYPING';
3437

38+
const recommendations = getRecommendations(msg.type, msg.stepData);
39+
3540
return (
3641
<BotMessage
3742
key={keyId}
@@ -41,7 +46,7 @@ function ChatList({
4146
message: msg.message,
4247
type: msg.type ?? 'TEXT',
4348
options: msg.type === 'RADIO_OPTIONS' ? (msg.stepData?.options ?? []) : [],
44-
recommendations: getRecommendations(msg.type, msg.stepData),
49+
recommendations,
4550
},
4651
]}
4752
showProfile={showProfile}

src/domains/recommend/components/ChatSection.tsx

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ import {
99
fetchSendTextMessage,
1010
} from '../api/chat';
1111
import { useAuthStore } from '@/domains/shared/store/auth';
12-
import {
13-
ChatMessage,
14-
stepPayload,
15-
StepRecommendation,
16-
RecommendationItem,
17-
} from '../types/recommend';
12+
import { ChatMessage, stepPayload } from '../types/recommend';
1813
import ChatList from './ChatList';
14+
import { useChatInit } from '../hook/useChatInit';
1915

2016
function ChatSection() {
2117
const [messages, setMessages] = useState<ChatMessage[]>([]);
@@ -143,27 +139,7 @@ function ChatSection() {
143139
await handleSendMessage(payload);
144140
};
145141

146-
// 채팅 기록 불러오기 없으면 greeting 호출
147-
useEffect(() => {
148-
const loadChatHistory = async () => {
149-
const history = await fetchChatHistory();
150-
if (history && history.length > 0) {
151-
setMessages(history.sort((a, b) => Number(a.id) - Number(b.id)));
152-
} else {
153-
const greeting = await fetchGreeting('');
154-
if (greeting) setMessages([greeting]);
155-
}
156-
};
157-
loadChatHistory();
158-
}, []);
159-
160-
const getRecommendations = (
161-
type: string | undefined,
162-
stepData?: StepRecommendation | null
163-
): RecommendationItem[] => {
164-
if (type !== 'CARD_LIST' || !stepData?.recommendations) return [];
165-
return stepData.recommendations;
166-
};
142+
useChatInit(setMessages);
167143

168144
return (
169145
<section className="relative flex-1 flex flex-col items-center w-full">
@@ -172,7 +148,6 @@ function ChatSection() {
172148
messages={messages}
173149
userCurrentStep={userCurrentStep}
174150
onSelectedOption={handleSelectedOption}
175-
getRecommendations={getRecommendations}
176151
/>
177152
<MessageInput onSubmit={handleSubmitText} disabled={isInputDisabled} />
178153
</section>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use client';
2+
3+
import { useEffect } from 'react';
4+
import { ChatMessage } from '../types/recommend';
5+
import { fetchChatHistory, fetchGreeting } from '../api/chat';
6+
7+
// 채팅 기록 불러오기 없으면 greeting api 호출
8+
export function useChatInit(setMessages: React.Dispatch<React.SetStateAction<ChatMessage[]>>) {
9+
useEffect(() => {
10+
const loadChatHistory = async () => {
11+
try {
12+
const history = await fetchChatHistory();
13+
if (history && history.length > 0) {
14+
setMessages(history.sort((a, b) => Number(a.id) - Number(b.id)));
15+
} else {
16+
const greeting = await fetchGreeting('');
17+
if (greeting) setMessages([greeting]);
18+
}
19+
} catch (err) {
20+
console.error('채팅 초기화 실패:', err);
21+
}
22+
};
23+
loadChatHistory();
24+
}, [setMessages]);
25+
}

src/domains/recommend/types/recommend.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,5 @@ export interface ChatListProps {
5656
messages: ChatMessage[];
5757
userCurrentStep: number;
5858
onSelectedOption: (value: string) => void;
59-
getRecommendations: (
60-
type: string | undefined,
61-
stepData?: StepRecommendation | null
62-
) => RecommendationItem[];
6359
isBotTyping?: boolean;
6460
}

0 commit comments

Comments
 (0)