-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpage.tsx
109 lines (100 loc) · 3.29 KB
/
page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use client'
import React, { useRef, useEffect, useState } from 'react';
import '@/styles/global.css';
import CommonLayout from './commonLayout';
import { ChatMessages, ChatInput } from '@/components/chatComponents';
import { useSession } from '@/hooks/sessionHook';
interface ChatResponse {
message: any;
}
function App() {
const { sessionId, messages, setMessages, resetSession } = useSession();
const messagesEndRef = useRef<HTMLDivElement>(null);
const inputFieldRef = useRef<HTMLInputElement>(null);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
if (inputFieldRef.current) {
inputFieldRef.current.focus();
}
}, []);
const scrollToBottom = () => {
setTimeout(() => {
if (messagesEndRef.current) {
messagesEndRef.current.scrollIntoView({ behavior: "smooth", block: "end" });
}
}, 100);
};
const sendMessage = async (message: string) => {
try {
console.log('Sending message:', process.env.REACT_APP_RESTAPI_ENDPOINT);
const response = await fetch(`/api/coffeechat`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Session-Id': sessionId
},
body: JSON.stringify({message})
});
const data = await response.json();
console.log('Response:', data);
return data;
} catch (error) {
console.error('Error', error);
}
};
const handleSendMessage = (input: string) => {
const newMessage = {
role: "user",
content: [
{
text: input
}
]
};
setMessages([...messages, newMessage]);
scrollToBottom();
setIsLoading(true);
sendMessage(input).then((response:ChatResponse ) => {
setMessages([...messages, newMessage, response.message]);
setIsLoading(false);
scrollToBottom();
});
};
return (
<CommonLayout
title="Coffee Chatbot"
navLink={{ text: "View Telemetry", href: "/s3" }}
mainClassName="h-screen overflow-hidden"
>
<div className="flex flex-col h-full">
<div className="flex justify-end mb-4">
<button
onClick={resetSession}
title="Clear chat history"
className="inline-flex items-center px-3 py-1 text-sm text-gray-600 hover:text-red-600
border border-gray-300 hover:border-red-300 rounded-md transition-colors
bg-white"
>
<svg className="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
Clear Chat
</button>
</div>
<div className="flex-1 bg-white rounded-lg shadow-sm overflow-hidden flex flex-col">
<div className="flex-1 overflow-y-auto">
<ChatMessages messages={messages} isLoading={isLoading} />
<div ref={messagesEndRef} />
</div>
<ChatInput
onSendMessage={handleSendMessage}
ref={inputFieldRef}
autoFocus={true}
className="focus-within:ring-2 focus-within:ring-blue-500 focus-within:border-transparent"
/>
</div>
</div>
</CommonLayout>
);
}
export default App;