chatbot-template/app/(chat)/chat/[id]/page.tsx

48 lines
966 B
TypeScript
Raw Normal View History

2023-06-02 15:33:48 -04:00
import { type Metadata } from 'next'
2023-06-16 21:52:01 +04:00
import { notFound, redirect } from 'next/navigation'
2023-06-16 21:52:01 +04:00
import { auth } from '@/auth'
import { getChat } from '@/app/actions'
2023-06-16 21:52:01 +04:00
import { Chat } from '@/components/chat'
2023-06-02 13:29:54 -04:00
2023-05-19 12:33:56 -04:00
export interface ChatPageProps {
params: {
2023-06-02 15:33:48 -04:00
id: string
}
2023-05-19 12:33:56 -04:00
}
export async function generateMetadata({
2023-06-02 15:33:48 -04:00
params
2023-05-19 12:33:56 -04:00
}: ChatPageProps): Promise<Metadata> {
2023-06-16 21:52:01 +04:00
const session = await auth()
if (!session?.user) {
return {}
}
const chat = await getChat(params.id, session.user.id)
2023-05-19 12:33:56 -04:00
return {
title: chat?.title.toString().slice(0, 50) ?? 'Chat'
2023-06-02 15:33:48 -04:00
}
2023-05-19 12:33:56 -04:00
}
export default async function ChatPage({ params }: ChatPageProps) {
2023-06-16 21:52:01 +04:00
const session = await auth()
if (!session?.user) {
2023-06-16 15:28:43 -04:00
redirect(`/sign-in?next=/chat/${params.id}`)
2023-06-16 21:52:01 +04:00
}
const chat = await getChat(params.id, session.user.id)
if (!chat) {
notFound()
}
2023-05-19 12:33:56 -04:00
2023-06-16 15:28:43 -04:00
if (chat?.userId !== session?.user?.id) {
notFound()
}
2023-06-15 16:07:03 +04:00
return <Chat id={chat.id} initialMessages={chat.messages} />
2023-06-02 15:15:35 -04:00
}