Skip to content

Commit bd35723

Browse files
committed
[refactor] selectedOptions hook 분리
1 parent f5fbd34 commit bd35723

File tree

2 files changed

+48
-34
lines changed

2 files changed

+48
-34
lines changed

src/domains/recommend/components/ChatSection.tsx

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
'use client';
22

3-
import { useEffect, useRef, useState } from 'react';
3+
import { useState } from 'react';
44
import MessageInput from './user/MessageInput';
5-
import {
6-
fetchChatHistory,
7-
fetchGreeting,
8-
fetchSendStepMessage,
9-
fetchSendTextMessage,
10-
} from '../api/chat';
5+
import { fetchSendStepMessage, fetchSendTextMessage } from '../api/chat';
116
import { useAuthStore } from '@/domains/shared/store/auth';
127
import { ChatMessage, stepPayload } from '../types/recommend';
138
import ChatList from './ChatList';
149
import { useChatInit } from '../hook/useChatInit';
10+
import { useSelectedOptions } from '../hook/useSelectedOptions';
1511

1612
function ChatSection() {
1713
const [messages, setMessages] = useState<ChatMessage[]>([]);
1814
const [userCurrentStep, setUserCurrentStep] = useState(0);
19-
const selectedOptions = useRef<{
20-
selectedSearchType?: string;
21-
selectedAlcoholStrength?: string;
22-
selectedAlcoholBaseType?: string;
23-
selectedCocktailType?: string;
24-
}>({});
15+
const { selectedOptions, setOption, setStepOption } = useSelectedOptions();
2516

2617
const isInputDisabled =
2718
selectedOptions.current.selectedSearchType !== 'QA' && userCurrentStep < 3;
@@ -72,12 +63,10 @@ function ChatSection() {
7263

7364
const nextStep = userCurrentStep === 3 ? userCurrentStep + 1 : userCurrentStep;
7465

75-
const payload: stepPayload = {
76-
currentStep: nextStep,
77-
message,
78-
userId,
79-
...selectedOptions.current,
80-
};
66+
const payload: stepPayload =
67+
nextStep === 0
68+
? { message, userId, currentStep: nextStep }
69+
: { message, userId, currentStep: nextStep, ...selectedOptions.current };
8170

8271
await handleSendMessage(payload);
8372
};
@@ -112,29 +101,21 @@ function ChatSection() {
112101
},
113102
]);
114103

104+
// QA (질문형) 일 시 0 나머지는 +1
115105
const nextStep = value === 'QA' ? 0 : (stepData?.currentStep ?? 0) + 1;
116106
setUserCurrentStep(nextStep);
117107

118108
// 0단계에서 QA 선택 시
119109
if (stepData.currentStep === 0 && value === 'QA') {
120-
selectedOptions.current.selectedSearchType = 'QA';
110+
setOption('selectedSearchType', 'QA');
121111
}
122112

123-
switch (stepData.currentStep + 1) {
124-
case 2:
125-
selectedOptions.current.selectedAlcoholStrength = value;
126-
break;
127-
case 3:
128-
selectedOptions.current.selectedAlcoholBaseType = value;
129-
break;
130-
}
113+
setStepOption(stepData.currentStep + 1, value);
131114

132-
const payload: stepPayload = {
133-
message: selectedLabel,
134-
userId,
135-
currentStep: nextStep,
136-
...selectedOptions.current,
137-
};
115+
const payload: stepPayload =
116+
nextStep === 0
117+
? { message: selectedLabel, userId, currentStep: nextStep }
118+
: { message: selectedLabel, userId, currentStep: nextStep, ...selectedOptions.current };
138119

139120
await handleSendMessage(payload);
140121
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { useRef } from 'react';
2+
3+
type SelectedOptions = {
4+
selectedSearchType?: string;
5+
selectedAlcoholStrength?: string;
6+
selectedAlcoholBaseType?: string;
7+
selectedCocktailType?: string;
8+
};
9+
10+
export function useSelectedOptions() {
11+
const selectedOptions = useRef<SelectedOptions>({});
12+
13+
const setOption = (key: keyof SelectedOptions, value: string) => {
14+
selectedOptions.current[key] = value;
15+
};
16+
17+
const setStepOption = (step: number, value: string) => {
18+
if (step === 2) selectedOptions.current.selectedAlcoholStrength = value;
19+
if (step === 3) selectedOptions.current.selectedAlcoholBaseType = value;
20+
// if (step === 4) selectedOptions.current.selectedCocktailType = value;
21+
};
22+
23+
const reset = () => {
24+
selectedOptions.current = {};
25+
};
26+
27+
return {
28+
selectedOptions,
29+
setOption,
30+
setStepOption,
31+
reset,
32+
};
33+
}

0 commit comments

Comments
 (0)