import { useForm } from "@mantine/form" import { useMutation, useQueryClient } from "@tanstack/react-query" import React from "react" import useDynamicTextareaSize from "~hooks/useDynamicTextareaSize" import PhotoIcon from "@heroicons/react/24/outline/PhotoIcon" import XMarkIcon from "@heroicons/react/24/outline/XMarkIcon" import { toBase64 } from "~libs/to-base64" import { useMessageOption } from "~hooks/useMessageOption" import { Tooltip } from "antd" import { MicIcon, StopCircleIcon } from "lucide-react" import { Image } from "antd" import { useSpeechRecognition } from "~hooks/useSpeechRecognition" type Props = { dropedFile: File | undefined } export const PlaygroundForm = ({ dropedFile }: Props) => { const textareaRef = React.useRef(null) const inputRef = React.useRef(null) const resetHeight = () => { const textarea = textareaRef.current if (textarea) { textarea.style.height = "auto" } } const form = useForm({ initialValues: { message: "", image: "" } }) React.useEffect(() => { if (textareaRef.current) { textareaRef.current.focus() } }, []) const onInputChange = async ( e: React.ChangeEvent | File ) => { if (e instanceof File) { const base64 = await toBase64(e) form.setFieldValue("image", base64) } else { if (e.target.files) { const base64 = await toBase64(e.target.files[0]) form.setFieldValue("image", base64) } } } React.useEffect(() => { if (dropedFile) { onInputChange(dropedFile) } }, [dropedFile]) useDynamicTextareaSize(textareaRef, form.values.message, 300) const { onSubmit, selectedModel, chatMode, speechToTextLanguage, stopStreamingRequest } = useMessageOption() const { isListening, start, stop, transcript } = useSpeechRecognition() React.useEffect(() => { if (isListening) { form.setFieldValue("message", transcript) } }, [transcript]) const queryClient = useQueryClient() const { mutateAsync: sendMessage, isPending: isSending } = useMutation({ mutationFn: onSubmit, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["fetchChatHistory"] }) } }) return (
Uploaded Image
{/*
*/}
{ if (!selectedModel || selectedModel.length === 0) { form.setFieldError("message", "Please select a model") return } form.reset() resetHeight() await sendMessage({ image: value.image, message: value.message.trim() }) })} className="shrink-0 flex-grow flex flex-col items-center ">