All files / src/components/chat/Chat Chat.tsx

0% Statements 0/21
0% Branches 0/8
0% Functions 0/6
0% Lines 0/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                         
import React, { useState, useRef, useEffect } from "react";
import { Model, ChatMessage, modelDisplayName } from "@/objects/Model";
import ChatHistory from "../ChatHistory/ChatHistory";
import { MyButton } from "chainr-frontend-shared";
 
export interface ChatInterface {
  messages: ChatMessage[];
  model: Model;
  loading: boolean;
  handle_submit: (prompt: string) => Promise<void>;
}
 
const Chat: React.FC<ChatInterface> = (props) => {
  const textareaRef = useRef<HTMLTextAreaElement | null>(null);
  const [inputValue, setInputValue] = useState<string>("");
 
  const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
    setInputValue(e.target.value);
  };
 
  const handleClick = async () => {
    if (!inputValue.trim()) return;
    await props.handle_submit(inputValue);
    setInputValue("");
  };
 
  const handleClickWrapper = () => {
    handleClick().catch(console.error); // catch errors if you want
  };
 
  useEffect(() => {
    const textarea = textareaRef.current;
    if (textarea) {
      textarea.style.height = "auto"; // reset height
      textarea.style.height = `${textarea.scrollHeight}px`; // expand to fit content
    }
  }, [inputValue]);
 
  return (
    <div>
      <ChatHistory
        messages={props.messages}
        model_name={modelDisplayName(props.model)}
        loading={props.loading}
      />
 
      <div className="flex flex-row gap-4">
        {/* <input
          type="text"
          className="chat-input"
          placeholder="Type your message..."
          value={inputValue}
          onChange={handleChange}
          onKeyDown={(e) => {
            if (e.key === "Enter") handleClick();
          }}
        /> */}
        <textarea
          ref={textareaRef}
          className="flex-1 p-2 border border-gray-300 rounded bg-slate-200 text-black"
          placeholder="Type your message..."
          value={inputValue}
          onChange={handleChange}
          onKeyDown={(e) => {
            if (e.key === "Enter" && !e.shiftKey) {
              e.preventDefault(); // Prevent newline on Enter
              handleClickWrapper();
            }
          }}
          rows={1}
          style={{ resize: "none", overflow: "hidden" }}
        />
        <MyButton
          classes="!mb-0"
          onClick={handleClickWrapper}
          disabled={props.loading}
          name="Submit"
        />
      </div>
    </div>
  );
};
 
export default Chat;