'use client'; /* eslint-disable @next/next/no-img-element */ import { useChat } from '@ai-sdk/react'; import { useRef, useState } from 'react'; export default function Page() { const { messages, sendMessage, status } = useChat(); const [input, setInput] = useState(''); const [files, setFiles] = useState(undefined); const fileInputRef = useRef(null); return (
{messages.map(message => (
{`${message.role}: `}
{message.parts.map((part, index) => { if (part.type === 'text') { return
{part.text}
; } if ( part.type === 'file' && part.mediaType?.startsWith('image/') ) { return (
{part.filename}
); } })}
))}
{ event.preventDefault(); sendMessage({ text: input, files }); setFiles(undefined); setInput(''); if (fileInputRef.current) { fileInputRef.current.value = ''; } }} className="fixed bottom-0 flex flex-col w-full gap-2 p-2" >
{files ? Array.from(files).map(attachment => { const { type } = attachment; if (type.startsWith('image/')) { return (
{attachment.name} {attachment.name}
); } }) : ''}
{ if (event.target.files) { setFiles(event.target.files); } }} multiple ref={fileInputRef} /> setInput(e.target.value)} className="w-full p-2 bg-zinc-100" disabled={status !== 'ready'} />
); }