-
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.
Merge pull request #89 from GDG-Hackathon-77ia/feature#85
ChatPage API 연동
- Loading branch information
Showing
6 changed files
with
125 additions
and
29 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
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 |
---|---|---|
@@ -1,20 +1,18 @@ | ||
import { Text } from "../Text"; | ||
import * as Styles from "./index.style"; | ||
import backArrow from "../../assets/backarrow.png" | ||
import { useNavigate } from "react-router-dom"; | ||
|
||
export interface PageBarProps { | ||
pageName: string; | ||
onClick?: () => void; // 수정: `onClick`은 매개변수를 받지 않음 | ||
} | ||
|
||
export const PageBar = ({pageName}: PageBarProps) => { | ||
const navi = useNavigate(); | ||
export const PageBar = ({ pageName, onClick }: PageBarProps) => { | ||
return ( | ||
<Styles.Container> | ||
<Styles.Arrow src={backArrow} onClick={() => {navi(-1)}}/> | ||
<Styles.Arrow onClick={onClick} /> | ||
<Text size="m" weight="bold" color="black"> | ||
{pageName} | ||
</Text> | ||
</Styles.Container> | ||
); | ||
} | ||
}; |
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,19 @@ | ||
import { saveChatHistory } from "@/apis/chatbot/chatbot"; | ||
import { ChatEntry } from "@/apis/chatbot/chatbot"; | ||
|
||
export const useChatSave = () => { | ||
const handleChatSave = async (chatHistory: ChatEntry[]) => { | ||
try { | ||
console.log(chatHistory); | ||
const response = await saveChatHistory("chat", chatHistory); | ||
console.log("Chat saved:", response.message); | ||
} catch (error) { | ||
const errorMessage = | ||
(error as { response?: { data?: { detail?: string } } }).response?.data?.detail || | ||
"Failed to save chat history."; | ||
console.error(errorMessage); | ||
} | ||
}; | ||
|
||
return { handleChatSave }; | ||
}; |
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,59 @@ | ||
import { useState, useRef } from "react"; | ||
|
||
import { startOrContinueChat } from "@/apis/chatbot/chatbot"; | ||
|
||
// interface ChatEntry { | ||
// question: string; | ||
// response: string; | ||
// responseDateTime: string; | ||
// type: "chat" | "diary"; | ||
// } | ||
|
||
export const useChatStart = () => { | ||
const [chatHistory, setChatHistory] = useState<{ variant: "AI" | "USER"; text: string }[]>([ | ||
{ variant: "AI", text: "무엇을 도와드릴까요?" }, | ||
]); | ||
const textAreaRef = useRef<HTMLTextAreaElement>(null); | ||
|
||
const handleSendMessage = async () => { | ||
const userMessage = textAreaRef.current?.value.trim(); | ||
if (!userMessage) return; | ||
|
||
const currentDateTime = new Date().toISOString(); | ||
|
||
// 사용자의 메시지 추가 | ||
setChatHistory((prevHistory) => [...prevHistory, { variant: "USER", text: userMessage }]); | ||
|
||
// AI의 응답 요청 | ||
try { | ||
const response = await startOrContinueChat("chat", [ | ||
...chatHistory.map((msg, idx) => ({ | ||
question: idx % 2 === 0 ? msg.text : "", // AI 메시지를 question으로 설정 | ||
response: idx % 2 !== 0 ? msg.text : "", // 사용자 메시지를 response로 설정 | ||
responseDateTime: currentDateTime, | ||
type: "chat" as const, | ||
})), | ||
{ | ||
question: "", | ||
response: userMessage, | ||
responseDateTime: currentDateTime, | ||
type: "chat", | ||
}, | ||
]); | ||
|
||
// AI의 응답을 chatHistory에 추가 | ||
setChatHistory((prevHistory) => [...prevHistory, { variant: "AI", text: response.response }]); | ||
} catch (error) { | ||
const errorMessage = | ||
(error as { response?: { data?: { detail?: string } } }).response?.data?.detail || | ||
"An unknown error occurred"; | ||
console.error("Chat failed:", errorMessage); | ||
} | ||
|
||
if (textAreaRef.current) { | ||
textAreaRef.current.value = ""; | ||
} | ||
}; | ||
|
||
return { chatHistory, textAreaRef, handleSendMessage }; | ||
}; |
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