An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: Add MessageWithAvatar component for consistent message display
Yoshiki Miurac0e7ec3ac6c6
diff --git a/components/answer-section.tsx b/components/answer-section.tsxindex ed681a85..cdfae8c0 100644--- a/components/answer-section.tsx+++ b/components/answer-section.tsx@@ -3,6 +3,7 @@import { Section } from './section'import { BotMessage } from './message'import { DefaultSkeleton } from './default-skeleton'+import { MessageWithAvatar } from './message-with-avatar'export type AnswerSectionProps = {content: string@@ -10,10 +11,10 @@ export type AnswerSectionProps = {export function AnswerSection({ content }: AnswerSectionProps) {return (- <div>+ <MessageWithAvatar role="assistant"><Section title="Answer">{content ? <BotMessage message={content} /> : <DefaultSkeleton />}</Section>- </div>+ </MessageWithAvatar>)}diff --git a/components/message-with-avatar.tsx b/components/message-with-avatar.tsxnew file mode 100644index 00000000..607537d2--- /dev/null+++ b/components/message-with-avatar.tsx@@ -0,0 +1,22 @@+import { cn } from '@/lib/utils'+import { UserCircle2, Bot } from 'lucide-react'++interface MessageWithAvatarProps {+ children: React.ReactNode+ role: 'user' | 'assistant'+}++export function MessageWithAvatar({ children, role }: MessageWithAvatarProps) {+ return (+ <div className="flex gap-3">+ <div className="relative flex flex-col items-center">+ <div className={cn('mt-[10px]', role === 'assistant' && 'pl-4')}>+ {role === 'user' && (+ <UserCircle2 size={20} className="text-muted-foreground" />+ )}+ </div>+ </div>+ <div className="py-2 flex-1">{children}</div>+ </div>+ )+}diff --git a/components/related-questions.tsx b/components/related-questions.tsxindex dd09e850..7bf2a6a1 100644--- a/components/related-questions.tsx+++ b/components/related-questions.tsx@@ -6,6 +6,7 @@ import { ArrowRight } from 'lucide-react'import { Section } from './section'import { Skeleton } from './ui/skeleton'import { JSONValue } from 'ai'+import { MessageWithAvatar } from './message-with-avatar'export interface RelatedQuestionsProps {annotations: JSONValue[]@@ -40,38 +41,42 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({const relatedQuestions = lastRelatedQuestionsAnnotation?.relatedQuestionsif (!relatedQuestions) {return (- <Section title="Related" separator={true}>- <Skeleton className="w-full h-6" />- </Section>+ <MessageWithAvatar role="assistant">+ <Section title="Related" separator={true}>+ <Skeleton className="w-full h-6" />+ </Section>+ </MessageWithAvatar>)}return (- <Section title="Related" separator={true}>- <div className="flex flex-wrap">- {Array.isArray(relatedQuestions.items) ? (- relatedQuestions.items- ?.filter(item => item?.query !== '')- .map((item, index) => (- <div className="flex items-start w-full" key={index}>- <ArrowRight className="h-4 w-4 mr-2 mt-1 flex-shrink-0 text-accent-foreground/50" />- <Button- variant="link"- className="flex-1 justify-start px-0 py-1 h-fit font-semibold text-accent-foreground/50 whitespace-normal text-left"- type="submit"- name={'related_query'}- value={item?.query}- onClick={() => onQuerySelect(item?.query)}- >- {item?.query}- </Button>- </div>- ))- ) : (- <div>Not an array</div>- )}- </div>- </Section>+ <MessageWithAvatar role="assistant">+ <Section title="Related" separator={true}>+ <div className="flex flex-wrap">+ {Array.isArray(relatedQuestions.items) ? (+ relatedQuestions.items+ ?.filter(item => item?.query !== '')+ .map((item, index) => (+ <div className="flex items-start w-full" key={index}>+ <ArrowRight className="h-4 w-4 mr-2 mt-1 flex-shrink-0 text-accent-foreground/50" />+ <Button+ variant="link"+ className="flex-1 justify-start px-0 py-1 h-fit font-semibold text-accent-foreground/50 whitespace-normal text-left"+ type="submit"+ name={'related_query'}+ value={item?.query}+ onClick={() => onQuerySelect(item?.query)}+ >+ {item?.query}+ </Button>+ </div>+ ))+ ) : (+ <div>Not an array</div>+ )}+ </div>+ </Section>+ </MessageWithAvatar>)}export default RelatedQuestionsdiff --git a/components/tool-section.tsx b/components/tool-section.tsxindex 48942289..59f9f3ca 100644--- a/components/tool-section.tsx+++ b/components/tool-section.tsx@@ -4,20 +4,25 @@ import { ToolInvocation } from 'ai'import { SearchSection } from './search-section'import { VideoSearchSection } from './video-search-section'import RetrieveSection from './retrieve-section'+import { MessageWithAvatar } from './message-with-avatar'interface ToolSectionProps {tool: ToolInvocation}export function ToolSection({ tool }: ToolSectionProps) {- switch (tool.toolName) {- case 'search':- return <SearchSection tool={tool} />- case 'video_search':- return <VideoSearchSection tool={tool} />- case 'retrieve':- return <RetrieveSection tool={tool} />- default:- return null- }+ const content = (() => {+ switch (tool.toolName) {+ case 'search':+ return <SearchSection tool={tool} />+ case 'video_search':+ return <VideoSearchSection tool={tool} />+ case 'retrieve':+ return <RetrieveSection tool={tool} />+ default:+ return null+ }+ })()++ return <MessageWithAvatar role="assistant">{content}</MessageWithAvatar>}diff --git a/components/user-message.tsx b/components/user-message.tsxindex b798bc31..c3f11492 100644--- a/components/user-message.tsx+++ b/components/user-message.tsx@@ -1,6 +1,6 @@import React from 'react'import { ChatShare } from './chat-share'-+import { MessageWithAvatar } from './message-with-avatar'type UserMessageProps = {message: stringchatId?: string@@ -14,9 +14,9 @@ export const UserMessage: React.FC<UserMessageProps> = ({}) => {const enableShare = process.env.ENABLE_SHARE === 'true'return (- <div className="flex items-center w-full space-x-1 mt-2 min-h-10">- <div className="text-xl flex-1 break-words w-full">{message}</div>+ <MessageWithAvatar role="user">+ <div className="flex-1 break-words w-full">{message}</div>{enableShare && showShare && chatId && <ChatShare chatId={chatId} />}- </div>+ </MessageWithAvatar>)}