-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#79) ๐ refactor: ์๊ฐ ์๋๋ก ์ฑ ์ปดํฌ๋ํธ ์ถ๋ ฅ๋๋๋ก ๋ฆฌํฉํ ๋ง
- Loading branch information
Showing
5 changed files
with
433 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,287 @@ | ||
import React, { useState, useEffect, useRef } from "react"; | ||
import BackLogoBar from "../../components/layout/BackLogoBar"; | ||
import { stadiumList } from "../../constants/ZoneData"; | ||
import StadiumSelection from "./components/stadiumcategory/StadiumSelection"; | ||
import ChatbotInputField from "./components/input/ChatbotInputField"; | ||
|
||
import DateBanner from "./components/DateBanner"; | ||
|
||
import { questionCategories, GuideResponseData } from "@/src/constants/ChatbotData"; | ||
import { GuideGetResponseType } from "@/src/api/ChatbotApiType"; | ||
|
||
import RookieChat from "./components/chat/RookieChat"; | ||
import UserChat from "./components/chat/UserChat"; | ||
import CategoryChat from "./components/chat/CategoryChat"; | ||
|
||
import RookieImageMessage from "./components/message/custom/RookieImageMessage"; | ||
import chatbotManualIcon from "@/src/assets/webp/chatbot_manual.webp"; | ||
|
||
import Chatting from "@/src/pages/Chatbot/components/Chatting"; | ||
|
||
const Chatbot = () => { | ||
// ์คํ๋์ ์ ํ ๊ด๋ จ | ||
const [selectedStadium, setSelectedStadium] = useState<string | null>(null); // ์ ํํ ์คํ๋์ ์ ์ฅ | ||
const isStadiumSelected = selectedStadium !== null && selectedStadium !== ""; // ์คํ๋์ ์ ํ ์ฌ๋ถ | ||
const [showInitialMessages, setShowInitialMessages] = useState(false); // ์ด๊ธฐ ๋ฉ์์ง ์ถ๋ ฅ ์ฌ๋ถ | ||
const [isLoading, setIsLoading] = useState(true); // ๋ก๋ฉ ์ํ ์ถ๊ฐ // ์ด๊ธฐ์๋ ์๋ ์คํฌ๋กค ์คํ๋์ง ์๋๋ก ํ๊ธฐ ์ํจ | ||
|
||
const handleStadiumSelect = (stadium: string) => { | ||
setSelectedStadium(stadium); | ||
setIsLoading(false); // ๋ก๋ฉ์ด ๋๋๋ฉด isLoading์ false๋ก ์ค์ | ||
}; | ||
|
||
|
||
// ์ฑ๋ด ์ฒซ ์ธ์ฌ ๋ ๋๋ง ๊ด๋ จ | ||
useEffect(() => { | ||
// ์ฑ๋ด ํ์ด์ง ๋ค์ด์จ ํ ์ด๊ธฐ ๋ฉ์์ง ํ์ | ||
setShowInitialMessages(true); | ||
}, []); | ||
|
||
|
||
// ์นดํ ๊ณ ๋ฆฌ ์ ํ ๊ด๋ จ (๋ฐฐ์ด๋ก ์ ์ฅํด์ผ ํ๋ก ํธ์์ ๊ด๋ฆฌ ๋ฐ ๊ณ์ ๋ํ ์์ฑ ๊ฐ๋ฅ) | ||
const [selectedCategories, setSelectedCategories] = useState<string[]>([]); | ||
const MAX_CATEGORIES = 15; // ์นดํ ๊ณ ๋ฆฌ ์ต๋ ๊ฐ์ ์ง์ | ||
const handleCategorySelect = (category: string) => { | ||
setSelectedCategories((prevCategories) => { | ||
const updatedCategories = [...prevCategories, category]; | ||
|
||
// ๋ฐฐ์ด์ด ์ต๋ ๊ฐ์๋ฅผ ๋์ผ๋ฉด ๊ฐ์ฅ ์ค๋๋ ํญ๋ชฉ ์ ๊ฑฐ ํ ์๋ก์ด ํญ๋ชฉ ์ถ๊ฐ | ||
if (updatedCategories.length > MAX_CATEGORIES) { | ||
updatedCategories.shift(); // ๊ฐ์ฅ ์ค๋๋ ํญ๋ชฉ ์ ๊ฑฐ | ||
} | ||
return updatedCategories; | ||
}); | ||
}; | ||
|
||
// ๊ฐ์ด๋ ์ฑ๋ด ๋ต๋ณ ๊ด๋ จ | ||
const [responseGuideDataList, setResponseGuideDataList] = useState<GuideResponseData[]>([]); // ์ธ๋ถ ์นดํ ๊ณ ๋ฆฌ index์ ๋งคํํ์ฌ API ์๋ต ์ ์ฅ | ||
|
||
// Guide API ์๋ต์ category index์ ๋งคํํ์ฌ ์ ์ฅ | ||
// ์ฑํ ์ฐฝ์ ๋ ๋๋งํ๊ฒ ์ํ ์ฉ๋ | ||
const handleGuideResponseUpdate = (answer: string, imgUrl: string, linkName: string, link: string, categoryKey: number, categoryName: string, subCategoryKey: number, subCategoryName: string) => { | ||
setResponseGuideDataList((prev) => [ | ||
...prev, | ||
|
||
{ | ||
answer: answer, | ||
imgUrl: imgUrl, | ||
linkName: linkName, | ||
link: link, | ||
categoryNumber: categoryKey, | ||
categoryName: categoryName, | ||
subcategoryNumber: subCategoryKey, | ||
subCategoryName: subCategoryName, | ||
}, | ||
]); | ||
}; | ||
|
||
|
||
// ํด๋ก๋ฐ ์ฑ๋ด ๋ต๋ณ ๊ด๋ จ | ||
const [responseClovaDataList, setResponseClovaDataList] = useState<string[]>([]); // ์ธ๋ถ ์นดํ ๊ณ ๋ฆฌ index์ ๋งคํํ์ฌ API ์๋ต ์ ์ฅ | ||
|
||
// Clova API ์๋ต์ category index์ ๋งคํํ์ฌ ์ ์ฅ | ||
// ์ฑํ ์ฐฝ์ ๋ ๋๋งํ๊ฒ ์ํ ์ฉ๋ | ||
const handleClovaResponseUpdate = (answer: string) => { | ||
setResponseClovaDataList((prev) => [ | ||
...prev, | ||
{ | ||
answer: answer, | ||
}, | ||
]); | ||
}; | ||
|
||
|
||
// ๊ฐ์ด๋ ๋ต๋ณ ๋ ๋๋ง | ||
const renderGuideAnswerData = (response: GuideGetResponseType) => { | ||
const answerImageUrl = response.imgUrl; | ||
const answerString = response.answer; | ||
const answerLinkName = response.linkName; | ||
const answerLink = response.link; | ||
|
||
const answerListWithImg = [ | ||
{ type: "imgUrl", content: answerImageUrl }, | ||
{ type: "preformattedText", content: answerString } | ||
]; | ||
const answerList = [ | ||
{ type: "preformattedTextWithTail", content: answerString }, | ||
]; | ||
const answerListWithBtn = [ | ||
{ type: "preformattedTextButtonWithTail", content: answerString, buttonContent: answerLinkName, url: answerLink }, | ||
]; | ||
|
||
return ( | ||
<> | ||
{answerImageUrl ? | ||
// ์ด๋ฏธ์ง, ๋ต๋ณ ์ถ๋ ฅ | ||
<RookieChat | ||
contentList={answerListWithImg} | ||
/> | ||
: answerLink ? | ||
// ์ด๋ฏธ์ง, ๋ต๋ณ, ๋งํฌ๋ก ์ด๋ํ๋ ๋ฒํผ ์ถ๋ ฅ | ||
<RookieChat | ||
contentList={answerListWithBtn} | ||
/> | ||
: | ||
// ๋ต๋ณ ์ถ๋ ฅ | ||
<RookieChat | ||
contentList={answerList} | ||
/> | ||
} | ||
</> | ||
); | ||
} | ||
|
||
|
||
// ์คํฌ๋กค์ ์กฐ์ํ ์์ญ(์ฑํ ์ฐฝ div) ์ง์ | ||
const chatContainerRef = useRef<HTMLDivElement>(null); | ||
|
||
// ์๋ ์คํฌ๋กค ๊ธฐ๋ฅ | ||
// ์ฑํ ์ด ์ถ๊ฐ๋ ๋ ์คํฌ๋กค ๋งจ ์๋๋ก ์ด๋ | ||
const scrollToBottom = () => { | ||
// ์ฑํ ์์ญ์ ๋์ด์ ์ ์ฒด ํ์ด์ง๋ฅผ ๋งจ ์๋๋ก ์คํฌ๋กค | ||
window.scrollTo({ | ||
top: document.documentElement.scrollHeight, | ||
behavior: 'smooth', // ๋ถ๋๋ฌ์ด ์คํฌ๋กค | ||
}); | ||
|
||
// ์ฑํ ์์ญ ๋งจ ์๋๋ก ์คํฌ๋กค | ||
if (chatContainerRef.current) { | ||
chatContainerRef.current.scrollTop = chatContainerRef.current.scrollHeight; | ||
} | ||
}; | ||
useEffect(() => { | ||
if (!isLoading) { | ||
scrollToBottom(); | ||
} | ||
}, [selectedStadium, selectedCategories, responseGuideDataList]); | ||
|
||
|
||
|
||
// ์ฒ์์ ๋ณด์ฌ์ค ์ปจํ ์ธ | ||
const renderInitialMessage = () => { | ||
return ( | ||
<> | ||
{/* ๋ฃจํค ์ฌ์ฉ ์ค๋ช ์ */} | ||
<RookieImageMessage imgIcon={chatbotManualIcon} /> | ||
|
||
{/* ์คํ๋์ ์ ํ์ฐฝ */} | ||
<StadiumSelection stadiums={stadiumList} onSelect={handleStadiumSelect} /> | ||
</> | ||
); | ||
} | ||
|
||
|
||
return ( | ||
<div> | ||
<div> | ||
{/* 1. ํค๋๋ฐ */} | ||
<BackLogoBar /> | ||
</div> | ||
|
||
<div className="flex justify-center items-center h-full min-h-screen bg-grayscale-10 mt-[55px] "> | ||
<div className="flex flex-col h-full max-w-[500px] w-full bg-grayscale-10"> | ||
{/* ์ฑํ ์์ญ */} | ||
<div | ||
ref={chatContainerRef} | ||
className="flex-1 px-3 py-4 h-full overflow-y-auto mb-10" | ||
> | ||
<Chatting/> | ||
|
||
{/* 2. ์ค๋ ๋ ์ง */} | ||
<DateBanner date={new Date()} /> | ||
|
||
{/* 3. ์ฑํ ๋ด์ญ */} | ||
<div className="px-1"> | ||
|
||
{/* ์ฑํ 1: ๊ตฌ์ฅ ์ ํ, ๋ฃจํค ์์ ์ธ์ฌ๋ง, ํ์ ์ถ๋ ฅ */} | ||
{showInitialMessages && ( | ||
<RookieChat | ||
contentList={[ | ||
{ | ||
type: "textListWithTail", | ||
content: questionCategories.greetings | ||
}, | ||
{ | ||
type: "component", | ||
content: renderInitialMessage() | ||
} | ||
]} | ||
/> | ||
)} | ||
|
||
|
||
|
||
{/* ์ผ๊ตฌ์ฅ ์ ํ์ */} | ||
{selectedStadium && ( | ||
<> | ||
{/* ์ฑํ 2: ์ฌ์ฉ์ ๋ต๋ณ, ํ์ ์ถ๋ ฅ */} | ||
<UserChat messageList={[selectedStadium]}/> | ||
|
||
{/* ์ฑํ 3: */} | ||
{/* ์ฒซ ๋ฒ์งธ ๋ด์ฉ๋ฌผ์ ๊ผฌ๋์ง ๋งํ์ ์ ์ถ๋ ฅ */} | ||
{/* ๋ ๋ฒ์งธ ๋ด์ฉ๋ฌผ์ ์ผ๋ฐ ๋งํ์ ์ ์ถ๋ ฅ */} | ||
<RookieChat | ||
contentList={[ | ||
{ | ||
type: "textListWithTail", | ||
content: [`'${selectedStadium}'์(๋ฅผ) ์ ํํ์ จ๊ตฐ์!๐`] | ||
}, | ||
|
||
{ | ||
type: "textList", | ||
content: questionCategories.baseballCategories.userMessage | ||
} | ||
]} | ||
/> | ||
</> | ||
)} | ||
|
||
{/* ์นดํ ๊ณ ๋ฆฌ ์ ํ์ ๋ฐฐ์ด์ ์ ์ฅ ๋ฐ ์์ฐจ ์ถ๋ ฅ */} | ||
{selectedStadium && selectedCategories.map((categoryFrontName, index) => ( | ||
<div key={index}> | ||
<> | ||
{/* ์ฌ์ฉ์ ๋ต๋ณ ์ถ๋ ฅ */} | ||
<UserChat messageList={[categoryFrontName]}/> | ||
|
||
{/* ์ ํ๋ ์นดํ ๊ณ ๋ฆฌ์ ๋ํ ์ฑ๋ด ์๋ต ์ถ๋ ฅ */} | ||
<RookieChat | ||
contentList={[ | ||
{ | ||
type: "component", | ||
content: <CategoryChat stadiumName={selectedStadium} categoryKey={index} categoryFrontName={categoryFrontName} onGuideResponseUpdate={handleGuideResponseUpdate} /> | ||
} | ||
]} | ||
/> | ||
|
||
|
||
{/* ์๋ธ ์นดํ ๊ณ ๋ฆฌ ์ ํ์ ์์ฐจ ์ถ๋ ฅ */} | ||
{/* Guide API ๋ต๋ณ ์ถ๋ ฅ: ํด๋น ์นดํ ๊ณ ๋ฆฌ์๋ง ๋งคํ๋๋ ๋ฐ์ดํฐ๋ฅผ ํํฐ๋งํ์ฌ ์ถ๋ ฅ */} | ||
{responseGuideDataList | ||
.filter((responseGuideData) => responseGuideData.categoryNumber === index) // ํ์ฌ ์นดํ ๊ณ ๋ฆฌ์ ํด๋นํ๋ ๋ฐ์ดํฐ๋ง ํํฐ๋ง | ||
.map((responseGuideData, responseIndex) => ( | ||
<div key={responseIndex}> | ||
<UserChat messageList={[responseGuideData.categoryName + " โถ๏ธ " + responseGuideData.subCategoryName]}/> | ||
|
||
{/* Guide API ๋ต๋ณ ์ถ๋ ฅ: ํด๋น ์นดํ ๊ณ ๋ฆฌ์๋ง ๋งคํ๋๋ ๋ฐ์ดํฐ๋ฅผ ํํฐ๋งํ์ฌ ์ถ๋ ฅ */} | ||
<div className="py-2"> | ||
{renderGuideAnswerData(responseGuideData)} | ||
</div> | ||
</div> | ||
))} | ||
</> | ||
</div> | ||
))} | ||
|
||
</div> | ||
</div> | ||
|
||
|
||
{/* 4. ์ฑํ ์ ๋ ฅ์ฐฝ */} | ||
<ChatbotInputField isStadiumSelected={isStadiumSelected} onSelect={handleCategorySelect} onClovaResponseUpdate={handleClovaResponseUpdate}/> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Chatbot; |
Oops, something went wrong.