An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
add attachements preview / impl
oussamadhouib6cda15fdd4bb
diff --git a/app/api/upload/route.ts b/app/api/upload/route.tsindex 3b1d7cbc..543ddbe7 100644--- a/app/api/upload/route.ts+++ b/app/api/upload/route.ts@@ -59,7 +59,7 @@ async function uploadFileToSupabase(file: File) {const filePath = `${Date.now()}-${sanitizedFileName}`const supabase = await createClient()const { error } = await supabase.storage- .from('chat-uploads')+ .from('user-uploads').upload(filePath, file, {cacheControl: '3600',upsert: false,@@ -75,9 +75,9 @@ async function uploadFileToSupabase(file: File) {} = supabase.storage.from('user-uploads').getPublicUrl(filePath)return {- name: file.name,+ filename: file.name,url: publicUrl,- contentType: file.type,- key: filePath+ mediaType: file.type,+ type: 'file'}}diff --git a/components/attachment-preview.tsx b/components/attachment-preview.tsxindex 4be9c473..6447e837 100644--- a/components/attachment-preview.tsx+++ b/components/attachment-preview.tsx@@ -3,11 +3,11 @@import React from 'react'interface Attachment {- name: string+ name: string | undefinedurl: stringcontentType: string}-+;[]interface AttachmentPreviewProps {attachments: Attachment[]}diff --git a/components/chat.tsx b/components/chat.tsxindex 3e13f7c5..89218cfe 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -277,9 +277,32 @@ export function Chat({}}- const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {+ const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {e.preventDefault()- handleSubmit(e)++ const uploadedAttachments = await Promise.all(+ uploadedFiles.map(async file => {+ const formData = new FormData()+ formData.append('file', file)+ const res = await fetch('/api/upload', {+ method: 'POST',+ body: formData+ })+ if (!res.ok) {+ const errorData = await res.json()+ toast.error(`Upload failed: ${errorData.error || 'Unknown error'}`)+ return null+ }+ const { file: uploadedFile } = await res.json()+ return uploadedFile // contains name, url, contentType, key+ })+ )+ const validAttachments = uploadedAttachments.filter(Boolean) // remove failed uploads+ handleSubmit(e, {+ files: validAttachments+ })++ setUploadedFiles([])}const { isDragging, handleDragOver, handleDragLeave, handleDrop } =diff --git a/components/render-message.tsx b/components/render-message.tsxindex f4b43533..297c0466 100644--- a/components/render-message.tsx+++ b/components/render-message.tsx@@ -15,11 +15,6 @@ interface RenderMessageProps {addToolResult?: (params: { toolCallId: string; result: any }) => voidonUpdateMessage?: (messageId: string, newContent: string) => Promise<void>reload?: (messageId: string) => Promise<void | string | null | undefined>- attachments?: {- name: string- url: string- contentType: string- }[]}export function RenderMessage({@@ -32,8 +27,7 @@ export function RenderMessage({status,addToolResult,onUpdateMessage,- reload,- attachments+ reload}: RenderMessageProps) {if (message.role === 'user') {return (@@ -41,7 +35,6 @@ export function RenderMessage({message={message}messageId={messageId}onUpdateMessage={onUpdateMessage}- attachments={attachments}/>)}diff --git a/components/user-message.tsx b/components/user-message.tsxindex a3831d12..0a8bb97f 100644--- a/components/user-message.tsx+++ b/components/user-message.tsx@@ -13,18 +13,12 @@ type UserMessageProps = {message: UIMessagemessageId?: stringonUpdateMessage?: (messageId: string, newContent: string) => Promise<void>- attachments?: {- name: string- url: string- contentType: string- }[]}export const UserMessage: React.FC<UserMessageProps> = ({message,messageId,- onUpdateMessage,- attachments+ onUpdateMessage}) => {const messageText =message.parts@@ -56,14 +50,23 @@ export const UserMessage: React.FC<UserMessageProps> = ({}}+ const derivedAttachments =+ message.parts+ ?.filter(part => part.type === 'file')+ .map(part => ({+ name: part.filename,+ url: part.url,+ contentType: part.mediaType+ })) || []+return (<CollapsibleMessage role="user"><divclassName="flex-1 break-words w-full group outline-none relative"tabIndex={0}>- {!isEditing && attachments && attachments.length > 0 && (- <AttachmentPreview attachments={attachments} />+ {!isEditing && derivedAttachments && derivedAttachments.length > 0 && (+ <AttachmentPreview attachments={derivedAttachments} />)}{isEditing ? (<div className="flex flex-col gap-2">