Skip to content

An AI-powered search engine with a generative UI

refactor: implement parts-based rendering for user messages - Split UserMessage into UserTextSection and UserFileSection components - UserTextSection: handles text parts with editing functionality, wrapped in CollapsibleMessage - UserFileSection: handles file parts without CollapsibleMessage wrapper - Update render-message.tsx to use parts-based rendering for user messages - Remove original user-message.tsx component - Maintain consistent rendering structure between user and assistant messages

Yoshiki Miurae36bb6713536
diff --git a/components/render-message.tsx b/components/render-message.tsx
index 297c0466..26c1899b 100644
--- a/components/render-message.tsx
+++ b/components/render-message.tsx
@@ -2,7 +2,8 @@ import { UIMessage, UseChatHelpers } from '@ai-sdk/react'
import { AnswerSection } from './answer-section'
import { ReasoningSection } from './reasoning-section'
import { ToolSection } from './tool-section'
-import { UserMessage } from './user-message'
+import { UserFileSection } from './user-file-section'
+import { UserTextSection } from './user-text-section'
interface RenderMessageProps {
message: UIMessage
@@ -31,15 +32,37 @@ export function RenderMessage({
}: RenderMessageProps) {
if (message.role === 'user') {
return (
- <UserMessage
- message={message}
- messageId={messageId}
- onUpdateMessage={onUpdateMessage}
- />
+ <>
+ {message.parts?.map((part, index) => {
+ switch (part.type) {
+ case 'text':
+ return (
+ <UserTextSection
+ key={`${messageId}-user-text-${index}`}
+ content={part.text}
+ messageId={messageId}
+ onUpdateMessage={onUpdateMessage}
+ />
+ )
+ case 'file':
+ return (
+ <UserFileSection
+ key={`${messageId}-user-file-${index}`}
+ file={{
+ name: part.filename || 'Unknown file',
+ url: part.url,
+ contentType: part.mediaType
+ }}
+ />
+ )
+ default:
+ return null
+ }
+ })}
+ </>
)
}
- // New way: Use parts instead of toolInvocations
return (
<>
{message.parts?.map((part, index) => {
diff --git a/components/user-file-section.tsx b/components/user-file-section.tsx
new file mode 100644
index 00000000..8155bf66
--- /dev/null
+++ b/components/user-file-section.tsx
@@ -0,0 +1,14 @@
+import React from 'react'
+import { AttachmentPreview } from './attachment-preview'
+
+interface UserFileSectionProps {
+ file: {
+ name: string
+ url: string
+ contentType: string
+ }
+}
+
+export const UserFileSection: React.FC<UserFileSectionProps> = ({ file }) => {
+ return <AttachmentPreview attachments={[file]} />
+}
diff --git a/components/user-message.tsx b/components/user-text-section.tsx
similarity index 74%
rename from components/user-message.tsx
rename to components/user-text-section.tsx
index 0a8bb97f..ecc9a9e7 100644
--- a/components/user-message.tsx
+++ b/components/user-text-section.tsx
@@ -1,36 +1,29 @@
'use client'
import { cn } from '@/lib/utils'
-import { UIMessage } from '@ai-sdk/react'
import { Pencil } from 'lucide-react'
import React, { useState } from 'react'
import TextareaAutosize from 'react-textarea-autosize'
-import { AttachmentPreview } from './attachment-preview'
import { CollapsibleMessage } from './collapsible-message'
import { Button } from './ui/button'
-type UserMessageProps = {
- message: UIMessage
+interface UserTextSectionProps {
+ content: string
messageId?: string
onUpdateMessage?: (messageId: string, newContent: string) => Promise<void>
}
-export const UserMessage: React.FC<UserMessageProps> = ({
- message,
+export const UserTextSection: React.FC<UserTextSectionProps> = ({
+ content,
messageId,
onUpdateMessage
}) => {
- const messageText =
- message.parts
- ?.filter(part => part.type === 'text')
- .map(part => part.text)
- .join('') || ''
const [isEditing, setIsEditing] = useState(false)
- const [editedContent, setEditedContent] = useState(messageText)
+ const [editedContent, setEditedContent] = useState(content)
const handleEditClick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation()
- setEditedContent(messageText)
+ setEditedContent(content)
setIsEditing(true)
}
@@ -50,24 +43,12 @@ 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">
<div
className="flex-1 break-words w-full group outline-none relative"
tabIndex={0}
>
- {!isEditing && derivedAttachments && derivedAttachments.length > 0 && (
- <AttachmentPreview attachments={derivedAttachments} />
- )}
{isEditing ? (
<div className="flex flex-col gap-2">
<TextareaAutosize
@@ -89,7 +70,7 @@ export const UserMessage: React.FC<UserMessageProps> = ({
</div>
) : (
<div className="flex justify-between items-start">
- <div className="flex-1">{messageText}</div>
+ <div className="flex-1">{content}</div>
<div
className={cn(
'absolute top-1 right-1 transition-opacity ml-2',