|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | | -import { useEffect, useRef, useState } from 'react'; |
| 3 | +import { useState } from 'react'; |
4 | 4 | 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'; |
11 | 6 | import { useAuthStore } from '@/domains/shared/store/auth'; |
12 | 7 | import { ChatMessage, stepPayload } from '../types/recommend'; |
13 | 8 | import ChatList from './ChatList'; |
14 | 9 | import { useChatInit } from '../hook/useChatInit'; |
| 10 | +import { useSelectedOptions } from '../hook/useSelectedOptions'; |
15 | 11 |
|
16 | 12 | function ChatSection() { |
17 | 13 | const [messages, setMessages] = useState<ChatMessage[]>([]); |
18 | 14 | 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(); |
25 | 16 |
|
26 | 17 | const isInputDisabled = |
27 | 18 | selectedOptions.current.selectedSearchType !== 'QA' && userCurrentStep < 3; |
@@ -72,12 +63,10 @@ function ChatSection() { |
72 | 63 |
|
73 | 64 | const nextStep = userCurrentStep === 3 ? userCurrentStep + 1 : userCurrentStep; |
74 | 65 |
|
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 }; |
81 | 70 |
|
82 | 71 | await handleSendMessage(payload); |
83 | 72 | }; |
@@ -112,29 +101,21 @@ function ChatSection() { |
112 | 101 | }, |
113 | 102 | ]); |
114 | 103 |
|
| 104 | + // QA (질문형) 일 시 0 나머지는 +1 |
115 | 105 | const nextStep = value === 'QA' ? 0 : (stepData?.currentStep ?? 0) + 1; |
116 | 106 | setUserCurrentStep(nextStep); |
117 | 107 |
|
118 | 108 | // 0단계에서 QA 선택 시 |
119 | 109 | if (stepData.currentStep === 0 && value === 'QA') { |
120 | | - selectedOptions.current.selectedSearchType = 'QA'; |
| 110 | + setOption('selectedSearchType', 'QA'); |
121 | 111 | } |
122 | 112 |
|
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); |
131 | 114 |
|
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 }; |
138 | 119 |
|
139 | 120 | await handleSendMessage(payload); |
140 | 121 | }; |
|
0 commit comments