2023-06-15 12:52:46 +03:00
|
|
|
/* eslint-disable */
|
2023-06-11 00:59:16 +03:00
|
|
|
"use client";
|
2023-06-13 17:33:41 +03:00
|
|
|
import Button from "@/lib/components/ui/Button";
|
2023-06-15 12:52:46 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
import { useChat } from "@/app/chat/[chatId]/hooks/useChat";
|
|
|
|
import { useState } from "react";
|
2023-07-03 18:39:59 +03:00
|
|
|
import { ConfigButton } from "./components/ConfigButton";
|
2023-07-05 14:33:26 +03:00
|
|
|
import { MicButton } from "./components/MicButton/MicButton";
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-15 12:52:46 +03:00
|
|
|
export const ChatInput = (): JSX.Element => {
|
2023-06-22 18:50:06 +03:00
|
|
|
const [message, setMessage] = useState<string>(""); // for optimistic updates
|
|
|
|
const { addQuestion, generatingAnswer } = useChat();
|
2023-06-15 12:52:46 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
const submitQuestion = () => {
|
2023-07-03 18:39:59 +03:00
|
|
|
if (message.length === 0) return;
|
|
|
|
if (!generatingAnswer) {
|
|
|
|
addQuestion(message, () => setMessage(""));
|
|
|
|
}
|
2023-06-22 18:50:06 +03:00
|
|
|
};
|
2023-07-03 18:39:59 +03:00
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
return (
|
|
|
|
<form
|
2023-07-03 18:39:59 +03:00
|
|
|
data-testid="chat-input-form"
|
2023-06-11 00:59:16 +03:00
|
|
|
onSubmit={(e) => {
|
|
|
|
e.preventDefault();
|
2023-07-03 18:39:59 +03:00
|
|
|
submitQuestion();
|
2023-06-11 00:59:16 +03:00
|
|
|
}}
|
2023-06-11 11:44:23 +03:00
|
|
|
className="sticky bottom-0 p-5 bg-white dark:bg-black rounded-t-md border border-black/10 dark:border-white/25 border-b-0 w-full max-w-3xl flex items-center justify-center gap-2 z-20"
|
2023-06-11 00:59:16 +03:00
|
|
|
>
|
|
|
|
<textarea
|
|
|
|
autoFocus
|
2023-06-22 18:50:06 +03:00
|
|
|
value={message}
|
|
|
|
required
|
|
|
|
onChange={(e) => setMessage(e.target.value)}
|
2023-06-11 00:59:16 +03:00
|
|
|
onKeyDown={(e) => {
|
|
|
|
if (e.key === "Enter" && !e.shiftKey) {
|
|
|
|
e.preventDefault(); // Prevents the newline from being entered in the textarea
|
2023-07-03 18:39:59 +03:00
|
|
|
submitQuestion();
|
2023-06-11 00:59:16 +03:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
className="w-full p-2 border border-gray-300 dark:border-gray-500 outline-none rounded dark:bg-gray-800"
|
|
|
|
placeholder="Begin conversation here..."
|
2023-07-03 18:39:59 +03:00
|
|
|
data-testid="chat-input"
|
2023-06-11 00:59:16 +03:00
|
|
|
/>
|
2023-06-14 23:58:37 +03:00
|
|
|
<Button
|
|
|
|
className="px-3 py-2 sm:px-4 sm:py-2"
|
|
|
|
type="submit"
|
2023-06-22 18:50:06 +03:00
|
|
|
isLoading={generatingAnswer}
|
2023-07-03 18:39:59 +03:00
|
|
|
data-testid="submit-button"
|
2023-06-14 23:58:37 +03:00
|
|
|
>
|
2023-06-22 18:50:06 +03:00
|
|
|
{generatingAnswer ? "Thinking..." : "Chat"}
|
2023-06-11 00:59:16 +03:00
|
|
|
</Button>
|
2023-06-14 23:58:37 +03:00
|
|
|
<div className="flex items-center">
|
2023-06-22 18:50:06 +03:00
|
|
|
<MicButton setMessage={setMessage} />
|
2023-06-14 23:58:37 +03:00
|
|
|
<ConfigButton />
|
|
|
|
</div>
|
2023-06-11 00:59:16 +03:00
|
|
|
</form>
|
|
|
|
);
|
2023-06-15 12:52:46 +03:00
|
|
|
};
|