2023-02-05 15:29:35 -05:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
|
import { SSE } from '../../app/sse';
|
|
|
|
|
|
|
|
|
|
const handleSubmit = (payload) => {
|
|
|
|
|
const events = new SSE('http://localhost:3050/ask', {
|
|
|
|
|
payload: JSON.stringify({ text: payload }),
|
|
|
|
|
headers: { 'Content-Type': 'application/json' }
|
|
|
|
|
});
|
|
|
|
|
console.log('we in handleSubmit');
|
|
|
|
|
events.onopen = function () {
|
|
|
|
|
console.log('connection is opened');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
events.onmessage = function (e) {
|
|
|
|
|
console.log(e);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
events.onerror = function (e) {
|
|
|
|
|
console.log(e, 'error in opening conn.');
|
|
|
|
|
events.close();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
events.stream();
|
|
|
|
|
};
|
2023-02-04 20:48:33 -05:00
|
|
|
|
|
|
|
|
export default function TextChat() {
|
2023-02-05 15:29:35 -05:00
|
|
|
const [text, setText] = useState('');
|
|
|
|
|
|
2023-02-04 20:48:33 -05:00
|
|
|
const handleKeyPress = (e) => {
|
|
|
|
|
if (e.key === 'Enter' && e.shiftKey) {
|
|
|
|
|
console.log('Enter + Shift');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
|
|
|
|
console.log('Submit Enter');
|
2023-02-05 15:29:35 -05:00
|
|
|
handleSubmit(text);
|
2023-02-04 20:48:33 -05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<textarea
|
|
|
|
|
className="m-10 h-16 p-4"
|
|
|
|
|
onKeyUp={handleKeyPress}
|
2023-02-05 15:29:35 -05:00
|
|
|
onChange={(e) => setText(e.target.value)}
|
2023-02-04 20:48:33 -05:00
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|