An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: Added convertToUIMessages function in share page
Yoshiki Miura57b2de4a75fa
diff --git a/app/search/[id]/page.tsx b/app/search/[id]/page.tsxindex 0c96bea0..25a909c2 100644--- a/app/search/[id]/page.tsx+++ b/app/search/[id]/page.tsx@@ -1,7 +1,6 @@import { notFound, redirect } from 'next/navigation'import { Chat } from '@/components/chat'import { getChat } from '@/lib/actions/chat'-import { convertToCoreMessages } from 'ai'import { convertToUIMessages } from '@/lib/utils'export const maxDuration = 60@@ -33,7 +32,5 @@ export default async function SearchPage(props: {notFound()}- console.log(chat)-return <Chat id={id} savedMessages={messages} />}diff --git a/app/share/[id]/page.tsx b/app/share/[id]/page.tsxindex 353b6066..c94c688a 100644--- a/app/share/[id]/page.tsx+++ b/app/share/[id]/page.tsx@@ -1,6 +1,7 @@import { notFound } from 'next/navigation'import { Chat } from '@/components/chat'import { getSharedChat } from '@/lib/actions/chat'+import { convertToUIMessages } from '@/lib/utils'export async function generateMetadata(props: {params: Promise<{ id: string }>@@ -22,10 +23,12 @@ export default async function SharePage(props: {}) {const { id } = await props.paramsconst chat = await getSharedChat(id)+ // convertToUIMessages for useChat hook+ const messages = convertToUIMessages(chat?.messages || [])if (!chat || !chat.sharePath) {notFound()}- return <Chat id={id} />+ return <Chat id={id} savedMessages={messages} />}diff --git a/components/chat-share.tsx b/components/chat-share.tsxindex 2dc92c6d..d60f73f9 100644--- a/components/chat-share.tsx+++ b/components/chat-share.tsx@@ -29,12 +29,9 @@ export function ChatShare({ chatId, className }: ChatShareProps) {const [pending, startTransition] = useTransition()const { copyToClipboard } = useCopyToClipboard({ timeout: 1000 })const [shareUrl, setShareUrl] = useState('')- const { isLoading, messages } = useChat({+ const { isLoading } = useChat({id: 'chat'})- const hasMultipleUserMessages =- messages.filter(message => message.role === 'user').length >= 2- const showShareButton = hasMultipleUserMessages ? true : !isLoadingconst handleShare = async () => {startTransition(() => {@@ -75,11 +72,11 @@ export function ChatShare({ chatId, className }: ChatShareProps) {><DialogTrigger asChild><Button- className={cn('rounded-full', !showShareButton && 'hidden')}+ className={cn('rounded-full', isLoading && 'hidden')}size="icon"variant={'ghost'}onClick={() => setOpen(true)}- disabled={!showShareButton}+ disabled={isLoading}><Share size={14} /></Button>