import { useState } from "react"; import { Button } from "../button"; import FileUploader from "../file-uploader"; import { Input } from "../input"; import UploadImagePreview from "../upload-image-preview"; import { ChatHandler } from "./chat.interface"; export default function ChatInput( props: Pick< ChatHandler, | "isLoading" | "input" | "onFileUpload" | "onFileError" | "handleSubmit" | "handleInputChange" > & { multiModal?: boolean; }, ) { const [imageUrl, setImageUrl] = useState(null); const onSubmit = (e: React.FormEvent) => { if (imageUrl) { props.handleSubmit(e, { data: { imageUrl: imageUrl }, }); setImageUrl(null); return; } props.handleSubmit(e); }; const onRemovePreviewImage = () => setImageUrl(null); const handleUploadImageFile = async (file: File) => { const base64 = await new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => resolve(reader.result as string); reader.onerror = (error) => reject(error); }); setImageUrl(base64); }; const handleUploadFile = async (file: File) => { try { if (props.multiModal && file.type.startsWith("image/")) { return await handleUploadImageFile(file); } props.onFileUpload?.(file); } catch (error: any) { props.onFileError?.(error.message); } }; return (
{imageUrl && ( )}
); }