atrokhym commited on
Commit
0ab3341
·
1 Parent(s): ccaa67b

adding to display the image in the chat conversation. and paste image too. tnx to @Stijnus

Browse files
app/components/chat/BaseChat.tsx CHANGED
@@ -190,6 +190,35 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
190
  input.click();
191
  };
192
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  const baseChat = (
194
  <div
195
  ref={ref}
@@ -286,6 +315,7 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
286
  onChange={(event) => {
287
  handleInputChange?.(event);
288
  }}
 
289
  style={{
290
  minHeight: TEXTAREA_MIN_HEIGHT,
291
  maxHeight: TEXTAREA_MAX_HEIGHT,
 
190
  input.click();
191
  };
192
 
193
+ const handlePaste = async (e: React.ClipboardEvent) => {
194
+ const items = e.clipboardData?.items;
195
+
196
+ if (!items) {
197
+ return;
198
+ }
199
+
200
+ for (const item of items) {
201
+ if (item.type.startsWith('image/')) {
202
+ e.preventDefault();
203
+
204
+ const file = item.getAsFile();
205
+
206
+ if (file) {
207
+ const reader = new FileReader();
208
+
209
+ reader.onload = (e) => {
210
+ const base64Image = e.target?.result as string;
211
+ setUploadedFiles?.([...uploadedFiles, file]);
212
+ setImageDataList?.([...imageDataList, base64Image]);
213
+ };
214
+ reader.readAsDataURL(file);
215
+ }
216
+
217
+ break;
218
+ }
219
+ }
220
+ };
221
+
222
  const baseChat = (
223
  <div
224
  ref={ref}
 
315
  onChange={(event) => {
316
  handleInputChange?.(event);
317
  }}
318
+ onPaste={handlePaste}
319
  style={{
320
  minHeight: TEXTAREA_MIN_HEIGHT,
321
  maxHeight: TEXTAREA_MAX_HEIGHT,
app/components/chat/UserMessage.tsx CHANGED
@@ -6,10 +6,39 @@ import { MODEL_REGEX, PROVIDER_REGEX } from '~/utils/constants';
6
  import { Markdown } from './Markdown';
7
 
8
  interface UserMessageProps {
9
- content: string;
10
  }
11
 
12
  export function UserMessage({ content }: UserMessageProps) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  const textContent = sanitizeUserMessage(content);
14
 
15
  return (
@@ -19,11 +48,6 @@ export function UserMessage({ content }: UserMessageProps) {
19
  );
20
  }
21
 
22
- function sanitizeUserMessage(content: string | Array<{ type: string; text?: string; image_url?: { url: string } }>) {
23
- if (Array.isArray(content)) {
24
- const textItem = content.find((item) => item.type === 'text');
25
- return textItem?.text?.replace(MODEL_REGEX, '').replace(PROVIDER_REGEX, '') || '';
26
- }
27
-
28
  return content.replace(MODEL_REGEX, '').replace(PROVIDER_REGEX, '');
29
  }
 
6
  import { Markdown } from './Markdown';
7
 
8
  interface UserMessageProps {
9
+ content: string | Array<{ type: string; text?: string; image?: string }>;
10
  }
11
 
12
  export function UserMessage({ content }: UserMessageProps) {
13
+ if (Array.isArray(content)) {
14
+ const textItem = content.find((item) => item.type === 'text');
15
+ const textContent = sanitizeUserMessage(textItem?.text || '');
16
+ const images = content.filter((item) => item.type === 'image' && item.image);
17
+
18
+ return (
19
+ <div className="overflow-hidden pt-[4px]">
20
+ <div className="flex items-start gap-4">
21
+ <div className="flex-1">
22
+ <Markdown limitedMarkdown>{textContent}</Markdown>
23
+ </div>
24
+ {images.length > 0 && (
25
+ <div className="flex-shrink-0 w-[160px]">
26
+ {images.map((item, index) => (
27
+ <div key={index} className="relative">
28
+ <img
29
+ src={item.image}
30
+ alt={`Uploaded image ${index + 1}`}
31
+ className="w-full h-[160px] rounded-lg object-cover border border-bolt-elements-borderColor"
32
+ />
33
+ </div>
34
+ ))}
35
+ </div>
36
+ )}
37
+ </div>
38
+ </div>
39
+ );
40
+ }
41
+
42
  const textContent = sanitizeUserMessage(content);
43
 
44
  return (
 
48
  );
49
  }
50
 
51
+ function sanitizeUserMessage(content: string) {
 
 
 
 
 
52
  return content.replace(MODEL_REGEX, '').replace(PROVIDER_REGEX, '');
53
  }