(props: Props)
| 11 | } |
| 12 | |
| 13 | const MessageTextarea = (props: Props) => { |
| 14 | const { disabled, sendMessage } = props; |
| 15 | const { t } = useTranslation(); |
| 16 | const connectionStore = useConnectionStore(); |
| 17 | const conversationStore = useConversationStore(); |
| 18 | const [value, setValue] = useState<string>(""); |
| 19 | const [isInIME, setIsInIME] = useState(false); |
| 20 | const textareaRef = useRef<HTMLTextAreaElement>(null); |
| 21 | |
| 22 | useEffect(() => { |
| 23 | if (textareaRef.current) { |
| 24 | textareaRef.current.focus(); |
| 25 | } |
| 26 | }, []); |
| 27 | |
| 28 | const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => { |
| 29 | setValue(e.target.value); |
| 30 | }; |
| 31 | |
| 32 | const handleSend = async () => { |
| 33 | let conversation = conversationStore.getConversationById(conversationStore.currentConversationId); |
| 34 | if (!conversation) { |
| 35 | const currentConnectionCtx = connectionStore.currentConnectionCtx; |
| 36 | if (!currentConnectionCtx) { |
| 37 | conversation = conversationStore.createConversation(); |
| 38 | } else { |
| 39 | conversation = conversationStore.createConversation(currentConnectionCtx.connection.id, currentConnectionCtx.database?.name); |
| 40 | } |
| 41 | } |
| 42 | if (!value) { |
| 43 | toast.error("Please enter a message."); |
| 44 | return; |
| 45 | } |
| 46 | if (disabled) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | setValue(""); |
| 51 | textareaRef.current!.value = ""; |
| 52 | await sendMessage(value); |
| 53 | }; |
| 54 | |
| 55 | const handleKeyDown = (event: React.KeyboardEvent) => { |
| 56 | if (event.key === "Enter" && !event.shiftKey && !isInIME) { |
| 57 | event.preventDefault(); |
| 58 | handleSend(); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | return ( |
| 63 | <div className="w-full h-auto flex flex-row justify-between items-end border dark:border-zinc-700 rounded-lg px-2 py-1 relative shadow bg-white dark:bg-zinc-800"> |
| 64 | <TextareaAutosize |
| 65 | ref={textareaRef} |
| 66 | className="w-full h-full outline-none border-none bg-transparent leading-6 py-2 px-2 resize-none hide-scrollbar" |
| 67 | placeholder={t("editor.placeholder") || ""} |
| 68 | rows={1} |
| 69 | minRows={1} |
| 70 | maxRows={5} |
nothing calls this directly
no outgoing calls
no test coverage detected