diff --git a/app/components/chat/BaseChat.tsx b/app/components/chat/BaseChat.tsx index 730a158..23ee665 100644 --- a/app/components/chat/BaseChat.tsx +++ b/app/components/chat/BaseChat.tsx @@ -22,6 +22,8 @@ import { ExportChatButton } from '~/components/chat/chatExportAndImport/ExportCh import { ImportButtons } from '~/components/chat/chatExportAndImport/ImportButtons'; import { ExamplePrompts } from '~/components/chat/ExamplePrompts'; +import FilePreview from './FilePreview'; + // @ts-ignore TODO: Introduce proper types // eslint-disable-next-line @typescript-eslint/no-unused-vars const ModelSelector = ({ model, setModel, provider, setProvider, modelList, providerList, apiKeys }) => { @@ -85,8 +87,11 @@ interface BaseChatProps { enhancePrompt?: () => void; importChat?: (description: string, messages: Message[]) => Promise; exportChat?: () => void; + uploadedFiles?: File[]; + setUploadedFiles?: (files: File[]) => void; + imageDataList?: string[]; + setImageDataList?: (dataList: string[]) => void; } - export const BaseChat = React.forwardRef( ( { @@ -96,20 +101,24 @@ export const BaseChat = React.forwardRef( showChat = true, chatStarted = false, isStreaming = false, - enhancingPrompt = false, - promptEnhanced = false, - messages, - input = '', model, setModel, provider, setProvider, - sendMessage, + input = '', + enhancingPrompt, handleInputChange, + promptEnhanced, enhancePrompt, + sendMessage, handleStop, importChat, exportChat, + uploadedFiles = [], + setUploadedFiles, + imageDataList = [], + setImageDataList, + messages, }, ref, ) => { @@ -159,6 +168,58 @@ export const BaseChat = React.forwardRef( } }; + const handleFileUpload = () => { + const input = document.createElement('input'); + input.type = 'file'; + input.accept = 'image/*'; + + input.onchange = async (e) => { + const file = (e.target as HTMLInputElement).files?.[0]; + + if (file) { + const reader = new FileReader(); + + reader.onload = (e) => { + const base64Image = e.target?.result as string; + setUploadedFiles?.([...uploadedFiles, file]); + setImageDataList?.([...imageDataList, base64Image]); + }; + reader.readAsDataURL(file); + } + }; + + input.click(); + }; + + const handlePaste = async (e: React.ClipboardEvent) => { + const items = e.clipboardData?.items; + + if (!items) { + return; + } + + for (const item of items) { + if (item.type.startsWith('image/')) { + e.preventDefault(); + + const file = item.getAsFile(); + + if (file) { + const reader = new FileReader(); + + reader.onload = (e) => { + const base64Image = e.target?.result as string; + setUploadedFiles?.([...uploadedFiles, file]); + setImageDataList?.([...imageDataList, base64Image]); + }; + reader.readAsDataURL(file); + } + + break; + } + } + }; + const baseChat = (
( )}
- + { + setUploadedFiles?.(uploadedFiles.filter((_, i) => i !== index)); + setImageDataList?.(imageDataList.filter((_, i) => i !== index)); + }} + />
( >