Skip to content

Commit dffb947

Browse files
committed
Adjustments for new API
1 parent 49ac1c1 commit dffb947

File tree

6 files changed

+72
-65
lines changed

6 files changed

+72
-65
lines changed

src/App.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useState } from "react";
2-
import ChatBody from "./components/ChatBody";
3-
import ChatInput from "./components/ChatInput";
42
import { useMutation } from "react-query";
53
import { fetchResponse } from "./api";
4+
import ChatBody from "./components/ChatBody";
5+
import ChatInput from "./components/ChatInput";
66
function App() {
77
const [chat, setChat] = useState([]);
88

@@ -13,7 +13,7 @@ function App() {
1313
onSuccess: (data) =>
1414
setChat((prev) => [
1515
...prev,
16-
{ sender: "ai", message: data.message.replace(/^\n\n/, "") },
16+
{ sender: "ai", message: data.text.replace(/^\n\n/, "") },
1717
]),
1818
});
1919

src/api.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
export const fetchResponse = async(chat) => {
2-
try {
3-
// after depoloyment you should change the fetch URL below
4-
const response = await fetch('https://localhost:3000', {
5-
method: 'POST',
6-
headers: {
7-
"Content-Type": "application/json"
8-
},
9-
body: JSON.stringify({
10-
message: chat.map((message)=> message.message).join(" \n ")
11-
})
12-
})
1+
let conversationId;
2+
let lastMessageId;
133

14-
const data = await response.json()
15-
return data
16-
} catch (error) {
17-
console.log(error);
18-
}
19-
}
4+
export const fetchResponse = async chat => {
5+
try {
6+
// after depoloyment you should change the fetch URL below
7+
const response = await fetch('http://localhost:3080', {
8+
method: 'POST',
9+
headers: {
10+
'Content-Type': 'application/json'
11+
},
12+
body: JSON.stringify({
13+
message: chat[chat.length - 1].message,
14+
conversationId,
15+
lastMessageId
16+
})
17+
});
18+
19+
const data = await response.json();
20+
conversationId = data.conversationId;
21+
lastMessageId = data.id;
22+
return data;
23+
} catch (error) {
24+
console.log(error);
25+
}
26+
};

src/components/ChatBody.jsx

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
1-
import React, { useRef } from "react";
2-
import autoAnimate from "@formkit/auto-animate";
3-
import { useEffect } from "react";
4-
1+
import autoAnimate from '@formkit/auto-animate';
2+
import React, { useEffect, useRef } from 'react';
53

64
const ChatBody = ({ chat }) => {
7-
const aiStyle =
8-
"bg-white bg-opacity-40 backdrop-blur-lg dropshadow-md mr-auto";
9-
10-
const parent = useRef(null);
11-
const bottomRef = useRef(null);
12-
13-
// only for aut animations
14-
useEffect(()=>{
15-
parent.current && autoAnimate(parent.current);
16-
}, [parent])
17-
18-
//for scrolling bottom
19-
useEffect(()=>{
20-
bottomRef.current?.scrollIntoView({behavior: "smooth"})
21-
}, [chat])
22-
23-
return (
24-
<div className="flex flex-col gap-4" ref={parent}>
25-
{chat.map((message, i) => {
26-
return (
27-
<div
28-
key={i}
29-
className={`border-[#999999] break-words border-2 rounded-xl self-end px-3 py-3 max-w-[80%] ${
30-
message.sender === "ai" && aiStyle
31-
}`}
32-
>
33-
<pre className="whitespace-pre-wrap">
34-
<span>{message.message}</span>
35-
</pre>
36-
</div>
37-
);
38-
})}
39-
40-
<div ref={bottomRef} className="h-3"></div>
41-
</div>
42-
);
5+
const aiStyle = 'bg-white bg-opacity-40 backdrop-blur-lg dropshadow-md mr-auto';
6+
7+
const parent = useRef(null);
8+
const bottomRef = useRef(null);
9+
10+
// only for aut animations
11+
useEffect(() => {
12+
parent.current && autoAnimate(parent.current);
13+
}, [parent]);
14+
15+
//for scrolling bottom
16+
useEffect(() => {
17+
bottomRef.current?.scrollIntoView({ behavior: 'smooth' });
18+
}, [chat]);
19+
20+
return (
21+
<div
22+
className="flex flex-col gap-4"
23+
ref={parent}
24+
>
25+
{chat.map((message, i) => {
26+
return (
27+
<div
28+
key={i}
29+
className={`border-[#999999] break-words border-2 rounded-xl self-end px-3 py-3 max-w-[80%] ${message.sender === 'ai' && aiStyle}`}
30+
>
31+
<pre className="whitespace-pre-wrap">
32+
<span>{message.message}</span>
33+
</pre>
34+
</div>
35+
);
36+
})}
37+
38+
<div
39+
ref={bottomRef}
40+
className="h-3"
41+
></div>
42+
</div>
43+
);
4344
};
4445

4546
export default ChatBody;

src/components/ChatInput.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import React from "react";
2-
import { useState } from "react";
1+
import React, { useState } from "react";
32

43
const ChatInput = ({ sendMessage, loading }) => {
54
const [value, setValue] = useState("");

src/main.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import ReactDOM from "react-dom/client";
3+
import { QueryClient, QueryClientProvider } from "react-query";
34
import App from "./App";
4-
import { QueryClientProvider, QueryClient } from "react-query";
55
import "./index.css";
66

77
const queryClient = new QueryClient()

vite.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { defineConfig } from 'vite'
21
import react from '@vitejs/plugin-react'
2+
import { defineConfig } from 'vite'
33

44
// https://vitejs.dev/config/
55
export default defineConfig({

0 commit comments

Comments
 (0)